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. 【POJ1456】Supermarket(贪心)

    BUPT2017 wintertraining(16) #4 F POJ - 1456 题意 每个商品有过期日期和价格,每天可以卖一个商品,必须在过期前出售才能收益,求最大收益. 题解 贪心,按价格排 ...

  2. android GridView 的使用 实现多项选择

    今天小研究了一下GridView,目的是为了实现 下面的效果(GridView多项选择): 首先,在布局文件添加GridView ,创建适配器的items... 具体的都在注释里边了,下面是 程序源码 ...

  3. Azure vm 扩展脚本自动部署Elasticsearch集群

    一.完整过程比较长,我仅给出Azure vm extension script 一键部署Elasticsearch集群的安装脚本,有需要的同学,可以邮件我,我给你完整的ARM Template 如果你 ...

  4. A1044. Shopping in Mars

    Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...

  5. 斯坦福大学公开课机器学习:梯度下降运算的特征缩放(gradient descent in practice 1:feature scaling)

    以房屋价格为例,假设有两个特征向量:X1:房子大小(1-2000 feets), X2:卧室数量(1-5) 关于这两个特征向量的代价函数如下图所示: 从上图可以看出,代价函数是一个又瘦又高的椭圆形轮廓 ...

  6. springboot的起步依赖

    加载自动配置的方式2: springboot读取配置文件的方式: 1.读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或applicatio ...

  7. go 包-锁机制

    线程同步 import(“sync”) 互斥锁, var mu sync.Mutex 读写锁, var mu sync.RWMutex 资源竞争样例 func testMap() { var a ma ...

  8. 计算机网络原理和OSI模型与TCP模型

    计算机网络原理和OSI模型与TCP模型 一.计算机网络的概述 1.计算机网络的定义 计算机网络是一组自治计算机的互连的集合 2.计算机网络的基本功能 a.资源共享 b.分布式处理与负载均衡 c.综合信 ...

  9. Unity5天空盒小黑点问题

    unity5打开的时候有时候天空盒基本上全是小黑点,原因我现在不知道,以后再补充. 解决办法: 打开windows--lighting选项.然后双击skybox里面的材质,右边会显示当前天空盒的材质界 ...

  10. 多目标遗传算法 ------ NSGA-II (部分源码解析)辅助变量 双链表操作 list.c

    /* A custom doubly linked list implemenation */ # include <stdio.h> # include <stdlib.h> ...