ActiveMQ简单使用

Maven
发送消息

public static void main(String[] args) throws Exception {
        ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616"); 
        Connection conn = null;
        Session session = null;
        MessageProducer producer = null;
        try {
            conn = cf.createConnection();
            conn.start();
            session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue("msg");
            producer = session.createProducer(destination);
            TextMessage message = session.createTextMessage();
            message.setText("你好!我是生产者");
            producer.send(message);         
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            try {
                if(producer != null) {
                    producer.close();
                }
                if(session != null) {
                    session.close();
                }
                if(conn != null) {
                    conn.close();
                }
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }

    }

接收消息

public static void main(String[] args) throws Exception {
        ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616"); 
        Connection conn = null;
        Session session = null;
        MessageConsumer consumer = null;
        try {
            conn = cf.createConnection();
            conn.start();
            session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue("msg");
            consumer = session.createConsumer(destination);
            
            while(true) {
                Message message = consumer.receive();
                if(message!=null) {
                    TextMessage textMessage = (TextMessage) message;
                    System.out.println("消费者获取到消息: " + textMessage.getText());
                } else {
                    break;
                }       
            }
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            try {
                if(consumer != null) {
                    consumer.close();
                }
                if(session != null) {
                    session.close();
                }
                if(conn != null) {
                    conn.close();
                }
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }

    }

问题:
1如何取消息?
11轮询polling
12消息驱动bean、消息驱动POJO
2ActiveMQ放在哪里?也就是消息发送到哪里?
3RPC?


Spring
Maven+Spring
Multiple annotations found at this line:?


    org.springframework
    spring-context
    4.3.25.RELEASE

如何添加amq命名空间?
beans

//默认命名空间
xmlns="http://www.springframework.org/schema/beans"
//声明命名空间
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:c="http://www.springframework.org/schema/c"
//XSD
xsi:schemaLocation="http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.2.xsd
        http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

1、声明命名空间+配置XSD版本
2、activemq-corexbean-springspring-jms[pom.xml]
如何写测试类?
https://baijiahao.baidu.com/s?id=1552330198766572&wfr=spider&for=pc
https://www.javaroad.cn/articles/1105
关闭ApplicationContext对象?
https://www.cnblogs.com/zhangyuanbo/p/11224039.html

你可能感兴趣的:(ActiveMQ简单使用)