Maven 配置文件  

<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.1</version>
</dependency>

provider 生产者代码

import static commons.Constants.*;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session; import org.apache.activemq.ActiveMQConnectionFactory; public class Provider {
public static void main(String[] args) {
ConnectionFactory cf = new ActiveMQConnectionFactory(URL); //创建工厂链接对象
Connection connection = null;
Session session = null;
Queue queue = null;
MessageProducer producer = null;
try {
connection = cf.createConnection(); //使用工厂创建链接
connection.start(); //开启连接
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //使用连接对象创建会话
queue = session.createQueue(QUEUE_ONE); //使用会话创建目标对象
producer = session.createProducer(queue); // 使用会话、目标对象创建生产者对象
Message message = session.createTextMessage("hello ,i'm not good"); //使用会话创建消息对象
producer.send(message);//发送消息
System.out.println("send:"+message.toString());
} catch (JMSException e) {
e.printStackTrace();
}finally {
if(producer != null) {
try {
producer.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
if(session != null) {
try {
session.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
if(connection != null) {
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
} }

Consumer 消费者

import static commons.Constants.*;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; public class Consumer { public static void main(String[] args) {
ConnectionFactory cf = new ActiveMQConnectionFactory(URL);
Connection connection = null;
Session session = null;
MessageConsumer consumer = null;
try {
connection = cf.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(QUEUE_ONE);
consumer = session.createConsumer(queue);
//向consumer对象中设置一个messageListener对象,用来接收消息
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
if(message instanceof TextMessage) { //当前测试,仅测试Text类型的消息
TextMessage text = (TextMessage)message;
try {
System.out.println(text.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
});
System.out.println("consumer : "+System.currentTimeMillis());
System.in.read();
}catch(Exception e) {
e.printStackTrace();
}finally {
if(consumer != null) {
try {
consumer.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
if(session != null) {
try {
session.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
if(connection != null) {
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
}

Constants 常量类

package commons;

public interface Constants {

    String URL = "tcp://192.168.49.128:61616";

    String QUEUE_ONE = "CQC_ONE";

}

ActiveMQ demo的更多相关文章

  1. activemq demo指南

    queue与topic的技术特点对比   topic queue 概要 Publish Subscribe messaging 发布订阅消息 Point-to-Point 点对点 有无状态 topic ...

  2. 消息中间件系列一:入门、JMS规范、ActiveMQ使用

    一.入门 1. 消息中间件的定义 没有标准定义,一般认为,采用消息传送机制/消息队列 的中间件技术,进行数据交流,用在分布式系统的集成 2. 为什么要用消息中间件 解决分布式系统之间消息的传递.电商场 ...

  3. ActiveMQ 集群配置 高可用

    自从activemq5.9.0开始,activemq的集群实现方式取消了传统的Pure Master Slave方式,增加了基于zookeeper+leveldb的实现方式,其他两种方式:目录共享和数 ...

  4. activemq 实战 四 传输连接器-Transport connectors 4.2

    In order to exchange messages, producers and consumers (clients) need to connect to the broker. This ...

  5. 消息队列之 ActiveMQ(山东数漫江湖)

    简介 ActiveMQ 特点 ActiveMQ 是由 Apache 出品的一款开源消息中间件,旨在为应用程序提供高效.可扩展.稳定.安全的企业级消息通信. 它的设计目标是提供标准的.面向消息的.多语言 ...

  6. Spring和ActiveMQ集成实现队列消息以及PUB/SUB模型

    前言:本文是基于Spring和ActiveMQ的一个示例文章,包括了Point-To-Point的异步队列消息和PUB/SUB(发布/订阅)模型,只是做了比较简单的实现,无任何业务方面的东西,作为一个 ...

  7. java之消息队列ActiveMQ实践

    原创论文:https://www.cnblogs.com/goujh/p/8510239.html 消息队列的应用场景: 消息队列应用场景 异步处理,应用解耦,流量削锋和消息通讯四个场景 异步处理: ...

  8. JMS学习七(ActiveMQ之Topic的持久订阅)

    非持久化订阅持续到它们订阅对象的生命周期.这意味着,客户端只能在订阅者活动时看到相关主题发布的消息.如果订阅者不活动,它会错过相关主题的消息.如果花费较大的开销,订阅者可以被定义为durable(持久 ...

  9. 消息队列之 ActiveMQ

    简介 ActiveMQ 特点 ActiveMQ 是由 Apache 出品的一款开源消息中间件,旨在为应用程序提供高效.可扩展.稳定.安全的企业级消息通信. 它的设计目标是提供标准的.面向消息的.多语言 ...

随机推荐

  1. [leetcode.com]算法题目 - Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  2. ZKWeb网页框架1.5正式发布

    本次更新的内容有 明显的改进了IoC容器在部分情况下的性能, 当前的性能和Grace, DryIoC同等 添加IHttpResquestHandlerWrapper接口让重载Http上下文更容易 添加 ...

  3. 使用Python请求http/https时设置失败重试次数

    设置请求时的重试规则 import requests from requests.adapters import HTTPAdapter s = requests.Session() a = HTTP ...

  4. Python网络练习题

    练习题 什么是C/S架构? C/S架构客户端.服务端架构,C/S端软件主要有网络游戏,QQ等 互联网协议是什么?分别介绍五层协议中每一层的功能? 互联网协议:计算机之间的通信标准 物理层:主要是基于电 ...

  5. opencv2.4.13.7的resize函数使用(c++)

    先来看一下resize函数的原型,如下. C++: void resize(InputArray src, OutputArray dst, Size dsize, double fx=0, doub ...

  6. linux free命令详解(一)

    一. 作用 free命令可以显示当前系统未使用的和已使用的内存数目,还可以显示被内核使用的内存缓冲区. 二. 语法 free [选项] 三. 选项       默认情况下,即在没有选项的情况下,&qu ...

  7. 全网最详细的Windows系统里Oracle 11g R2 Database(64bit)安装后的初步使用(图文详解)

    不多说,直接上干货! 前期博客 全网最详细的Windows系统里Oracle 11g R2 Database(64bit)的下载与安装(图文详解) 命令行方式测试安装是否成功 1)   打开服务(cm ...

  8. 大叔来说说Markdown的使用

    强调和高亮背景 中国是伟大的民族! Highlight 中国是`伟大`的民族! ==Highlight== 链接 大叔博客园 [大叔博客园](http://www.cnblogs.com/lori & ...

  9. SHELL脚本编程的常识和VI常用技巧

    来源:http://mprc.pku.edu.cn/mentors/training/TrainingCourses/material/ShellProgramming.HTM#_Toc3751808 ...

  10. Spring mvc 4系列教程(一)

    一.Spring框架概览 Spring框架是一种轻量级.一站式解决企业级应用的解决方案.不仅如此,Spring的模块化的特点,可以使你只引用所需要的部分,而无需引用全部.你可以使用控制反转容器(IoC ...