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. 经典Java面试题收集

    1.面向对象的特征有哪些方面? 答:面向对象的特征主要有以下几个方面: 抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象和行为抽象两方面.抽象只关注对象有哪些属性和行为,并不关注这些 ...

  2. hud 1312

    There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...

  3. HSQL可视化工具

    本地使用HSQL数据库进行开发,多是集成在开发工具的内部,比如studio,往往看不到HSQL数据库,那么如何查看HSQL数据库呢? 可以使用hsql自带的可视化工具,运行hsqldb-*.jar 包 ...

  4. 自学Linux Shell3.6-文件查看命令file cat more less tail head

    点击返回 自学Linux命令行与Shell脚本之路 3.6-文件查看命令file cat more less tail head 1.参看文件类型file 该命令用来识别文件类型,也可用来辨别一些文件 ...

  5. loj #117. 有源汇有上下界最小流

    题目链接 有源汇有上下界最小流,->上下界网络流 注意细节,边数组也要算上后加到SS,TT边. #include<cstdio> #include<algorithm> ...

  6. bzoj4571/luogu3293 美味 (主席树+贪心)

    首先想到建出可持久化trie树然后在上面贪心,但是它加了一个数所以不能这么做 但依然可以贪心,仿照上面那个的过程,如果设y是在第i位上^b是1的数(前面的位数已经贪好了),我只要在[l,r]范围内能有 ...

  7. 前端学习 -- Html&Css -- 条件Hack 和属性Hack

    条件Hack 语法: <!--[if <keywords>? IE <version>?]> HTML代码块 <![endif]--> <keyw ...

  8. ffmpeg 在ubuntu上编译环境搭建和开发

    步骤如下: 1. 下载 官网永远是王道,呵呵:http://ffmpeg.org/download.html 或者 svn checkout svn://svn.mplayerhq.hu/ffmpeg ...

  9. 2018年湘潭大学程序设计竞赛 H统计颜色

    链接:https://www.nowcoder.com/acm/contest/105/H来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  10. mui dtpicker 时间的设置 以及MUI的弹窗

    1)引入mui.min.css,然后引入mui.picker.min.css            注意这个mui.picker.min.css  与 mui.picker.css  不一样 2)引入 ...