Maven
發(fā)送消息
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("你好!我是生產(chǎn)者");
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("消費(fèi)者獲取到消息: " + 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消息驅(qū)動(dòng)bean篷就、消息驅(qū)動(dòng)POJO
2ActiveMQ放在哪里?也就是消息發(fā)送到哪里腐巢?
3RPC品追?
Spring
Maven+Spring
Multiple annotations found at this line:?
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.25.RELEASE</version>
</dependency>
如何添加amq命名空間冯丙?
beans
//默認(rèn)命名空間
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-core胃惜、xbean-spring泞莉、spring-jms[pom.xml]
如何寫測試類?
https://baijiahao.baidu.com/s?id=1552330198766572&wfr=spider&for=pc
https://www.javaroad.cn/articles/1105
關(guān)閉ApplicationContext對象船殉?
https://www.cnblogs.com/zhangyuanbo/p/11224039.html