跳转到主要内容

单击提交则表示您同意developerWorks 的条款和条件。 查看条款和条件.

当您初次登录到 developerWorks 时,将会为您创建一份概要信息。您在 developerWorks 概要信息中选择公开的信息将公开显示给其他人,但您可以随时修改这些信息的显示状态。您的姓名(除非选择隐藏)和昵称将和您在 developerWorks 发布的内容一同显示。

所有提交的信息确保安全。

  • 关闭 [x]

当您初次登录到 developerWorks 时,将会为您创建一份概要信息,您需要指定一个昵称。您的昵称将和您在 developerWorks 发布的内容显示在一起。

昵称长度在 3 至 31 个字符之间。 您的昵称在 developerWorks 社区中必须是唯一的,并且出于隐私保护的原因,不能是您的电子邮件地址。

单击提交则表示您同意developerWorks 的条款和条件。 查看条款和条件.

所有提交的信息确保安全。

  • 关闭 [x]

JMS 1.1 simplifies messaging with unified domains

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

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