Sample ApplicationSection overview You should now have a fairly comprehensive understanding of how JCA's components work together to create and manage complex interactions with an enterprise system. All that's left to do is play with the code itself. You will find a sample application that uses the sample resource adapter in the file helloworldra.ear. It is a J2EE Web application with an input HTML form (with only a Submit button), a servlet controller, a JavaBeans component that invokes the resource adapter, and a JavaServer Pages (JSP) component to display the results of the invocation. We'll close this tutorial with a look at the bean that invokes the resource adapter, as well as another class (provided separately from the .ear file) that helps deploy an InteractionSpec instance into JNDI. HelloWorldBean class This HelloWorldBean class, through its execute() method, invokes the resource adapter, using the CCI interface. First, the ConnectionFactory is retrieved from JNDI and is used to create a RecordFactory. The RecordFactory is used to create an input record and an output record. Next, an InteractionSpec is retrieved from JNDI. Then a Connection is created from the ConnectionFactory and an Interaction is created from the Connection. The Interaction is used to execute the function, and the results are stored in the bean's Message property. You will observe that the two JNDI lookups are different. The lookup of the ConnectionFactory is done using the java:comp/env context, while the lookup of the InteractionSpec is not. This is because the environment that was used to develop and test the resource adapter and sample application did not have a deployment tool to bind an InteractionSpec into JNDI; it only provided for binding the ConnectionFactory, which could then be accessed using a resource reference in the Web application.
...
public void execute() throws NamingException, ResourceException { InitialContext context = new InitialContext(); ConnectionFactory cxFactory = (ConnectionFactory) context.lookup("java:comp/env/HelloWorld"); RecordFactory recordFactory = cxFactory.getRecordFactory(); IndexedRecord input = recordFactory.createIndexedRecord(HelloWorldIndexedRecord.INPUT); IndexedRecord output = recordFactory.createIndexedRecord(HelloWorldIndexedRecord.OUTPUT); InteractionSpec ispec = (InteractionSpec) context.lookup("jca/HelloWorldISpec"); Connection connection = cxFactory.getConnection(); Interaction interaction = connection.createInteraction(); interaction.execute(ispec, input, output); message = (String) output.get(HelloWorldIndexedRecord.MESSAGE_FIELD); interaction.close(); connection.close(); } ... DeployISpec class The DeployISpec class, which is provided in the helloworldradeploy.jar file, is used to deploy the InteractionSpec object into JNDI if your deployment environment does not provide a tool to accomplish that. It has a simple main() method that creates the InteractionSpec and binds it into JNDI. This class should be run after the resource adapter is deployed and the connection factory has been bound into JNDI.
...
public static void main(String[] args) throws NamingException { Properties properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY); InitialContext context = new InitialContext(properties); HelloWorldInteractionSpecImpl ispec = new HelloWorldInteractionSpecImpl(); ispec.setFunctionName(HelloWorldInteractionSpec.SAY_HELLO_FUNCTION); context.bind("jca/HelloWorldISpec", ispec); } ... Running the code After deploying the resource adapter into your application server environment, create a connection factory with the JNDI name of jca/HelloWorld. Then run the DeployISpec class to bind the InteractionSpec into JNDI under the name jca/HelloWorldISpec. After deploying the Web application, you should pull up a browser and load the URL http://SERVER_NAME/hello. Once the page has loaded, click the Submit button and the results page with the "Hello World!" message should appear. The sample resource adapter and sample application were successfully deployed and tested on IBM WebSphere Application Server Advanced Edition Version 4.03. |