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

📄 jms11example.java

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