package activemq.basic.demo;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* @Description TODO
* @author Cai
* @date 2018年1月8日 上午11:44:26
* @version V1.0
*/
public class QueueSender {
public static void main(String[] args) throws Exception {
// 连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.52.129:61616");
//创建连接
Connection connection = connectionFactory.createConnection();
connection.start();
//创建会话,携带事物,自动匹配消息
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// 创建消息目的地
Destination destination = session.createQueue("my-queue2");
//创建生产者
MessageProducer producer = session.createProducer(destination);
for (int i = 0; i < 3; i++) {
TextMessage message = session.createTextMessage("message--"+i);
Thread.sleep(1000);
producer.send(message);
}
session.commit();
session.close();
connection.close();
}
}
package activemq.basic.demo;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* @Description TODO
* @author Cai
* @date 2018年1月8日 下午12:47:46
* @version V1.0
*/
public class QueueReceiver {
/**
* 关于可靠性
* 1. 在事务性会话当中,session.commit()后,消息生产者即认为消息消费者已经接收该消息.
* 何为事务性,即在创建会话时,携带的参数(Boolean.true,).
*/
/**
* 关于可靠性
* 1. 在非事务性会话时,可靠性取决于该会话的应答模式. 应答模式以消费者设置为准
* 何为应答模式,即为消费者创建会话时,携带的参数(Boolean.false,Session.AUTO_ACKNOWLEDGE).. 生产者间会话的事物与消费者事物不在同一个事物内.
* 1. 模式一 Session.AUTO_ACKNOWLEDGE
* 当客户成功的从receive()返回,或者MessageListener.onMessage()方法成功返回时,会话自动确认收到消息
* 2. 模式二 Session.CLIENT_ACKNOWLEDGE
* 此时由消费者调用 acknowledge()方法确认.(此方法为确认该会话的所有消息) // message.acknowledge();
* 3. 模式三 Session.DUPS_OK_ACKNOWLEDGE
* 此模式 迟钝的确认消息的提交,容易造成数据的重复处理. 如果是此模式,呐消息生产者应该将消息头的JMSRedelivered字段设置为true
*/
public static void main(String[] args){
try {
// 连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.52.129:61616");
//创建连接
Connection connection = connectionFactory.createConnection();
connection.start();
//创建会话,携带事物,消息签收机制
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// 创建消息目的地
Destination destination = session.createQueue("my-queue2");
//创建生产者
MessageConsumer consumer = session.createConsumer(destination);
for (int i = 0; i < 3; i++) {
// 同步消费的方式,会形成阻塞
TextMessage message = (TextMessage) consumer.receive();
session.commit(); // 意思是确认消息,告知已接收该消息
/* if (i == 2) {
message.acknowledge();
}*/
System.out.println("接受消息为: "+message.getText());
}
session.close();
connection.close();
} catch (Exception e) {
System.out.println("....");
}
}
}
package activemq.basic.persistent.not;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* 非持久化pub/sbu 订阅/被订阅模式
* 消息生产者
* @author C
*/
public class QueueSender {
public static void main(String[] args) throws Exception{
// 连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.230.130:61616");
//创建连接
Connection connection = connectionFactory.createConnection();
connection.start();
//创建会话,携带事物,自动匹配消息
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// 创建消息目的地
Destination destination = session.createTopic("MyTopic");
//创建生产者
MessageProducer producer = session.createProducer(destination);
for (int i = 0; i < 3; i++) {
TextMessage message = session.createTextMessage("message--"+i);
Thread.sleep(1000);
producer.send(message);
}
session.commit();
session.close();
connection.close();
}
}
package activemq.basic.persistent.not;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class QueueReceiver {
/**
* 非持久化topic消息消费者,需要保持该消费者处于激活状态,才能接收到来自被订阅者的消息
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception{
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.230.130:61616");
//创建连接
Connection connection = connectionFactory.createConnection();
connection.start();
//创建会话,携带事物,消息签收机制
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// 创建消息目的地
Destination destination = session.createTopic("MyTopic");
//创建生产者
MessageConsumer consumer = session.createConsumer(destination);
// 同步消费的方式,会形成阻塞
TextMessage message = (TextMessage) consumer.receive();
while(message != null) {
System.out.println("接收消息: "+message.getText());
session.commit(); // 意思是确认消息,告知已接收该消息
message = (TextMessage) consumer.receive();
}
session.close();
connection.close();
}
}
package activemq.basic.persistent.yes;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class TopicSender {
/**
* topic持久化消息模式
* 1. 消息生产者用使用DeliveryMode.PERSISTENT模式
* 2. 然后开始连接
*/
public static void main(String[] args) throws Exception{
// 连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.230.130:61616");
//创建连接
Connection connection = connectionFactory.createConnection();
//创建会话,携带事物,自动匹配消息
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// 创建消息目的地
Destination destination = session.createTopic("MyTopicCJ");
//创建生产者
MessageProducer producer = session.createProducer(destination);
// 消息生产者设置 消息的持久化模式
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
connection.start();
for (int i = 0; i < 3; i++) {
TextMessage message = session.createTextMessage("message--"+i);
Thread.sleep(1000);
producer.send(message);
}
session.commit();
session.close();
connection.close();
}
}
package activemq.basic.persistent.yes;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicSubscriber;
import org.apache.activemq.ActiveMQConnectionFactory;
public class TopicReceiver {
/**
* topic持久化消息模式
*
*/
public static void main(String[] args) throws Exception{
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.230.130:61616");
//创建连接
Connection connection = connectionFactory.createConnection();
// 设置客户端持久化消息消费者ID
connection.setClientID("aha");
//创建会话,携带事物,消息签收机制
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// 创建消息目的地
Topic destination = session.createTopic("MyTopicCJ");
TopicSubscriber tq = session.createDurableSubscriber(destination, "haha");
connection.start();
// 同步消费的方式,会形成阻塞
Message message = tq.receive();
while(message != null) {
TextMessage textMessage = (TextMessage) message;
session.commit(); // 意思是确认消息,告知已接收该消息
System.out.println("接收消息: "+textMessage.getText());
message = tq.receive();
}
session.close();
connection.close();
}
}
1. 以代码方式启动activemq实例broker
package activemq.broker.basic;
import java.net.URI;
import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService;
public class InnerBroker {
//broker service 启动mq实例
/*public static void main(String[] args) throws Exception{
// 嵌入式broker(activemq实例)
BrokerService broker = new BrokerService();
broker.setUseJmx(true);
broker.addConnector("tcp://localhost:61616");
broker.start();
}*/
// brokerFactory 启动mq实例
public static void main(String[] args) throws Exception{
String url = "properties:broker.properties";
BrokerService broker = BrokerFactory.createBroker(new URI(url));
broker.addConnector("tcp://localhost:61616");
broker.start();
}
}
# broker.properties
useJms=true
persistent=false
brokerName=cheese
- 将broker配置在配置文件中,与spring整合
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<!-- activemq -->
<!-- 此种方式,在启动容器时,会自动启动内嵌式的mq服务器 -->
<!-- 推荐 -->
<!-- <bean id="broker" class="org.apache.activemq.broker.BrokerService"
init-method="start" destroy-method="stop">
<property name="brokerName" value="myBroker" />
<property name="persistent" value="false" />
<property name="brokerName" value="aha"></property>
<property name="transportConnectorURIs">
<list>
<value>tcp://localhost:61616</value>
</list>
</property>
</bean> -->
<!-- brokerFactory方式启动 -->
<!-- <bean id="broker" class="org.apache.activemq.xbean.BrokerFactoryBean">
<property name="config" value="resources/activemq-simple.xml"></property>
<property name="start" value="true"></property>
</bean> -->
</beans>