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 an import from an export using a JMS binding

Once you have created an export, another module may want to use it. This section shows you how to quickly create an import in one module from an export in another using a JMS binding.


Prerequisite: You must have two modules. In this sample, one module is called JMSExport, where we will create the export component with the JMS binding. The second module we have called JMSImport, where we will create the import component with a JMS binding from the export component.

You will create an export component with a JMS binding. Then, in another module, you will create an import component from it also with a JMS binding.


Procedure

  1. Add a business object to the JMS Export module called CredentialsBO. Right-click the module and from the menu select New > Business Object. Add CredentialsBO to the Name field. Click Finish. The business object is created and opens in the business object editor.

  2. Add the following fields.

    Business object fields
    Name Data Type Property Description
    userid string Required: yes, Minimum length: 8, Maximum length: 8
    password string Required: yes, Minimum length: 4, Maximum length: 4

  3. Create an interface for the component. Select the module and from the menu, select New > Interface. In the Name field, add Account. Click Finish. The Account interface opens in the interface editor. Add the following fields to it.

    Interface fields
    Operation Input Input Type Output Output Type Fault Fault Type
    loginAccount credentials CredentialsBO response string credentialsError string
    selectAccount accountName string        
    updateAccount performCredit boolean balance int    
    amount int        

  4. Create a Java™ component in the assembly editor. Right-click the module and from the menu select Open. The assembly editor opens. From the palette, select a Java component and drag it onto the canvas. From the icons, select Add Interface and add the Account interface.

  5. Add the Java implementation to the component. Rename your component's display name and name AccountInfo. Name determines what the first part of your class name will be. Impl is added to it. So in this case your generated class name will be AccountInfoImpl. Right-click the component and from the menu select Generate implementation.

  6. The Generate Implementation window box opens. Click New Package and specify the package name as accountrecord. Then select it and click OK. A shell AccountInfoImpl class is created and opens in a Java editor, which means the code will be checked each time you save it. The operations you created earlier in the interface editor have become methods in Java. Add the following field at the class level: private String account;

  7. Add the following code for the loginAccount method.
    public String loginAccount(DataObject credentials) {
    		
    		String userid = credentials.getString("userid");
    		String password = credentials.getString("password");
    		
    		return "Result from loginAccount(DataObject credentials) is userid: " + userid + " password: " + password;
    	}

  8. Add the following code for the selectAccount method.
    public void selectAccount(String accountName) {
    		account = accountName;
    	}

  9. Add the following code for the updateAccount method.
    public Integer updateAccount(Boolean performCredit, Integer amount) {
    		int balance = 0;
    		if ("chequing".equals(account)) {
    			int c_balance = 1000;
    			if (performCredit.booleanValue())
    				balance = c_balance + amount.intValue();
    			else
    				balance = c_balance - amount.intValue();
    		} else if ("savings".equals(account)) {
    			int c_balance = 5000;
    			if (performCredit.booleanValue())
    				balance = c_balance + amount.intValue();
    			else
    				balance = c_balance - amount.intValue();
    		} 		
    		return new Integer(balance);
    	} 
  10. Save your implementation. From the menu, select File > Save. Return to the assembly editor. Export the Java component with a JMS binding. Select the component and from the menu, select Export > JMS Binding. The JMS Export binding selection window box opens. Change the Select how data is serialized between Business Object and JMS Message field to Object. Click OK.

  11. The AccountInfoExport component is created. Save your work.

  12. In the Business Integration navigation, copy CredentialsBO and Account to the JMSImport module. Open the assembly editor of the JMSImport module. Right-click module name and from the menu select Open. Drag the AccountInfoExport component from the JMSExport module navigation to the assembly editor canvas of the JMSImport module. A Manage Project Dependencies message opens. Click OK. An import component is created with a JMS binding.

  13. Select the component and in the properties view select the Binding tab. The JMS binding details open.

  14. The Java code in the example is provided as part of the steps as you go through it. To test, create a stand-alone reference in the JMSExport module and connect it to the Import1 import component. Stand-alone references are used to represent components outside the Service Component Architecture such as JSPs. Save your changes.

  15. Create a Dynamic Web Project called TestImportClient, but do not associate an EAR project with it. (Clear Add module to an EAR project.)

  16. In the JMSImport module, open the dependency editor. Right-click the module and from the menu select Open Dependency Editor. In the J2EE section, click Add and add the TestImportClient project. Save your changes and close the editor.

  17. Create a JSP in the TestImportClient project using the following code:
    <P>Loggin in:
    <BR>
    <%
    	com.ibm.websphere.sca.ServiceManager serviceManager = new com.ibm.websphere.sca.ServiceManager();
    	com.ibm.websphere.sca.Service service = (com.ibm.websphere.sca.Service) serviceManager.locateService("AccountPartner");
    	com.ibm.websphere.sca.scdl.Reference reference = service.getReference();
    	com.ibm.websphere.sca.sdo.DataFactory dataFactory = com.ibm.websphere.sca.sdo.DataFactory.INSTANCE;
    	com.ibm.websphere.bo.BOFactory boFactory = (com.ibm.websphere.bo.BOFactory)serviceManager.locateService("com/ibm/websphere/bo/BOFactory");
    
    	String login = null;
    	try {
    		commonj.sdo.DataObject credentialsBO = boFactory.create("http://JMSInbound", "CredentialsBO");
    		credentialsBO.setString("userid", "abcdXYZZ");
    		 credentialsBO.setString("password", "1234");
    		
    		login = (String) service.invoke("loginAccount", credentialsBO);
    	} catch (com.ibm.websphere.sca.ServiceBusinessException e) {
    		Object error = e.getData();
    	} 
     %>
    The login result is <%=login%> <BR>
    
    <BR>
    Changing account to chequing <BR>
    <%
    		 com.ibm.websphere.sca.scdl.OperationType selectAccountOp = reference.getOperationType("selectAccount");
    		 service.invokeAsync(selectAccountOp, "chequing");
    %>
    
    Performing credit of 100 <BR>
    
    <%
    	com.ibm.websphere.sca.scdl.OperationType updateAccountOp = null;
    	commonj.sdo.Type updateAccountOpInput = null;
    	commonj.sdo.DataObject transaction = null;
    	Integer balance = null;
    	try {
    		updateAccountOp = reference.getOperationType("updateAccount");
    		updateAccountOpInput = updateAccountOp.getInputType();
    		transaction = dataFactory.create(updateAccountOpInput);
    		transaction.set(0, Boolean.TRUE);
    		transaction.set(1, new Integer(100));
    		
    		balance = (Integer) service.invoke("updateAccount", transaction);
    		%>
    		The new balance is: <%= balance%> <BR>
    		<%
    	} catch (com.ibm.websphere.sca.ServiceBusinessException e) {
    		commonj.sdo.DataObject error = (commonj.sdo.DataObject)e.getData();
    		%>
    		Error: <%= error.get("account")%> amount: <%= error.get("balance")%> <BR>
    		<%
    	}	
    %>
    
    <BR>
    
    <BR>
    Changing account to savings <BR>
    <%
    		 service.invokeAsync(selectAccountOp, "savings");
    %>
    
    Performing debit of 500 <BR>
    <%
    try {
    		transaction.set(0, Boolean.FALSE);
    		transaction.set(1, new Integer(500));
    		
    		balance = (Integer) service.invoke("updateAccount", transaction);
    		%>
    		The new balance is: <%= balance%> <BR>
    		<%
    	} catch (com.ibm.websphere.sca.ServiceBusinessException e) {
    		commonj.sdo.DataObject error = (commonj.sdo.DataObject)e.getData();
    		%>
    		Error: <%= error.get("account")%> amount: <%= error.get("balance")%> <BR>
    		<%
    	}	
    %>
    <BR>
    
    </P>

  18. Add all your projects to the server. Right-click the server in the servers view and from the menu select Add and remove projects. Add your projects.
  19. With the server running, select the JSP and run it. In IBM Integration Designer web browser, enter the following URL: http://localhost:9080/TestImportClient/<JSPName>. It will update the balance of an account.


What to do next

In this example, an import would be created that has JNDI references to the destinations that are referenced by the export. It is therefore critical that on deployment of this type of application that you deploy the module containing the export (that is, the module defined with a service type of inbound) first followed by the module containing the import (that is, the module defined with a service type of outbound). If you were to deploy in the reverse order, you would receive an exception at run time.

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 a JMS client to receive messages from a JMS import
Create a user-defined JMS data binding