A special definition file is not required when creating a client that invokes a session bean. In the Java class (servlet or similar) that is used as the client, code the processing that gets the session bean business interface and the processing that uses the interface to invoke the business method.
Creating the Business Interface
The business interface can be fetched easily using the Dependency Injection.
Use @EJB annotation as shown below to get the business interface.
Example of using EJB Annotation
@EJB private Calc calc; |
Coding Example | Explanation |
---|---|
ejb/Calc | Specify the JNDI name associated with the session bean. |
Calc | This is the business interface. (It is a local interface.) |
Calc#add(int, int) | This is the business method. |
Note
The Dependency Injection can be used only in Java EE components managed in Java EE containers, such as servlets, EJB, and so on. For other classes, use JNDI lookup to get objects.
For details, refer to "10.2.2 Using JNDI Lookup to Obtain Objects".
Invoking the Business Method
To invoke the business method, use the obtained business interface to invoke the method.
Example of Invoking a Business Method
int result = calc.add(100,200); |
A special definition file is not required when creating a client that sends messages to a message-driven bean. In the Java class (servlet or similar) that is used as the client, code the processing that fetches the message-driven bean JMS connection factory or similar and the processing that uses this to send messages.
The explanation below is for sending messages to a point-to-point model message-driven bean.
Fetching the JMS Connection Factory and the JMS Destination
The JMS connection factory and the JMS Destination can be fetched easily using a Dependency Injection.
Use an @Resource annotation such as the following to fetch the JMS connection factory and the JMS Destination.
Example of Using Resource Annotation
@Resource(name="jms/QueueCF001") private QueueConnectionFactory qcf; @Resource(name="jms/Queue") private Queue queue; |
Coding Example | Explanation |
---|---|
jms/QueueCF001 | JMS connection factory name |
jms/Queue | JMS Destination name |
Note
The Dependency Injection can be used only in Java EE components managed in Java EE containers, such as servlets, EJB, and so on. For other classes, use JNDI lookup to fetch objects.
For details, refer to "10.2.2 Using JNDI Lookup to Obtain Objects".
Sending Messages
After the JMS connection factory and the JMS Destination are fetched, the message sending procedure is the same as for up to EJB 2.1. An example is below.
Example of Sending a Message
// Create connection, session, and sender QueueConnection connection = qcf.createQueueConnection(); QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); QueueSender sender = session.createSender(queue); // Send a text message TextMessage msg = session.createTestMessage(); msg.setText("Hello World!"); sender.send(msg); // Close connection.close(); |