jms102example.java

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

JAVA
84
字号
package jms102;

import javax.naming.*;  // The JNDI classes         
import javax.jms.*;     // The JMS 1.02 classes

public class JMS102Example {

    public QueueSender getQueueSender(String queueConnectionFactoryName, String queueName)
        throws NamingException, JMSException {

        // Get the specified connection factory and queue
        Context jndiContext = new InitialContext();
        QueueConnectionFactory factory = (QueueConnectionFactory)
            jndiContext.lookup(queueConnectionFactoryName);
        Queue queue = (Queue) jndiContext.lookup(queueName);

        // Create the connection and session
        QueueConnection connection = factory.createQueueConnection();
        QueueSession session = connection.createQueueSession(false,
            Session.AUTO_ACKNOWLEDGE);

        // Use the session and queue to create the sender
        QueueSender sender = session.createSender(queue);
        return sender;
    }

    public TopicPublisher getTopicPublisher(String topicConnectionFactoryName, String topicName)
        throws NamingException, JMSException {

        // Get the specified connection factory and queue
        Context jndiContext = new InitialContext();
        TopicConnectionFactory factory = (TopicConnectionFactory)
            jndiContext.lookup(topicConnectionFactoryName);
        Topic topic = (Topic) jndiContext.lookup(topicName);

        // Create the connection and session
        TopicConnection connection = factory.createTopicConnection();
        TopicSession session = connection.createTopicSession(false,
            Session.AUTO_ACKNOWLEDGE);

        // Use the session and topic to create the publisher
        TopicPublisher publisher = session.createPublisher(topic);
        return publisher;
    }

    public QueueReceiver getQueueReceiver(String queueConnectionFactoryName, String queueName)
        throws NamingException, JMSException {

        // Get the specified connection factory and queue
        Context jndiContext = new InitialContext();
        QueueConnectionFactory factory = (QueueConnectionFactory)
            jndiContext.lookup(queueConnectionFactoryName);
        Queue queue = (Queue) jndiContext.lookup(queueName);

        // Create the connection and session
        QueueConnection connection = factory.createQueueConnection();
        QueueSession session = connection.createQueueSession(false,
            Session.AUTO_ACKNOWLEDGE);

        // Use the session and queue to create the receiver
        QueueReceiver receiver = session.createReceiver(queue);
        return receiver;
    }

    public TopicSubscriber getTopicSubscriber(String topicConnectionFactoryName, String topicName)
        throws NamingException, JMSException {

        // Get the specified connection factory and queue
        Context jndiContext = new InitialContext();
        TopicConnectionFactory factory = (TopicConnectionFactory)
            jndiContext.lookup(topicConnectionFactoryName);
        Topic topic = (Topic) jndiContext.lookup(topicName);

        // Create the connection and session
        TopicConnection connection = factory.createTopicConnection();
        TopicSession session = connection.createTopicSession(false,
            Session.AUTO_ACKNOWLEDGE);

        // Use the session and topic to create the subscriber
        TopicSubscriber subscriber = session.createSubscriber(topic);
        return subscriber;
    }
}

⌨️ 快捷键说明

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