⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jms11unifiedtransactionexample.java

📁 全面解析jms源码
💻 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();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -