 |
Return to article
Listing 2. JMS11Example.java
package jms11;
import javax.naming.*; // The JNDI classes
import javax.jms.*; // The JMS 1.1 classes
public class JMS11Example {
public MessageProducer getProducer(String connectionFactoryName, String destinationName)
throws NamingException, JMSException {
// Get the specified connection factory and queue
Context jndiContext = new InitialContext();
ConnectionFactory factory = (ConnectionFactory)
jndiContext.lookup(connectionFactoryName);
Destination destination = (Destination) jndiContext.lookup(destinationName);
// Create the connection and session
Connection connection = factory.createConnection();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
// Use the session and destination to create the producer
MessageProducer producer = session.createProducer(destination);
return producer;
}
public MessageConsumer getConsumer(String connectionFactoryName, String destinationName)
throws NamingException, JMSException {
// Get the specified connection factory and queue
Context jndiContext = new InitialContext();
ConnectionFactory factory = (ConnectionFactory)
jndiContext.lookup(connectionFactoryName);
Destination destination = (Destination) jndiContext.lookup(destinationName);
// Create the connection and session
Connection connection = factory.createConnection();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
// Use the session and destination to create the consumer
MessageConsumer consumer = session.createConsumer(destination);
return consumer;
}
}
|
Return to article
|  |
|