ActiveMQ
来源:互联网 发布:股票预测软件破解 编辑:程序博客网 时间:2024/05/16 11:16
1:要用ActiveMQ 首先要把环境变量配置好:
先把JDK的 JAVA_HOME , classpath , path 配置好;
再配置 ACTIVE_HOME
最后再在classpath中配置 %ACTIVE_HOME%\conf\
path中配置%ACTIVE_HOME%\bin
环境配置好以后 打开 F:\ActiveMQ5.8\apache-activemq-5.8.0\bin 双击activemq.bat打开 ,如果正常启动;证明环境配置好了。
这里我遇到一个问题(Caused by: java.io.IOException: Failed to bind to server socket: tcp://0.0.0.0:6
1616?maximumConnections=1000&wireformat.maxFrameSize=104857600 due to: java.net.
BindException: Address already in use: JVM_Bind)
解决办法:右键 我的电脑 --> 管理-->服务和应用程序-->服务 找到 Internet Connection Sharing (ICS) 右键 停止 。再重新启动就可以了。
2:代码点击打开链接
需要的jar:
activemq-all-5.8.0.jar
geronimo-j2ee-management_1.1_spec-1.0.1.jar
geronimo-jms_1.1_spec-1.1.1.jar
slf4j-api-1.6.6.jar
log4j-1.2.17.jar
发送者:
package com.liuxian.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;
//Destination : 消息目的地,消息发送给谁
Destination destination;
//MessageProducer : 消息发送者
MessageProducer producer;
//构造connectionFactory的实例对象,此处采用ActiveMq的实现jar
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost: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("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);
}
}
}
接收者:
package com.liuxian.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://localhost: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("FirstQueue");
consumer = session.createConsumer(destination);
while (true) {
//设置接收者接收消息的时间,为了便于测试,这里谁定为100s
TextMessage message = (TextMessage) consumer.receive(500000);
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) {
}
}
}
}
- ActiveMQ
- ACtiveMQ
- ACtiveMQ
- activeMQ
- ActiveMQ
- activeMQ
- activeMQ
- activemq
- ActiveMQ
- ActiveMQ
- ActiveMQ
- ActiveMQ
- activeMQ
- activemq
- ActiveMQ
- ActiveMQ
- ActiveMQ
- ActiveMQ
- problem中Project'XX'is missing required library:'[路径]\XXX.jar'解决方法
- iOS开发入门:Xcode常用快捷键
- 11个强大的Visual Studio调试小技巧
- C语言模拟试题4
- 一句一笑,笑成蛇精病
- ActiveMQ
- Oracle 执行计划中access 和 filter的区别
- 解题笔记(31)——从数列1,2...n中随意取几个数,使其和等于m
- 反转一个链表。递归算法
- 面向对象中类的构造函数是没有返回值的 或者说是返回值无用
- 无尽弹珠
- OSI七层协议和TCP/IP四层协议之比较
- QT5.3.2+mingw静态编译(生成exe大约14M)
- 如何优雅的管理游戏资源