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

📄 jms102example.java

📁 全面解析jms源码
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -