美文网首页Java学习笔记
HornetQ版的Hello World简单例子

HornetQ版的Hello World简单例子

作者: lynch0571 | 来源:发表于2017-03-02 13:30 被阅读45次

    1、Maven依赖

            <dependency>
                <groupId>org.hornetq</groupId>
                <artifactId>hornetq-jms-client</artifactId>
                <version>2.4.7.Final</version>
            </dependency>
    
            <dependency>
                <groupId>jboss</groupId>
                <artifactId>jnp-client</artifactId>
                <version>4.2.2.GA</version>
            </dependency>
    

    2、代码

    package com.lynch.hornetq.demo;
    
    import java.util.Properties;
    
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.JMSException;
    import javax.jms.MessageConsumer;
    import javax.jms.MessageProducer;
    import javax.jms.Queue;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    
    public class HornetqTextMessageDemo {
    
        public static void main(String[] args) {
            // 设置HornetQ连接信息
            Properties p = new Properties();
            p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
            p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
            p.put(Context.PROVIDER_URL, "jnp://192.168.137.239:1099");
    
            Context context = null;
            ConnectionFactory factory = null;
            Queue queue = null;
    
            // 初始化连接信息,并指定连接工厂和消息队列
            try {
                context = new InitialContext(p);
                factory = (ConnectionFactory) context.lookup("/ConnectionFactory");
                queue = (Queue) context.lookup("queue/DLQ");
            } catch (NamingException e) {
                e.printStackTrace();
            }
    
            // 建立连接和会话,进而发送和接收消息
            try (Connection connection = factory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);) {
                connection.start();
                produceMessage(session, queue, "Hello world!");
                consumeMessage(session, queue);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
        public static void produceMessage(Session session, Queue queue, String text) {
            try (MessageProducer producer = session.createProducer(queue)) {
                TextMessage message = session.createTextMessage(text);
                producer.send(message);
                System.out.println("Send message:" + text);
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    
        public static void consumeMessage(Session session, Queue queue) {
            try (MessageConsumer consumer = session.createConsumer(queue)) {
                TextMessage message = (TextMessage) consumer.receive(5000);
                System.out.println("Received message:" + message.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    
    }
    
    

    相关文章

      网友评论

        本文标题:HornetQ版的Hello World简单例子

        本文链接:https://www.haomeiwen.com/subject/kjmcgttx.html