IBM BPM, V8.0.1, All platforms > Authoring services in Integration Designer > Services and service-related functions > Access external services with messaging systems > Java Message Service (JMS) > Create applications with JMS bindings
Create a JMS client to receive messages from a JMS import
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.
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.
- 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.
- Create a business object, Customer, with two fields, name and address, both with a string type.
- 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
- 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.
- 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(); } }
Create applications with JMS bindings
Related tasks:
Create a JMS import to communicate with a JMS client
Create a JMS export to communicate with a JMS client
Create an import from an export using a JMS binding
Create a user-defined JMS data binding