activeMQ 消息发送与接收(二)

[b]三、发布/订阅模式[/b]
[b]3.1、消息发送[/b]


import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
* 发布/订阅模式--消息发送
* 单连接、无持久化、无事务、消息接收自动确认
*/
public class TopicPublishers {

public static void main(String[] args) {
Connection conn = null;
try{
ActiveMQConnectionFactory connFactory =
new ActiveMQConnectionFactory("admin","showlike",
"tcp://localhost:61616");
conn = connFactory.createConnection();
conn.start();

Session session = conn.createSession(Boolean.FALSE,
Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("FirstTopic");

MessageProducer producer = session.createProducer(topic);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

long startTime = System.currentTimeMillis();
int i = 1;
do{
TextMessage message = session.createTextMessage();
message.setText(i+" , message_"+System.currentTimeMillis());
producer.send(message);
System.out.println("Sent message: "+message.getText());

// Thread.sleep(1000);
i++;
}while(i<=100000);

long endTime = System.currentTimeMillis();
long diff = endTime - startTime;
long diffSeconds = diff / 1000;

System.out.print("top publishers total time: "+diffSeconds + " seconds.");
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn != null){
try {
conn.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}

}


[b]3.2、接收消息[/b]


import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
* 发布/订阅模式--消息接收
* 单连接、无持久化、无事务、消息接收自动确认
*/
public class TopicSubscribers {

public static void main(String[] args) {
Connection conn = null;
try{
ActiveMQConnectionFactory connFactory =
new ActiveMQConnectionFactory("admin","showlike",
"tcp://localhost:61616?jms.prefetchPolicy.queuePrefetch=1");
conn = connFactory.createConnection();
conn.start();

Session session = conn.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("FirstTopic");

MessageConsumer consumer = session.createConsumer(topic);

long startTime = System.currentTimeMillis();
int i = 1;
do{
consumer.setMessageListener(new MessageListener(){
public void onMessage(Message message) {
TextMessage tm = (TextMessage) message;
try {
System.out.println("Received message: " + tm.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
i++;
}while(i<=100000);

long endTime = System.currentTimeMillis();
long diff = endTime - startTime;
long diffSeconds = diff / 1000;

System.out.print("top publishers total time: "+diffSeconds + " seconds.");
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn != null){
try {
conn.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值