zoukankan      html  css  js  c++  java
  • ActiveMQ实现点对点通讯模式

    ActiveMQ默认启动到8161端口,启动完了后在浏览器地址栏输入:http://localhost:8161/admin要求输入用户名密码,默认用户名密码为admin、admin,

    这个用户名密码是在conf/users.properties中配置的。输入用户名密码后便可看到的ActiveMQ控制台界面了

    引入pom文件依赖 
     <dependencies>
            <dependency>
                <groupId>org.apache.activemq</groupId>
                <artifactId>activemq-core</artifactId>
                <version>5.7.0</version>
            </dependency>
        </dependencies>

    创建生产者

    public class Producter {
        public static void main(String[] args) throws JMSException {
            // ConnectionFactory :连接工厂,JMS 用它创建连接
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
                    ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
            // JMS 客户端到JMS Provider 的连接
            Connection connection = connectionFactory.createConnection();
            connection.start();
            // Session: 一个发送或接收消息的线程
            Session session = connection.createSession(Boolean.falst, Session.AUTO_ACKNOWLEDGE);
            // Destination :消息的目的地;消息发送给谁.
            // 获取session注意参数值my-queue是Query的名字
            Destination destination = session.createQueue("my-queue");
            // MessageProducer:消息生产者
            MessageProducer producer = session.createProducer(destination);
            // 设置不持久化
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            // 发送一条消息
            for (int i = 1; i <= 5; i++) {
                sendMsg(session, producer, i);
            }
            connection.close();
        }
        /**
         * 在指定的会话上,通过指定的消息生产者发出一条消息
         * 
         * @param session
         *            消息会话
         * @param producer
         *            消息生产者
         */
        public static void sendMsg(Session session, MessageProducer producer, int i) throws JMSException {
            // 创建一条文本消息
            TextMessage message = session.createTextMessage("Hello ActiveMQ!" + i);
            // 通过消息生产者发出消息
            producer.send(message);
        }
    }

    创建消费者

    public class JmsReceiver {
        public static void main(String[] args) throws JMSException {
            // ConnectionFactory :连接工厂,JMS 用它创建连接
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
                    ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
            // JMS 客户端到JMS Provider 的连接
            Connection connection = connectionFactory.createConnection();
            connection.start();
            // Session: 一个发送或接收消息的线程
            Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            // Destination :消息的目的地;消息发送给谁.
            // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
            Destination destination = session.createQueue("my-queue");
            // 消费者,消息接收者
            MessageConsumer consumer = session.createConsumer(destination);
            while (true) {
                TextMessage message = (TextMessage) consumer.receive();
                if (null != message) {
                    System.out.println("收到消息:" + message.getText());
                } else
                    break;
            }
            session.close();
            connection.close();
        }
    }

    需要ActiveMQ的安装包的,可以加我QQ:3385351592

  • 相关阅读:
    SVN被锁定解决办法
    onchange监听input值变化及input隐藏后change事件不触发的原因与解决方法(设置readonly后onchange不起作用的解决方案)
    button的格式的问题
    javaScript年份下拉列表框内容为当前年份及前后50年
    ORACLE导入、导出所有数据到文件的SQL语句
    Oracle存储过程学习笔记
    SQlServer的日期相减(间隔)datediff函数
    td中嵌套table,让table完全填充父元素td
    Cause: org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: java.sql.SQLException: 不支持的特性
    HTML认知
  • 原文地址:https://www.cnblogs.com/nancheng/p/9339936.html
Copyright © 2011-2022 走看看