JMS-ActiveMQ入门
一.了解JMS;
在JMS前各个mom厂商API不同,混乱不堪;jms至少统一了java世界的MOM
二、java环境,jdk安装【不多说,搞jms,activemq这个是小意思】,eclipse安装【解压】
三、Apache ActiveMQ安装使用【下载解压就可以使用,如果启动失败,google哈了网上都有】
开始:
下载apache-activemq-5.2.0解压到G:\,然后运行bin目录的activemq.bat启动服务;
如图:
2009-02-15 22:19:08,218 [main ] INFO TransportServerThreadSupport - Listening for connections at: tcp://20081026-1344:61616
2009-02-15 22:19:08,218 [main ] INFO TransportConnector - Connector openwire Started
2009-02-15 22:19:08,515 [main ] INFO TransportServerThreadSupport - Listening for connections at: ssl://20081026-1344:61617
2009-02-15 22:19:08,515 [main ] INFO TransportConnector - Connector ssl Started
2009-02-15 22:19:08,515 [main ] INFO TransportServerThreadSupport - Listening for connections at: stomp://20081026-1344:61613
2009-02-15 22:19:08,515 [main ] INFO TransportConnector - Connector stomp Started
2009-02-15 22:19:08,546 [main ] INFO TransportServerThreadSupport - Listening for connections at: xmpp://20081026-1344:61222
2009-02-15 22:19:08,546 [main ] INFO TransportConnector - Connector xmpp Started
2009-02-15 22:19:08,546 [main ] INFO NetworkConnector - Network Connector default-nc Started
2009-02-15 22:19:08,546 [main ] INFO BrokerService - ActiveMQ JMS Message Broker (localhost, ID:20081026-1344-4094-1234707547843-0:0) started
2009-02-15 22:19:08,656 [main ] INFO log - Logging to org.slf4j.impl.JCLLoggerAdapter(org.mortbay.log) via org.mortbay.log.Slf4jLog
2009-02-15 22:19:08,671 [main ] INFO log - jetty-6.1.9
2009-02-15 22:19:09,062 [main ] INFO WebConsoleStarter - ActiveMQ WebConsole initialized.
2009-02-15 22:19:09,375 [main ] INFO /admin - Initializing Spring FrameworkServlet 'dispatcher'
2009-02-15 22:19:09,468 [main ] INFO log - ActiveMQ Console at http://0.0.0.0:8161/admin
2009-02-15 22:19:09,625 [main ] INFO log - ActiveMQ Web Demos at http://0.0.0.0:8161/demo
2009-02-15 22:19:09,765 [main ] INFO log - RESTful file access application at http://0.0.0.0:8161/fileserver
2009-02-15 22:19:09,781 [main ] INFO log - Started SelectChannelConnector@0.0.0.0:8161
2009-02-15 22:19:10,015 [main ] INFO TransportConnector - Connector vm://localhost Started
http://192.168.1.103:8161/admin
进入管理管理页面
新建一个queue
发送代码:
package com.bobo.activemq;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Sender {
private static final int SEND_NUMBER = 5;
public static void main(String[] args) {
// ConnectionFactory :连接工厂,JMS 用它创建连接
ConnectionFactory connectionFactory;
// Connection :JMS 客户端到JMS Provider 的连接
Connection connection = null;
// Session: 一个发送或接收消息的线程
Session session;
// Destination :消息的目的地;消息发送给谁.
Destination destination;
// MessageProducer:消息发送者
MessageProducer producer;
// TextMessage message;
// 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://192.168.1.103:61616");
try {
// 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(Boolean.TRUE,
Session.AUTO_ACKNOWLEDGE);
// 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
destination = session.createQueue("xingbo.xu-queue");
// 得到消息生成者【发送者】
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);
}
}
}
接受代码:
package com.bobo.activemq;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Receiver {
public static void main(String[] args) {
// ConnectionFactory :连接工厂,JMS 用它创建连接
ConnectionFactory connectionFactory;
// Connection :JMS 客户端到JMS Provider 的连接
Connection connection = null;
// Session: 一个发送或接收消息的线程
Session session;
// Destination :消息的目的地;消息发送给谁.
Destination destination;
// 消费者,消息接收者
MessageConsumer consumer;
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://192.168.1.103:61616");
try {
// 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(Boolean.FALSE,
Session.AUTO_ACKNOWLEDGE);
// 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
destination = session.createQueue("xingbo.xu-queue");
consumer = session.createConsumer(destination);
while (true) {
TextMessage message = (TextMessage) consumer.receive(1000);
if (null != message) {
System.out.println("收到消息" + message.getText());
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != connection)
connection.close();
} catch (Throwable ignore) {
}
}
}
}
详情看附件【world,代码】
上一篇: C#访问远程主机资源的多种方法分享