public class ConsumerListener {//默认连接用户名
    private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
    //默认连接密码
    private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
    //默认连接地址
    private static final String BROKER_URL = ActiveMQConnection.DEFAULT_BROKER_URL;

    public static void main(String[] args) {
        //连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKER_URL);
        try {
            //连接
            Connection connection = connectionFactory.createConnection();
            //启动连接
            connection.start();
            //创建session
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            //消息目的地
            Destination destination = session.createQueue("短信发送");
            //消息消费者
            MessageConsumer consumer = session.createConsumer(destination);
//            while (true) {
//                TextMessage message = (TextMessage) consumer.receive();
//                if (message != null) {
//                    System.out.println("接收到消息: " + message.getText());
//                } else {
//                    break;
//                }
//            }
            //使用内部类为消息接收者加载相应的Listener监听
            consumer.setMessageListener(new MessageListener() {
                //重写onMessage方法
                public void onMessage(Message msg) {
                    if (msg != null) {
                        TextMessage textMessage = (TextMessage) msg;
                        try {
                            System.out.println("接收#" + textMessage.getText());
                        } catch (JMSException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
//            session.close();
//            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

ActiveMQ 点对点-使用监听器接收消息
ActiveMQ 点对点-使用监听器接收消息

相关文章:

  • 2021-04-27
  • 2022-01-01
  • 2022-12-23
  • 2021-07-29
  • 2021-11-09
  • 2021-06-01
  • 2021-12-05
  • 2021-12-03
猜你喜欢
  • 2022-12-23
  • 2021-09-16
  • 2021-12-18
  • 2022-12-23
  • 2022-12-23
  • 2021-05-18
  • 2022-12-23
相关资源
相似解决方案