ActiveMQ使用例子
网上收集的例子:有broker,producer,consumer
public class MqApp {
public static void main(String[] args) throws Exception {
// 启动broker
BrokerService broker = new BrokerService();
// configure the broker
broker.addConnector("tcp://localhost:61616");
broker.start();
// 创建producer和consumer
makeThread(new HelloProducer(), false);
makeThread(new HelloConsumer(), false);
// LockSupport.park();
}
public static void makeThread(Runnable runnable, boolean daemon) {
Thread t = new Thread(runnable);
t.setDaemon(daemon);
t.start();
}
public static class HelloProducer implements Runnable {
public void run() {
try {
// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory =
// new ActiveMQConnectionFactory("vm://localhost");
new ActiveMQConnectionFactory("tcp://localhost:61616");
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createQueue("TEST.FOO");
// Create a MessageProducer from the Session to the Topic or Queue
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Create a messages
String text = "Hello world! From: " + Thread.currentThread().getName() + ", " + this.hashCode();
TextMessage message = session.createTextMessage(text);
// Tell the producer to send the message
System.out.println("Sent message: "+ message.hashCode() + ", " + Thread.currentThread().getName());
producer.send(message);
// Clean up
session.close();
connection.close();
}
catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}
}
public static class HelloConsumer implements Runnable, ExceptionListener {
public void run() {
try {
// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory =
// new ActiveMQConnectionFactory("vm://localhost");
new ActiveMQConnectionFactory("tcp://localhost:61616");
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
connection.setExceptionListener(this);
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createQueue("TEST.FOO");
// Create a MessageConsumer from the Session to the Topic or Queue
MessageConsumer consumer = session.createConsumer(destination);
// Wait for a message
Message message = consumer.receive(1000);
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
System.out.println("Received: " + text);
} else {
System.out.println("Received: " + message);
}
consumer.close();
session.close();
connection.close();
} catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}
public synchronized void onException(JMSException ex) {
System.out.println("JMS Exception occured. Shutting down client.");
}
}
}
ActiveMQ使用例子的更多相关文章
- JMS Activemq实战例子demo
上一篇已经讲了JMS的基本概念,这一篇来上手练一练,如果对JMS基本概念还不熟悉,欢迎参靠JMS基本概. 这篇文章所使用的代码已经不是我刚入手时的代码,已经经过我重构过的代码,便于理解,并且加了很多中 ...
- Jmeter 测试 JMS (Java Message Service)/ActiveMQ 性能
前言 JMS介绍:JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送 ...
- Apache ActiveMQ教程
一.特性及优势 1.实现JMS1.1规范,支持J2EE1.4以上 2.可运行于任何jvm和大部分web容器(ActiveMQ works great in any JVM) 3.支持多种语言客户端(j ...
- SpringBoot2.0源码分析(一):SpringBoot简单分析
SpringBoot2.0简单介绍:SpringBoot2.0应用(一):SpringBoot2.0简单介绍 本系列将从源码角度谈谈SpringBoot2.0. 先来看一个简单的例子 @SpringB ...
- ActiveMQ结合WebScoket应用例子以及介绍
一.ActiveMQ的介绍? 1.JMS基础概念 JMS(java Message Service) 即使java消息服务,它提供标准的产生.发送.接收的接口简化企业应用开发,它支持两种消息通信模型: ...
- ActiveMQ学习--002--Topic消息例子程序
一.非持久的Topic消息示例 注意 此种方式消费者只能接收到 消费者启动之后,发送者发送的消息. 发送者 package com.lhy.mq.helloworld; import java.uti ...
- 理解面向消息中间件及JMS 以及 ActiveMQ例子
为了帮助你理解ActiveMQ的意义,了解企业消息传送背景和历史是很重要的.讨论完企业消息传送,你将可以通过一个小例子了解JMS及其使用.这章的目的是简要回顾企业消息传送及JMS规范.如果你已经熟悉这 ...
- 《ActiveMQ in Action》例子
本章内容: 介绍本书中所有例子的使用场景 使用 Maven 编译.运行例子 例子中怎么使用 ActiveMQ 简介 ActiveMQ 不仅实现了 JMS 规范中定义的所有特性,也额外提供了一些特有且有 ...
- ActiveMQ的简单例子应用
ActiveMQ是一种消息中间件,它实现了JMS规范,提供点对点和订阅-发布两种模式.下面介绍下ActiveMQ的使用: 一.环境的搭建 首先我们需要下载ActiveMQ的安装包,下载地址http:/ ...
随机推荐
- CSS sprites
CSS Sprites在国内很多人叫css精灵,是一种网页图片应用处理方式. 优点: 它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问该页面时,载入的图片就不会像以前那样一 ...
- ashx和aspx的区别
1. ashx是一般处理程序,一般返回的数据有两种,一种是html页面,一种是只返回一个字符串. 2. aspx是web窗体程序,每次新建都回自带一个界面和一个后台处理程序. 3. 根据以上两点,可以 ...
- Excel 常用设置
首行固定 视图->冻结窗口
- mysql 清空表——truncate 与delete的区别
清空表 truncate table [表名]: delete from [表名]: 注: truncate是整体删除(速度较快), delete是逐条删除(速度较慢). truncate不写服务器l ...
- 查看GPU占用率以及指定GPU加速程序
GPU占用率查看: 方法一:任务管理器 如图,GPU0和GPU1的占用率如下显示. 方法二:GPU-Z软件 下面两个GPU,上面是GPU0,下面是GPU1 sensors会话框里的GPU ...
- 学习笔记23—window10 64位 python2.7 安装liblinear
最近在使用pythin,因为要使用libsvm,所以到官网去下载libsvm.官网地址为libsvm(https://www.csie.ntu.edu.tw/~cjlin/libsvm/)结果下载下来 ...
- redux与redux-react使用示例
redux使用 <script type="text/babel"> var Counter=React.createClass({ incrementIfOdd:fu ...
- powerdesiger 导入sqlserver 方法
https://jingyan.baidu.com/album/7f766daf465e9c4101e1d0d5.html
- Zabbix安装(debian,centos)
lnmp和lamp架构搭建一键安装脚本下载地址:https://lnmp.org/download.html https://github.com/teddysun/lamp/tree/master ...
- 如何理解以太坊ABI - 应用程序二进制接口
很多同学不是很明白以太坊ABI是什么,他的作用是什么,读完本文就明白了. 写在前面 阅读本文前,你应该对以太坊.智能合约有所了解, 如果你还不了解,建议你先看以太坊是什么,也可以观看我们的视频:零基础 ...