IBM®
跳转到主要内容
    中国 [选择]    使用条款
 
 
Select a scope: Search for:    
    首页    产品    服务与解决方案     支持与下载    个性化服务    
跳转到主要内容

developerWorks 中国  >  Java technology  >

JMS 1.1 simplifies messaging with unified domains

Learn how the new API will help you write more reusable JMS clients

developerWorks

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

    关于 IBM 隐私条约 联系 IBM 使用条款