ActiveMq 的queue和Topic的例子


package Client;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class DeliverQueue {

public static void main(String[] args) {
String brokerUrl = "tcp://127.0.0.1:61616";
String userName = "admin";
String pswd = "admin";
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();

((ActiveMQConnectionFactory) connectionFactory).setBrokerURL(brokerUrl);
((ActiveMQConnectionFactory) connectionFactory)
.setUserName(userName);

((ActiveMQConnectionFactory) connectionFactory)
.setPassword(pswd);

try {
Connection conn = ((ActiveMQConnectionFactory) connectionFactory).createConnection();

conn.start();

Session jmsSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

Destination destination = jmsSession.createQueue("MONKEY_Q");
MessageProducer producer = jmsSession.createProducer(destination);

Message msg = jmsSession.createTextMessage("Monkey king.");
producer.send(msg);

producer.close();
jmsSession.close();
conn.close();

System.out.println("sent msg :" + msg);


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}




package Client;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
* Hello world!
*
*/
public class ReceiveQueue
{
public static void main( String[] args )
{
String brokerUrl = "tcp://127.0.0.1:61616";
String userName = "admin";
String pswd = "admin";
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();

((ActiveMQConnectionFactory) connectionFactory).setBrokerURL(brokerUrl);
((ActiveMQConnectionFactory) connectionFactory)
.setUserName(userName);

((ActiveMQConnectionFactory) connectionFactory)
.setPassword(pswd);

try {
Connection conn = ((ActiveMQConnectionFactory) connectionFactory).createConnection();

conn.start();

Session jmsSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

Destination destination = jmsSession.createQueue("MONKEY_Q");
MessageConsumer comsumer = jmsSession.createConsumer(destination);

Message msg = comsumer.receive(1000L);

comsumer.close();
jmsSession.close();
conn.close();

System.out.println(msg);


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}
}





package Client;

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.ActiveMQConnectionFactory;

public class ReceiveTopic implements Runnable{
private static final String brokerUrl = "tcp://127.0.0.1:61616";
private static final String userName = "admin";
private static final String pswd = "admin";
private String threadName;

ReceiveTopic(String name){
threadName = name;
}

@Override
public void run() {

ConnectionFactory connFactory;
Connection conn = null;
Session session;

Destination dest;

MessageConsumer consumer;

connFactory = new ActiveMQConnectionFactory(userName,pswd,brokerUrl);
try{

conn = connFactory.createConnection();
conn.start();
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
dest = session.createTopic("AllWorld");

consumer = session.createConsumer(dest);

while(true){
TextMessage message = (TextMessage)consumer.receive(100*1000);
if(null != message){
System.out.println("Thread " + threadName + " has reveived the message . " + message.getText());
}else{
continue;
}
}

}catch(Exception ex){
ex.printStackTrace();
}finally{
try{
if(null != conn){
conn.close();
}
}catch(Throwable ignore){

}
}

}

public static void main(String[] args) {

ReceiveTopic r1 = new ReceiveTopic("Thread 1 ");
ReceiveTopic r2 = new ReceiveTopic("Thread 2 ");
ReceiveTopic r3 = new ReceiveTopic("Thread 3 ");

Thread thread1 = new Thread(r1);
Thread thread2 = new Thread(r2);
Thread thread3 = new Thread(r3);

thread1.start();
thread2.start();
thread3.start();

}


}







package Client;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class SendToTopic {
private static final String brokerUrl = "tcp://127.0.0.1:61616";
private static final String userName = "admin";
private static final String pswd = "admin";
private static final int SEND_NUMBER = 5;

public static void sendMessage(Session session, MessageProducer producer)
throws Exception
{
for(int i=1; i <= SEND_NUMBER ; i++){
TextMessage message = session.createTextMessage("I am new a message, number " + i + ".");

System.out.println("Send message " + i );
producer.send(message);

}
}

public static void main(String[] args) {
ActiveMQConnectionFactory connFactory ;
Connection conn = null;
Session session;

Destination dest;

MessageProducer producer;
connFactory = new ActiveMQConnectionFactory(
userName,
pswd,
brokerUrl);


try{
conn = connFactory.createConnection();
conn.start();

session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);

dest = session.createTopic("AllWorld");

producer = session.createProducer(dest);

producer.setDeliveryMode(DeliveryMode.PERSISTENT);

sendMessage(session, producer);

session.commit();


}catch(Exception ex){
ex.printStackTrace();
}finally{
try{
if(null != conn){
conn.close();
}
}catch(Throwable ignore){

}
}


}

}




[quote]
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>

<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
</dependency>

<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</dependency>

<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>

<dependency>
<groupId>opensymphony</groupId>
<artifactId>quartz</artifactId>
</dependency>

<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
</dependency>

<dependency>
<groupId>com.****.****</groupId>
<artifactId>****-framework-core</artifactId>
<version>1.3.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>com.****.****</groupId>
<artifactId>****-framework-activemq</artifactId>
<version>1.3.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.2</version>
</dependency>
</dependencies>

[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值