ActiveMq初探一

1、下载ActiveMQ

 http://activemq.apache.org/

2)

Grab yourself a Download, try our Getting Started Guide, surf our FAQ or start Contributing and join us on our Discussion Forums

点击Download

 3)

See the Download Archives for all time releases.

点击Download Archives

4)

点击


5)

点击 apache-activemq-5.9.0-bin.zip

说明::我是在Windows下研究的,故选择Windows版本

3 解压zip文件得到ActiveMq目录接口


说明:与Tomcat类似。

4.在Eclipse中新建一个Demo项目,导入的jar包如图:


Sender端代码:

package server;


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 ActiveServer extends Thread{


private static final int MAX_MESSAGE = 5;

@Override
public void run(){
ConnectionFactory connFac = null;
Connection conn = null;
Session session = null;
Destination destination = null;
MessageProducer producer = null;
connFac = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61236");
try {
conn = connFac.createConnection ( );
conn.start ( );
session = conn.createSession ( Boolean.TRUE , Session.AUTO_ACKNOWLEDGE );
destination = session.createQueue ( "MyQueue" );
producer = session.createProducer ( destination );
producer.setDeliveryMode ( DeliveryMode.NON_PERSISTENT );
sendMessage(session,producer);
session.commit ( );
} catch ( JMSException e ) {
e.printStackTrace();
}finally{
if(conn != null){
try {
conn.close ( );
} catch ( JMSException e ) {
e.printStackTrace();
}
}
}
}


private void sendMessage (
Session session , MessageProducer producer ) throws JMSException {
for(int i =MAX_MESSAGE;i>0 ; i--){
TextMessage message = session.createTextMessage ( "Activemq 消息" + i);
System.out.println ( "开始发送:" +i);
producer.send ( message );
}
}

}


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 ActiveServer extends Thread{


private static final int MAX_MESSAGE = 5;

@Override
public void run(){
ConnectionFactory connFac = null;
Connection conn = null;
Session session = null;
Destination destination = null;
MessageProducer producer = null;
connFac = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61236");
try {
conn = connFac.createConnection ( );
conn.start ( );
session = conn.createSession ( Boolean.TRUE , Session.AUTO_ACKNOWLEDGE );
destination = session.createQueue ( "MyQueue" );
producer = session.createProducer ( destination );
producer.setDeliveryMode ( DeliveryMode.NON_PERSISTENT );
sendMessage(session,producer);
session.commit ( );
} catch ( JMSException e ) {
e.printStackTrace();
}finally{
if(conn != null){
try {
conn.close ( );
} catch ( JMSException e ) {
e.printStackTrace();
}
}
}
}


private void sendMessage (
Session session , MessageProducer producer ) throws JMSException {
for(int i =MAX_MESSAGE;i>0 ; i--){
TextMessage message = session.createTextMessage ( "Activemq 消息" + i);
System.out.println ( "开始发送:" +i);
producer.send ( message );
}
}

}

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 ActiveServer extends Thread{


private static final int MAX_MESSAGE = 5;

@Override
public void run(){
ConnectionFactory connFac = null;
Connection conn = null;
Session session = null;
Destination destination = null;
MessageProducer producer = null;
connFac = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61236");
try {
conn = connFac.createConnection ( );
conn.start ( );
session = conn.createSession ( Boolean.TRUE , Session.AUTO_ACKNOWLEDGE );
destination = session.createQueue ( "MyQueue" );
producer = session.createProducer ( destination );
producer.setDeliveryMode ( DeliveryMode.NON_PERSISTENT );
sendMessage(session,producer);
session.commit ( );
} catch ( JMSException e ) {
e.printStackTrace();
}finally{
if(conn != null){
try {
conn.close ( );
} catch ( JMSException e ) {
e.printStackTrace();
}
}
}
}


private void sendMessage (
Session session , MessageProducer producer ) throws JMSException {
for(int i =MAX_MESSAGE;i>0 ; i--){
TextMessage message = session.createTextMessage ( "Activemq 消息" + i);
System.out.println ( "开始发送:" +i);
producer.send ( message );
}
}

}


收端代码:

package client;


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 ActiveClient  extends Thread{



@Override
public void run(){
ConnectionFactory connFac = null;
Connection conn = null;
Session session = null;
Destination destination = null;
MessageConsumer consumer;
connFac = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61236"
);
try {
conn = connFac.createConnection ( );
conn.start ( );
session = conn.createSession ( Boolean.FALSE , Session.AUTO_ACKNOWLEDGE );
destination = session.createQueue ( "MyQueue" );
consumer = session.createConsumer ( destination );
   while(true){
    TextMessage message = (TextMessage) consumer.receive ( 100000l );
    if(message != null){
    System.out.println("收到消息:"+message.getText ( ));
    }else{
    break;
    }
   }
} catch ( JMSException e ) {
e.printStackTrace();
}finally{
if(conn != null){
try {
conn.close ( );
} catch ( JMSException e ) {
e.printStackTrace();
}
}
}
}

}


测试类代码:

import client.ActiveClient;
import server.ActiveServer;


public class DemoTest {


public  static void main(String[] args){
ActiveServer server = new ActiveServer();
server.run ( );
ActiveClient client = new ActiveClient();
client.run ( );
}

}

结果:

log4j:WARN No appenders could be found for logger (org.apache.activemq.transport.WireFormatNegotiator).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
开始发送:5
开始发送:4
开始发送:3
开始发送:2
开始发送:1
收到消息:Activemq 消息5
收到消息:Activemq 消息4
收到消息:Activemq 消息3
收到消息:Activemq 消息2
收到消息:Activemq 消息1

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值