1、下载ActiveMQ
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