zoukankan      html  css  js  c++  java
  • 通过JBOSS服务器来实现JMS消息传送

    首先必须启动JBOSS服务器,以便于充当JMS传递消息的中间键;

    JBOSS消息发送端:

    package test;
    
    
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.TimeUnit;
    import java.util.logging.Logger;
    import java.util.Properties;
    
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.Destination;
    import javax.jms.MessageProducer;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    
    /**
     * <p>Description:JMS客户端消息生产者 </p>
     */
    public class JMSProducer {
        private static final Logger log = Logger.getLogger(JMSProducer.class.getName());
    
        private static final String DEFAULT_MESSAGE = "the 4 message!";
        // xml文件272行
        private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
        // xml文件293行,初次找JBOSS的JNDI太不容易了
        private static final String DEFAULT_DESTINATION = "jms/queue/test";
        private static final String DEFAULT_MESSAGE_COUNT = "1";
    
        private static final String DEFAULT_USERNAME = "admin";
        private static final String DEFAULT_PASSWORD = "123456";
        private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
        //private static final String INITIAL_CONTEXT_FACTORY = "org.jnp.interfaces.NamingContextFactory";
        private static final String PROVIDER_URL = "remote://localhost:4447";
    
        public static void main(String[] args) throws Exception {
            Context context=null;
            Connection connection=null;
            try {
                // 设置上下文的JNDI查找
                log.info("设置JNDI访问环境信息也就是设置应用服务器的上下文信息!");
                final Properties env = new Properties();
                env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);// 该KEY的值为初始化Context的工厂类,JNDI驱动的类名
                env.put(Context.PROVIDER_URL,  PROVIDER_URL);// 该KEY的值为Context服务提供者的URL.命名服务提供者的URL
                
                env.put(Context.SECURITY_PRINCIPAL, DEFAULT_USERNAME);
                env.put(Context.SECURITY_CREDENTIALS, DEFAULT_PASSWORD);//应用用户的登录名,密码.
                // 获取到InitialContext对象.
                context = new InitialContext(env);
                log.info("初始化上下文,'JNDI驱动类名','服务提供者URL','应用用户的账户','密码'完毕.");
                log.info("获取连接工厂!");
                ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup(DEFAULT_CONNECTION_FACTORY);
                log.info("获取目的地!");
                Destination destination = (Destination) context.lookup(DEFAULT_DESTINATION);
    
                // 创建JMS连接、会话、生产者和消费者
                connection = connectionFactory.createConnection(DEFAULT_USERNAME, DEFAULT_PASSWORD);
                log.info("开启JMS工厂模式");
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                log.info("获取操作连接");
                MessageProducer producer = session.createProducer(destination);
                connection.start();
                log.info("启动工程模式与连接");
                int count = Integer.parseInt(DEFAULT_MESSAGE_COUNT);
                // 发送特定数目的消息
                TextMessage message = null;
                for (int i = 0; i < count; i++) {
                    message = session.createTextMessage(DEFAULT_MESSAGE);
                    producer.send(message);
                    log.info("message:"+message);
                    log.info("message:"+DEFAULT_MESSAGE);
                }
                // 等待30秒退出
                CountDownLatch latch = new CountDownLatch(1);
                latch.await(30, TimeUnit.SECONDS);
                
            } catch (Exception e) {
                log.severe(e.getMessage());
                throw e;
            } finally {
                if (context != null) {
                    context.close();
                }
                // 关闭连接负责会话,生产商和消费者
                if (connection != null) {
                    connection.close();
                }
            }
        }
    }

    JMS消息接收端:

    package test;
    import java.util.Properties;
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.TimeUnit;
    import java.util.logging.Logger;
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.Destination;
    import javax.jms.MessageConsumer;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    
    public class JMSConsumer {
        private static final Logger log = Logger.getLogger(JMSConsumer.class.getName());
        private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
        private static final String DEFAULT_DESTINATION = "jms/queue/test";
        private static final String DEFAULT_USERNAME = "admin";
        private static final String DEFAULT_PASSWORD = "123456";
        private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
        private static final String PROVIDER_URL = "remote://localhost:4447";
    
        public static void main(String[] args) throws Exception {
    
            ConnectionFactory connectionFactory = null;
            Connection connection = null;
            Session session = null;
            MessageConsumer consumer = null;
            Destination destination = null;
            TextMessage message = null;
            Context context = null;
            try {
                final Properties env = new Properties();
                env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
                env.put(Context.PROVIDER_URL, PROVIDER_URL);
                env.put(Context.SECURITY_PRINCIPAL, DEFAULT_USERNAME);
                env.put(Context.SECURITY_CREDENTIALS, DEFAULT_PASSWORD);
                context = new InitialContext(env);
                connectionFactory = (ConnectionFactory) context.lookup(DEFAULT_CONNECTION_FACTORY);
                destination = (Destination) context.lookup(DEFAULT_DESTINATION);
                connection = connectionFactory.createConnection(DEFAULT_USERNAME, DEFAULT_PASSWORD);
                session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                consumer = session.createConsumer(destination);
                connection.start();
    
                // 等待30秒退出
                CountDownLatch latch = new CountDownLatch(1);
                while (message == null) {
                    log.info("开始从JBOSS端接收信息-----");
                    message = (TextMessage) consumer.receive(5000);
                    latch.await(1, TimeUnit.SECONDS);
                }
                log.info("接收到的消息的内容:" + message.getText());
            } catch (Exception e) {
                log.severe(e.getMessage());
                throw e;
            } finally {
                if (context != null) {
                    context.close();
                }
                if (connection != null) {
                    connection.close();
                }
            }
        }
    }
    View Code
  • 相关阅读:
    Android 智能问答机器人的实现
    hadoop分布式安装部署具体视频教程(网盘附配好环境的CentOS虚拟机文件/hadoop配置文件)
    淘宝数据库OceanBase SQL编译器部分 源代码阅读--生成物理查询计划
    linux启动基本流程
    C#开发Unity游戏教程之Scene视图与脚本的使用
    软件项目量化管理(CMMI高成熟度)实践经验谈——之项目管理过程监督与控制篇
    Servlet+JSP 原理
    2 会计要素和会计科目
    Android系统优化
    [Swift]LeetCode259.三数之和较小值 $ 3Sum Smaller
  • 原文地址:https://www.cnblogs.com/feitianshaoxai/p/6559432.html
Copyright © 2011-2022 走看看