关于JMS一个最简单的Demo:
发送消息的类JmsSender
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JmsSender {
/**
* @param args
* @throws JMSException
*/
public static void main(String[] args) throws JMSException {
// TODO Auto-generated method stub
// ConnectionFactory :连接工厂,JMS 用它创建连接
ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER
, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
//JMS 客户端到JMS Provider 的连接
Connection connection = factory.createConnection();
connection.start();
// Session: 一个发送或接收消息的线程
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// Destination :消息的目的地;消息发送给谁.
// 获取session注意参数值my-queue是Query的名字
Destination destination = session.createQueue("testQueue");
// MessageProducer:消息生产者
MessageProducer producer = session.createProducer(destination);
//设置不持久化
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
sendMsg(session, producer);
session.commit();
connection.close();
}
public static void sendMsg(Session session, MessageProducer producer) throws JMSException {
//创建一条文本消息
TextMessage message = session.createTextMessage("Hello ActiveMQ!");
//通过消息生产者发送消息
producer.send(message);
System.out.println("");
}
}
接收消息的类JmsReceiver
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JmsReceiver {
/**
* @param args
* @throws JMSException
*/
public static void main(String[] args) throws JMSException {
// TODO Auto-generated method stub
// ConnectionFactory :连接工厂,JMS 用它创建连接
ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER
, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
//JMS 客户端到JMS Provider 的连接
Connection connection = factory.createConnection();
connection.start();
// Session: 一个发送或接收消息的线程
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// Destination :消息的目的地;消息发送给谁.
// 获取session注意参数值my-queue是Query的名字
Destination destination = session.createQueue("myqueue");
MessageConsumer consumer = session.createConsumer(destination);
while (true) {
TextMessage message = (TextMessage) consumer.receive(1000);
if (message != null) {
System.out.println("收到消息:" + message.getText());
} else {
break;
}
}
session.close();
connection.close();
}
}
启动ActiveMQ:
先run发送者,后run接收者。
分享到:
相关推荐
activeMQ之hello world小程序
我们使用ActiveMQ为大家实现一种点对点的消息模型。如果你对点对点模型的认识较浅,可以看一下第一篇博文的介绍。
qt5.8 msvc2015 加载activemq 成功自发自首,编写了模块,可以自行下载,activemq里面的dll,也都存在, 调用方法 CSpiderPluginAMq::GetInstance()->InitNetwork(); CSpiderPluginAMq::GetInstance()->...
解压缩apache-activemq-5.5.1-bin.zip,然后双击...包含了apache-activemq-5.5.1-bin.zip以及ActiveMQ一个helloworld的demo启动ActiveMQ以后,登陆:http://localhost:8161/admin/,创建一个Queue,命名为FirstQueue。
ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的...
com.activemq下是直接手发消息 springhello包下整合spring收发消息 xml中包含DefaultMessageListenerContainer
为了有助于使用Apache ActiveMQ 5,你也许希望从Version 5 Getting Started手册或Configuring Version 5 Transports开始。除此之外,下面还有一份手册列表: Configure version 5 Brokers(版本5代理的配置) ...
的基础知识,并且讲解了一个HelloWorld级别的例子帮助理解。这是关于消息中间件ActiveMQ的一个系列专题文章,将涵盖JMS、ActiveMQ的初步入门及API详细使用、两种经典的消息模式(PTP andPub/Sub)、与Spring整合、...
融合JMS POC 如何运行: 确保Confluent企业和ActiveMQ都已安装并在默认端口上运行。... 请求“ Hello World被发送到一个Confluent主题,该主题由回复者接收并发送回请求者。 注意匹配的相关ID。 现在,您只需更改JNDI
Spring Boot ActiveMQ演示一个使用嵌入式ActiveMQ进行演示的Spring Boot应用程序: 重新送货延迟接收和回复模式跑步启动应用程序并提交一条消息,以查看延迟重新交付。 $ curl -XPOST ...
ActiveMQ安装 : 1.下载:http://activemq.apache.org/download.html 最新Windows版本 2.配置jdk环境,解压ActiveMQ到本地 3.启动ActiveMQ服务器: 32位机 : 直接运行\bin\win32\activemq.bat 64位机 : 直接运行\...
jun_spring 项目module组件功能列表【spring_helloworld】springboot 的一个 helloworld【spring_properties】springboot 读取配置文件中的内容TODO:1、整cs的login跟index的page,适配Nginx跟ssm_jwt;2、整ssm_...
string text = (string) "Hello world! from thread " + threadIdStr; for (int ix = 0; ix ; ++ix) { std::auto_ptr<TextMessage> message(session->createTextMessage(text)); message->setIntProperty(...
iBase4J-tools(依赖服务及其一键启动).windows环境下一键启动iBase4j的所有环境。包括nginx,redis,zookeeper和activemq。
:简单Hello World的NodeJS应用特色rhea JavaScript客户端 : Apache Camel 路由使用 ActiveMQ 组件和JmsSSLConnectionFactoy进行 AMQ Online TLS 连接 :Spring 应用程序显示具有 TLS 连接的 AMQ JMS 池库 :使用...
java开发oa系统源码下载 jun_spring 项目 项目说明 基于Spring家族的360度整合,...spring_activemq\ 干掉,放到mq里面 spring_atomikos\ 调整 spring_autowired\ 干掉,合并到spring_demos spring_camel\ 调
ActiveMQ Binding JAX-WS 2.x Endpoints to Jetty Contexts Java Management Extensions (JMX) 配置JMX教程 处理JVM NIO Bug Rewrite模块 Inversion of Control and Dependency Injection Frameworks Jetty XML IOC ...
如何在main方法执行前输出”hello world” java程序的初始化顺序 请说出作用域public,private,protected,以及不写时的区别 为什么java中有些接口没有任何方法 java中的clone方法有什么作用 面向对象有哪些特征 ...
开始maven 依赖<dependency> <groupId>io.jboot</groupId> <artifactId>jboot</artifactId> <version>3.9.8</version></dependency>Hello World@RequestMapping("/")public class HelloworldController extends ...