ActiveMQ中共分为两种:queue和topicjava
queue:在点对点消息传递域中,目的地被称为队列(一对一)spring
topic:在发布订阅消息中,目的地被称为主题(一对多)apache
特色:一、生产者将消息发布到topic中,每一个消息能够有多个消费者,属于一对多的关系springboot
二、生产者和消费者有时间上的相关性,订阅某个主题的消费者只能消费自他订阅之后发布到消息session
三、生产者生产消息时,topic是不保存消息它是无状态不落地的,假如无人订阅就生产消息即生产了一条废消息,因此通常先启动消费者,再启动生产者;maven
<!--activemq--> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.15.9</version> </dependency>
import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; /** * @ProjectName: springbootActiveMQ * @Package: cn.**.test * @Author: huat * @Date: 2020/1/4 9:29 * @Version: 1.0 */ public class ActiveMQTopicTest { //url路径 private static final String ACTRIVE_URL="tcp://192.168.44.135:61616"; //主题名称 private static final String TOPIC_NAME = "topic01"; public static void main(String[] args) { //一、建立链接工厂 //若是帐号密码没有修改的话,帐号密码默认均为admin ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTRIVE_URL); //若是帐号密码修改的话 //第一个参数为帐号,第二个为密码,第三个为请求的url //ActiveMQConnectionFactory activeMQConnectionFactory1=new ActiveMQConnectionFactory("admin","admin",ACTRIVE_URL); try { //二、经过链接工厂获取链接 Connection connection = activeMQConnectionFactory.createConnection(); connection.start(); //三、建立session会话 //里面会有两个参数,第一个为事物,第二个是签收 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //四、建立目的地(具体是队列仍是主题),这里是建立主题 Topic topic=session.createTopic(TOPIC_NAME); //五、建立消息生产者,主题模式 MessageProducer messageProducer = session.createProducer(topic); //六、经过messageProducer生产三条消息发送到MQ消息主题中 for (int i=0;i<3;i++){ //七、建立消息 TextMessage textMessage = session.createTextMessage("msg----->" + i);//建立一个文本消息 //八、经过messageProducer发送给mq messageProducer.send(textMessage); } messageProducer.close(); session.close(); connection.close(); System.out.println("消息发送成功"); } catch (JMSException e) { e.printStackTrace(); } } }
import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; /** * @ProjectName: springbootActiveMQ * @Package: cn.**.test * @Author: huat * @Date: 2020/1/4 9:43 * @Version: 1.0 */ public class ActiveMQTopicConsumer { //url路径 private static final String ACTRIVE_URL="tcp://192.168.44.135:61616"; //主题名称 private static final String TOPIC_NAME = "topic01"; public static void main(String[] args) { //一、建立链接工厂 //若是帐号密码没有修改的话,帐号密码默认均为admin ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTRIVE_URL); //若是帐号密码修改的话 //第一个参数为帐号,第二个为密码,第三个为请求的url //ActiveMQConnectionFactory activeMQConnectionFactory1=new ActiveMQConnectionFactory("admin","admin",ACTRIVE_URL); try { //二、经过链接工厂获取链接 Connection connection = activeMQConnectionFactory.createConnection(); connection.start(); //三、建立session会话 //里面会有两个参数,第一个为事物,第二个是签收 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //四、这里接受的topic的名称要和发送者的一致 Topic topic = session.createTopic(TOPIC_NAME); //五、建立消费者 MessageConsumer consumer = session.createConsumer(topic); //六、经过监听的方式消费消息 consumer.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { //若是message不等于null而且属于TextMessage类型(由于消息发送的类型是TextMessage,因此这里判断是不是这个类型) if(null!=message&&message instanceof TextMessage){ TextMessage textMessage=(TextMessage)message; try { System.out.println(textMessage.getText()); } catch (JMSException e) { e.printStackTrace(); } } } }); //七、保证控制台一直在运行 System.in.read(); //八、闭资源 consumer.close(); session.close(); connection.close(); }catch (Exception e){ e.printStackTrace(); } } }