1、接口

JMS 公共

点对点域

发布/订阅域 

ConnectionFactory QueueConnectionFactory TopicConnectionFactory
Connection QueueConnection TopicConnection
Destination Queue Topic
Session QueueSession TopicSession
MessageProducer QueueSender TopicPublisher
MessageConsumer QueueReceiver TopicSubscriber

P2P模式:

package com.xh.mq.queue;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class Sender {
private static final int SEND_NUMBER = 15;
public static void main(String[] args) {
ConnectionFactory connectionFactory;
Connection connection = null;
Session session;
Destination destination;
MessageProducer producer;
connectionFactory = new ActiveMQConnectionFactory(
"tcp://localhost:61616");
try {
connection = connectionFactory.createConnection();
// 启动
connection.start();
session = connection.createSession(Boolean.TRUE,
Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("FirstQueue");
producer = session.createProducer(destination);
// 设置不持久化
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
sendMessage(session, producer);
session.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != connection)
connection.close();
} catch (Throwable ignore) {
}
}
} public static void sendMessage(Session session, MessageProducer producer)
throws Exception {
for (int i = 1; i <= SEND_NUMBER; i++) {
TextMessage message = session
.createTextMessage("ActiveMq 发送的消息" + i);
System.out.println("发送消息:" + "ActiveMq 发送的消息" + i);
producer.send(message);
Thread.sleep(1000);
}
}
}
package com.xh.mq.queue;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class Receiver { public static void main(String[] args) throws JMSException {
ConnectionFactory connectionFactory;
Connection connection = null;
Session session;
Destination destination;
MessageConsumer consumer;
connectionFactory = new ActiveMQConnectionFactory(
"tcp://localhost:61616");
connection = connectionFactory.createConnection();
// 启动
connection.start();
session = connection.createSession(Boolean.FALSE,
Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("FirstQueue");
consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
TextMessage message_ = (TextMessage) message;
try {
System.out.println("收到消息" + message_.getText());
Thread.sleep(1000);
} catch (JMSException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}

Topic模式:

package com.xh.mq.topic;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQMapMessage;
import javax.jms.*;
public class Publisher { protected static int count = 1;
protected static int total;
protected static int index; protected static String brokerURL = "tcp://localhost:61616";
protected static transient ConnectionFactory factory;
protected transient Connection connection;
protected transient Session session;
protected transient Destination destination;
protected transient MessageProducer producer; public Publisher() throws JMSException {
factory = new ActiveMQConnectionFactory(brokerURL); //创建连接工场
connection = factory.createConnection(); //创建连接
try {
connection.start(); //打开连接
} catch (JMSException jmse) {
connection.close();
throw jmse;
}
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //创建session 不带事务
destination = session.createTopic("STOCKS.topic"); //创建topic
producer = session.createProducer(destination); //创建publisher
} public void close() throws JMSException {
if (connection != null) {
connection.close();
}
} public static void main(String[] args) throws JMSException {
Publisher publisher = new Publisher();
while (total < 15) {
for (int i = 0; i < count; i++) {
publisher.sendMessage();
}
total += count;
try {
Thread.sleep(1000);
} catch (InterruptedException x) {
}
}
publisher.close();
} protected void sendMessage() throws JMSException {
Message message = createStockMessage(session);
System.out.println("Sending: " + ((ActiveMQMapMessage) message).getContentMap() + " on destination: " + destination);
producer.send(destination, message);
} protected Message createStockMessage(Session session) throws JMSException {
MapMessage message = session.createMapMessage();
message.setString("topic", "topic");
message.setInt("index",index++); return message;
} }
package com.xh.mq.topic;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class Subscriber { private static String brokerURL = "tcp://localhost:61616";
private static transient ConnectionFactory factory;
private transient Connection connection;
private transient Session session;
private transient Destination destination;
private transient MessageConsumer messageConsumer; public Subscriber() throws JMSException {
factory = new ActiveMQConnectionFactory(brokerURL);
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createTopic("STOCKS.topic" );
messageConsumer = session.createConsumer(destination); } public static void main(String[] args) throws JMSException {
Subscriber consumer1 = new Subscriber();
consumer1.messageConsumer.setMessageListener(new Listener()); } private static class Listener implements MessageListener { public void onMessage(Message message) {
try {
MapMessage map = (MapMessage)message;
String topic = map.getString("topic");
int index = map.getInt("index");
System.out.println(topic + "\t" +index+"\n" );
} catch (Exception e) {
e.printStackTrace();
}
} } }

ActiveMQ学习笔记1的更多相关文章

  1. ActiveMQ学习笔记(5)——使用Spring JMS收发消息

      摘要 ActiveMQ学习笔记(四)http://my.oschina.net/xiaoxishan/blog/380446 中记录了如何使用原生的方式从ActiveMQ中收发消息.可以看出,每次 ...

  2. apache activemq 学习笔记

    0.activemq的概念 activemq实现了jms(java Message server),用于接收,发送,处理消息的开源消息总线. 1.activemq和jms的区别 jms说白了就是jav ...

  3. ActiveMQ学习笔记

    关键接口和类: ConnectionFactory connectionFactory;//连接工厂 Connection connection;//连接 Session session; Desti ...

  4. ActiveMQ 学习笔记

    http://somebody-hjh.iteye.com/blog/726050 一.概述 Message,即消息.人与人之间通过消息传递信息.言语.眼神.肢体动作都可被视为消息体.当然还有我们经常 ...

  5. ActiveMQ学习笔记(二) JMS与Spring

    上文可见,JMS Native API使用起来不是特别方便.好在Spring提供了很好的JMS支持. (一)配置ConnectionFactory 如果使用连接池的话,不要忘记activemq-poo ...

  6. ActiveMQ学习笔记(一) JMS概要

    (一)什么是JMS jms即Java消息服务(Java Message Service)应用程序接口是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送 ...

  7. activemq学习笔记2

    基本步骤: ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616"); ...

  8. ActiveMQ学习笔记(22)----ActiveMQ的优化和使用建议

    1. 什么时候使用ActiveMQ 1. 异步通信 2. 一对多通信 3. 做个系统的集成,同构,异构 4. 作为RPC的替代 5. 多个应用相互解耦 6. 作为事件驱动架构的幕后支撑 7. 为了提高 ...

  9. ActiveMQ学习笔记(21)----ActiveMQ集成Tomcat

    1. 监控和管理Broker Web Console 方式:直接访问ActiveMQ的管理页面:http://localhost:8161/admin,默认的用户名和密码是admin/admin.具体 ...

随机推荐

  1. UUID类型如何比较是否相等

    直接使用equals方法 UUID x = UUID.fromString("062db347-6a72-69a1-40c0-7516e0a26459"); UUID y = UU ...

  2. Sublime text3 插件LiveReload 实现实时预览

    1.首先要安装插件LiveReload Sublime text3. 菜单 preferences->packages control,输入install.. 回车,输入LiveReload回车 ...

  3. 值得Python小白学习的书 简单推荐几本吧

    于我个人而言,我很喜欢Python,当然我也有很多的理由推荐你去学python.我只说两点.一是简单,二是写python薪资高.我觉得这俩理由就够了,对不对.买本书,装上pycharm,把书上面的例子 ...

  4. Leetcode 414.Fizz Buzz By Python

    写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz": 3.如果 n 同时是3和 ...

  5. linux ssh keys

    1. 原理: SSH 密钥对总是成双出现的,一把公钥,一把私钥.公钥可以自由的放在您所需要连接的 SSH 服务器上,而私钥必须稳妥的保管好. 所谓"公钥登录",原理很简单,就是用户 ...

  6. Elasticsearch基础知识要点QA

    前言:本文为学习整理实践他人成果的记录型博客.在此统一感谢各原作者,如果你对基础知识不甚了解,可以通过查看Elasticsearch权威指南中文版, 此处注意你的elasticsearch版本,版本不 ...

  7. Codeforces Round #419 (Div. 2)(B)差分数组

    传送门:Problem B https://www.cnblogs.com/violet-acmer/p/9721160.html 题意: Karen有n个关于煮咖啡的食谱,每个食谱都有个煮咖啡的最适 ...

  8. Failed to load because no supported source was found

    Uncaught (in promise) DOMException: Failed to load because no supported source was found? 等待解决:

  9. MQTT 及其 测试工具

    协议说明书:https://mcxiaoke.gitbooks.io/mqtt-cn/content/mqtt/04-OperationalBehavior.html 官网提供了很多的broker模拟 ...

  10. SQL Server 2012/2016/2017 新增函数

    /************************************************************** SQL Server 2012 新增的函数 ************** ...