This topic applies only to the IBM Business Automation Workflow Advanced
configuration.

Creating a JMS client to receive messages from a JMS import

Draft comment:
This topic only applies to BAW, and is located in the BAW repository. Last updated on 2025-01-20 10:38
Draft comment:
This topic was viewed 4 times since its publication
Previously, the focus has been to show how to create SCA imports and exports to communicate with a JMS client. This section shows the reverse; that is, if you have an SCA import with an interface and a business object, this section shows the JMS client code you would need to create to communicate with them.

Before you begin

Prerequisite: You should have a module called CustomerModule created. Next follow these steps to build the SCA artifacts and binding necessary to interact with the JMS client.
  1. In your CustomerModule module, create an interface called CustomerProcess. It should have a request-response operation getCustomerAddress with an input, customer, and an output, customerResult. Both input and output have a business object type, Customer.
  2. Create a business object, Customer, with two fields, name and address, both with a string type.
  3. Create an import with a JMS binding using the following settings: JMS messaging domain set to Point-to-Point and data format transformation to Serialized Java Object (JMS).

Procedure

  1. With the SCA artifacts created and the JMS import binding created, create a JMS client like the one in the next step to receive messages from the import.
  2. The SCA application simply sends a business object with a name value to the JMS client and expects the client to return the address for that name. For the JMS client, here is an illustration of Message-Driven Bean (MDB) code that can interact with the SCA application.
    public void onMessage(javax.jms.Message msg) {
    	try {
    		// Get the value 'TargetFunctionName'
    		String targetFunctionNameValue = msg.getStringProperty("TargetFunctionName");
    		
    		// Base off the 'TargetFunctionName', call the appropriate method to
    		// process
    		if (targetFunctionNameValue.equals("getCustomerAddress")) {
    			getCustomerAddress(msg);
    		}
    	} catch (JMSException e) {
    		// Todo - handle exception
    		e.printStackTrace();
    	}
    }
    
    private void getCustomerAddress(javax.jms.Message msg) {
    	try {
    		
    		// Cast the incoming JMS Message to TextMessage since the SOA
    		// application
    		// is using 'Business Object XML using JMS TextMessage' as the
    		// serialization format
    		TextMessage message = (TextMessage)msg;
    		
    		// Get the context in the TextMessage. It would be the XML
    		// of the BusinessObject 'Customer'. For example:
    		//
    		// <?xml version="1.0" encoding="UTF-8"?>
    		// <module:Customer xmlns:module="http://CustomerModule">
    		// <name>Name1</name>
    		// <address></address>
    		// </module:Customer>
    		
    		String customerXML = message.getText();
    		
    		// Have code to process the XML
    		// We will do a rudimentary processing of this by doing a String
    		// operations
    		
    		// Get the value of the name element
    		int beginIndex = customerXML.indexOf("<name>");
    		int endIndex = customerXML.indexOf("</name>");
    		String name = customerXML.substring(beginIndex + 6, endIndex);
    		
    		String newCustomerXML = null;
    		if (name == null)
    			newCustomerXML = customerXML;
    		else if (name.equals("Nathan"))
    			newCustomerXML = customerXML.replaceFirst("<address></address>", "<address>123 AppleValley Drive</address>");
    		else 
    			newCustomerXML = customerXML.replaceFirst("<address></address>", "<address>No such address</address>");
    		
    		// **** Send the response message ***
    		// Lookup the Connection Factory JNDI
    		Context context = new InitialContext();
    		ConnectionFactory factory = (ConnectionFactory)context.lookup("java:comp/env/jms/CF");
    		
    		if (factory != null) {
    			Connection connection = factory.createConnection();
    			Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    			
    			// Create the Message Producer using the Destination
    			// specified in the sent message 'JMSReplyTo'
    			MessageProducer producer = session.createProducer(msg.getJMSReplyTo());
    			TextMessage responseMessage = session.createTextMessage(newCustomerXML);
    			
    			// Need to correlate the response message by specifying the
    			// 'JMSCorrelationID
    			// to the sent message 'JMSMessageID'
    			responseMessage.setJMSCorrelationID(msg.getJMSMessageID());
    			
    			// Send the response message
    			connection.start();
    			producer.send(responseMessage);
    			connection.close();
    		}			
    	} catch (JMSException e) {
    		// Todo - handle exception
    		e.printStackTrace();
    	} catch (NamingException e) {
    		// Todo - handle exception
    		e.printStackTrace();
    	} 
    	
    }