Inserting Java code to send a message to a JMS queue listener
Before you begin
About this task
The inserted Java code sends a message to the JMS queue listener using the queue connection factory reference and the queue name reference that you provide. You can update the value of the parameter in the setText() method to change the text of the message.
Example snippet insertion for sending a message to a JMS queue listener
Before snippet insertion:
public class Main {
public void sendMyMessage() {
// insert snippet here
}
}
After snippet insertion, with cursor placed inside
sendMyMessage method:
import com.ibm.etools.service.locator.ServiceLocatorManager;
import javax.jms.*;
import javax.naming.*;
public class Main {
private final static String STATIC_CONNECTION_FACTORY_REF_NAME = "queueconnectionfactoryreference";
private final static String STATIC_QUEUE_REF_NAME = "queuename";
public void sendMyMessage() {
// insert snippet here
send_queuenameMessage();
}
protected void send_queuenameMessage() {
try {
QueueConnectionFactory qConnectionFactory = ServiceLocatorManager
.lookupQueueConnectionFactory(STATIC_CONNECTION_FACTORY_REF_NAME);
Queue queue = ServiceLocatorManager
.lookupQueue(STATIC_QUEUE_REF_NAME);
QueueConnection qConnection = qConnectionFactory
.createQueueConnection();
QueueSession qSession = qConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
QueueSender sender = qSession.createSender(queue);
TextMessage message = qSession.createTextMessage();
message.setText("Foo Sample Queue message");
sender.send(message);
sender.close();
qSession.close();
qConnection.close();
} catch (JMSException jmse) {
// TODO Auto-generated catch block
jmse.printStackTrace();
}
}
}
Procedure
- In the Java EE perspective, open in the Java editor the Java file where you want to add the code snippet and place your cursor in the point of the Java file where you want to insert the code.
- In the Snippets view, expand the EJB drawer and double-click Send a Message to JMS Queue Listener. The Insert Message-Driven Bean Queue Type Service wizard opens.
- Select an EJB reference for the message and click Next.
- Enter the Provider URL and Name Service Type to locate the resource references for the queue connection factory and queue name. If the client and listener are in the same application server container, you can select Use default context properties for doing a lookup on this reference.
- Click Next.
- Select the resource reference for the queue connection factory. If you have not yet created the reference, click New Queue Connection Factory Reference. The Add Reference wizard opens where you can define your javax.jms.QueueConnectionFactory resource reference.
- Click Finish to exit the Add Reference wizard and then click Next.
- Select the resource reference for the queue name. If you have not yet created the reference, click New Queue Name Reference. The Add Reference wizard opens where you can define your javax.jms.Queue resource reference.
- Click Finish.
Results
Note: A serviceLocatorMgr.jar file
is added as a utility JAR file to each enterprise application that
the Java class you edited belongs
to. This serviceLocator.jar file includes a ServiceLocatorManager
class that is used within the inserted snippets of Java code. This class optimizes the lookups
of the home interfaces and InitialContexts, and ensures that they
are only looked up once for the entire application. Because the utility
JAR file is added, a Java JAR
dependency for the serviceLocator.jar file is added for the module
or Java utility project that
the Java file belongs to.
The ServiceLocatorManager class has a static method called setErrorHandler(ServiceLocatorErrorHandler handler) that you can use to state a specific error handler for error conditions that occur when looking up the home interface. The default handler simply calls printStackTrace() on the exception that is handled.