首先下载ActiveMQ:http://activemq.apache.org/download.html
在windows下启动服务:
activemq.bat start
然后进入admin管理页面:http://127.0.0.1:8161/admin/
账号和密码为:admin/admin
账号可以配置的在conf/users.properties
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class MQSession {
//ActiveMq 的默认用户名
private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
//ActiveMq 的默认登录密码
private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
//ActiveMQ 的链接地址
private static final String BROKEN_URL = ActiveMQConnection.DEFAULT_BROKER_URL;
public static Session getSession()
{
Session createSession = null;
try {
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEN_URL);
Connection createConnection = activeMQConnectionFactory.createConnection();
createConnection.start();
createSession = createConnection.createSession(true, Session.SESSION_TRANSACTED);
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return createSession;
}
}
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
public class ServerActiveMQ {
private Session session;
public ServerActiveMQ()
{
session = MQSession.getSession();
}
public void sendMsg(String queueName)
{
try {
MessageProducer createProducer = session.createProducer(session.createQueue(queueName));
for(int i=1;i<=100;i++)
{
String msg = "ServerActiveMQ send:"+i+"次";
System.out.println(msg);
createProducer.send(session.createTextMessage(msg));
session.commit();
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
new ServerActiveMQ().sendMsg("test");
}
}
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
public class ClientActiveMQ {
private Session session;
public ClientActiveMQ()
{
session = MQSession.getSession();
}
public void getMessage(String queueName){
try {
MessageConsumer createConsumer = session.createConsumer(session.createQueue(queueName));
while(true)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TextMessage msg = (TextMessage)createConsumer.receive();
if(null != msg)
{
msg.acknowledge();
System.out.println("接受服务发送的内容:"+msg.getText());
}
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new ClientActiveMQ().getMessage("test");
}
}
在管理页面http://127.0.0.1:8161/admin/ 点击Queues可以看到相关队列信息。
服务和客户端我这里定义的Queue是test,所以在此通道下数据得以交互
分享到:
相关推荐
Apache ActiveMQ5.8 入门最简单例子 消息队列 Master
JMS、ActiveMQ入门及深入使用的例子,这些例子都是我测试的
Activemq入门实例.pdf
简单消息服务-httpsqs 博文链接:https://sosuny.iteye.com/blog/509846
activemq入门代码,里面有详细的操作步骤,是入门的不错例子,包含基本消息应用方
activeMQ入门到精通
消息队列-activemq入门实例 博客地址:https://blog.csdn.net/u010476739/article/details/100076646
ActiveMQ入门,ActiveMQ介绍和应用案例,提供一个实例(基于spring);
activemq 配置说明与activemq入门讲解
使用activeMQ实现生产者消费者
这是一个activemq应用的简单示例代码,使用maven搭建的.适合刚刚开始学习activemq的程序员
ActiveMQ入门例子,有三个小Demo,有简单的说明,具体下载看项目吧。
一个activemq的简单例子,我只是留个备份,你们想要可以私聊我
ActiveMQ实践入门指南
activeMQ的java简单实例.zipactiveMQ的java简单实例.zip
This is an introduction to the document describing ActiveMQ
ActiveMQ 入门实战(3)--SpringBoot 整合 ActiveMQ(csdn)————程序
ActiveMQ开发实例-2,使用源码,需配合ActiveMQ开发实例系统其他资源使用