jms11example.java

来自「全面解析jms源码」· Java 代码 · 共 46 行

JAVA
46
字号
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;
    }
}

⌨️ 快捷键说明

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