Working with external clients

An external client (i.e., outside of WebSphere Process Server) may need to interact with an application installed in WebSphere Process Server.

Why and when to perform this task

To illustrate working with external clients we consider a very simple scenario in which an external client wishes to interact with a generic application on a WebSphere Process Server. The figure depicts a typical simple scenario.
Figure 1. A simple use-case scenario. An external client interacts with a WebSphere Process Server application.A client Java Virtual Machine (JVM) is shown containing a non-WepSphere process server client. A WebSphere Process Server installation is shown featuring a WebSphere Process Server-based service. The two are linked by a double-headed arrow labelled "service invocation".

The service is exported using a JMS binding, this makes the service available to external clients.

When you have an external client in a Java virtual machine (JVM) separate from WebSphere Process Server, there are several steps you must take in order to make a connection and interact with a JMS Export. The client obtains an InitialContext with the correct values then look up the resources through JNDI. The client then using JMS 1.1 spec client to access the destinations and send and receive messages on the destinations. The default JNDI names of the resources created automatically by the runtime are listed in table xyz. If you have pre-created resources then use those JNDI names.

Steps for this task

  1. You must configure JMS destinations and connection factory to send the message.
  2. You must have the correct JNDI context and port for the SIB Resource Adapter and the messaging bootstrapping port.

    WebSphere Process Server uses some default ports but if there are more servers installed in that machine, alternative ports are created at installation time to avoid conflicts with other server instances. You can determine which ports WebSphere Process Server is employing with the administrative console. Go to Servers > Application Servers > your server name > Configuration and click Ports under Communication. You can edit the port which is being used by clicking on Ports or details.

  3. The client obtains an Initial Context with the correct values then look up the resources through JNDI.
  4. Using JMS 1.1 specifications, the client accesses the destinations and the Send and Receive messages on the destinations.

    The default JNDI names of the resources created automatically by the runtime are described in related topics.

    If you have pre-created resources then use the JNDI names of the resources that you created.

Example: request-response operation for an order processing application

Example: request-response operation for an order processing application

The client source code for a request-response operation is shown below.
ConnectionFactory conFact;
Connection conn;
Session sess;
Destination requestQueue, responseQueue;
MessageProducer requestProducer;
MessageConsumer replyConsumer;
String jmsRequestQName = "jms/ExportIn";
String jmsReplyQName = "jms/ExportOut";
String jmsFactoryName = "jms/MyCF";
// the initial context factory to be used to get the JNDI initial context
String jndiFactoryName = "com.ibm.websphere.naming.WsnInitialContextFactory";
// URL for bootstrapping, containing ip address of WPS host machine
// and bootstrapping port that by default is 2809
// you should change it to the values of your host:port
String jmsContextURL = "iiop://9.26.237.246:2809/";
TextMessage jmsRequestMessage, jmsReplyMessage;
// this is the serialized business object “order”,
// we have it already serialized as a String in order to avoid
// the extra complexity of serialization to this sample.
String message = " " String message = "<order:Order xsi:type=\"order:Order\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" 
xmlns:order=\"http://OrderProcessing\"> " 
+ "<orderId>123</orderId>"
+ "<customerId>456</customerId>"
+ "<orderDate>2002-01-01Z</orderDate>"
+ "<orderStatus>Reject</orderStatus>"
+ "<totalPrice>1000.0</totalPrice>" 
+ "</order:Order>";

System.out.println("JMS Factory name:" + jmsFactoryName);
System.out.println("Queue Factory name:" + jmsRequestQName);
System.out.println("Initial Context Provider URL:" + jmsContextURL);
System.out.println();

try {
  Hashtable jndiHash = new Hashtable();
jndiHash.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, 
jndiFactoryName);
	jndiHash.put(javax.naming.Context.PROVIDER_URL, jmsContextURL);
	javax.naming.Context ctx = new InitialContext(jndiHash);
	System.out.println("Got Initial Context from provider: " + 
jmsContextURL);

	conFact = (ConnectionFactory) ctx.lookup(jmsFactoryName);
	if (conFact != null) {
		System.out.println("Got Connection Factory: "	+ jmsFactoryName);
		System.out.println(conFact.toString());
	}

	conn = conFact.createConnection();
	conn.start();
	System.out.println("Created Queue Connection...");
	System.out.println(conn.toString());
			
	sess = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
	System.out.println("created Session...");
	System.out.println(sess.toString());
	System.out.println(conn.toString());

	requestQueue = (Queue) ctx.lookup(jmsRequestQName);
	if (requestQueue != null) {
System.out.println("Found JMS request Destination " + jmsRequestQName);
		System.out.println(requestQueue.toString());
	}
	responseQueue = (Queue) ctx.lookup(jmsReplyQName);
	if (requestQueue != null) {
		System.out.println("Found JMS reply-to Queue " + jmsReplyQName);
	}

	requestProducer = sess.createProducer(requestQueue);
	System.out.println("Created Sender...");

	jmsRequestMessage = sess.createTextMessage();
	System.out.println("Creating Message...");
	jmsRequestMessage.setText(message);
	jmsRequestMessage.setJMSReplyTo(responseQueue);
	jmsRequestMessage.setStringProperty("TargetFunctionName", 
"submitOrderRequestResponse");

	requestProducer.send(jmsRequestMessage);
	sess.commit();

	System.out.println("MESSAGE SENT!");
	System.out.println();
	System.out.println("JMS Correlation ID: ");
	String corrID = jmsRequestMessage.getJMSCorrelationID();
	if (corrID != null) {
		System.out.println(jmsRequestMessage.getJMSCorrelationID());
	}
	else {
		System.out.println("NULL");
	}
	System.out.println();
	System.out.println("JMS Message: ");
	System.out.println(jmsRequestMessage.toString());

	System.out.println();
	System.out.println("WAITING FOR THE RESPONSE...");
	System.out.println();

	responseQueue = (Queue) ctx.lookup(jmsReplyQName);
	if (responseQueue != null) {
		System.out.println("Found JMS response Destination " + 
jmsReplyQName);
		System.out.println(responseQueue.toString());
	}
	else {
		System.out.println("JMS response Destination not found: " +
jmsReplyQName);
	}

	replyConsumer = sess.createConsumer(responseQueue);
	jmsReplyMessage= (TextMessage)replyConsumer.receive(10000);
	if (jmsReplyMessage != null) {
		System.out.println("MESSAGE RECEIVED!!");
		System.out.println();
		System.out.println("JMS Message: ");
		System.out.println(jmsRequestMessage.toString());
	}
	else {
		System.out.println("DIDN'T FIND MESSAGE IN THE REMOTE QUEUE: " + 
jmsReplyQName);
	}
	sess.commit();
requestProducer.close();
	replyConsumer.close();
	sess.close();
	conn.close();
		
} catch (Exception j) {
	System.out.println("There was an exception, (for whole stack trace look 
in SystemErr.log): " + j.getMessage());
	j.printStackTrace();

}
Related concepts
Configuring JMS export bindings

Last updated: Wed 06 Dec 2006 07:08:08

(c) Copyright IBM Corporation 2005, 2006.
This information center is powered by Eclipse technology (http://www.eclipse.org)