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 3. JMS11UnifiedTransactionExample.java
        
package jms11;
import javax.naming.*;  // The JNDI classes         
import javax.jms.*;     // The JMS 1.1 classes
public class JMS11UnifiedTransactionExample {
    /**
     * Transfer a message from a source destination
     * to a target destination. The destinations may
     * both be queues, both be topics, or may be from a
     * queue to a topic, or from a topic to a queue.
     */
    public void transferMessage(String connectionFactoryName, String sourceName, String targetName)
        throws NamingException, JMSException {
        // Get the specified connection factory and destinations
        Context jndiContext = new InitialContext();
        ConnectionFactory factory = (ConnectionFactory)
            jndiContext.lookup(connectionFactoryName);
        Destination source = (Destination) jndiContext.lookup(sourceName);
        Destination target = (Destination) jndiContext.lookup(targetName);
        // Create the connection and session
        Connection connection = factory.createConnection();
        Session session = connection.createSession(true,
            Session.AUTO_ACKNOWLEDGE);
        // Use the session and destinations to
        // create the consumer and producer
        MessageConsumer consumer = session.createConsumer(source);
        MessageProducer producer = session.createProducer(target);
        // Transfer the next message on the source 
        // destination to the target destination
        // in a single transaction
        try {
            producer.send(consumer.receive());
            session.commit();
        }
        catch (JMSException ex) {
            session.rollback();
        }
        // Release all resources
        producer.close();
        consumer.close();
        session.close();
        connection.close();
    }
}
      

Return to article

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