The application client model enables you to access enterprise beans and other resources (for example, JMS or database) from an application client. To use these resources (that is, to be able to invoke their methods), you must first obtain a reference to them. The resource and enterprise bean references are bound in the JNDI namespace and are obtained by performing a lookup operation in a relevant location in the naming.
To look up an enterprise bean or other resource reference (JMS, JDBC), use the following names:
· Enterprise beans – you can use the JNDI name (the name used for looking up the bean from the naming) or an arbitrary name. Enter the name in the <ejb-ref-name> element of application-client.xml and appclient-j2ee-engin.xml. If you choose an arbitrary name, you must specify the JNDI name of the bean in the <jndi-name> element in appclient-j2ee-engine.xml.
· JMS administered object (Destination) – you can use JMS Destination by the name that you specify when you create it or by an arbitrary name. Enter the name in the <resource-env-ref-name> element of application-client.xml and appclient-j2ee-engine.xml. If you choose an arbitrary name, you must specify the JNDI name of the object in the <jndi-name> sub-element of <resource-env-ref> element in appclient-j2ee-engine.xml.
· DataSource and JMS ConnectionFactory – you can use the name that you have specified when creating the object or an arbitrary name. Enter the name in the <res-ref-name> element of application-client.xml and appclient-j2ee-engine.xml. If you choose an arbitrary name, you must specify the JNDI name of the object in the <res-link> sub-element of <resource-ref> element in appclient-j2ee-engine.xml.
...
1. Get InitialContext. In this case you must declare the new InitialContext without any parameters.
The following code illustrates how you obtain the InitialContext:
try { context = new InitialContext(); } catch (NamingException ne) { System.out.println( "Could not obtain naming: " + ne.getMessage()); return ; } |
2. Lookup the resource you need using the InitialContext you have obtained:
i. To look up the enterprise bean use java:comp/env as prefix and the bean’s reference name declared in the deployment descriptor for the bean. You must also invoke the create() method of the bean’s home interface to get an instance of the enterprise bean:
TheBeanHome beanHome = (AppClientHome) context.lookup("java:comp/env/ejb/<ejb-ref-name>"); TheBean bean = beanHome.create(); |
ii. You can look up JMS ConnectionFactory and Destination objects.
The following code illustrates how to lookup a Queue- or a TopicConnectionFactory:
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) ctx.lookup("java:comp/env/<res-ref-name>");
TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) ctx.lookup("java:comp/env/<res-ref-name"); |
To look up a Queue or a Topic destination, use the following model:
}Topic theTopic = (Topic) ctx.lookup("java:comp/env/<resource-env-ref-name");
Queue theQueue = (Queue) ctx.lookup("java:comp/env/<resource-env-ref-name"); |
iii. You can lookup DataSource objects from your application client. DataSource objects provide connections to relational databases.
The following code illustrates how to lookup a DataSource object:
DataSource ds = (DataSource) ctx.lookup("java:comp/env/<res-ref-name>"); |
You can invoke methods on the referenced enterprise bean, JMS or DataSource resource.
See also: