activeMq的点对点发送

来源:互联网 发布:淘宝网棉衣女装中长款 编辑:程序博客网 时间:2024/05/29 15:23

1. 建立连接工厂

ConnectionFactory factory = new ActiveMQConnectionFactory("system","manager","tcp://localhost:61616");参数1:用户名,默认:ActiveMQConnection.DEFAULT_USER=null;可以根据mq的配置文件自己设置参数2:密码,默认:ActiveMQConnection.DEFAULT_PASSWORD=null;可以根据mq的配置文件自己设置参数3:url,默认:ActiveMQConnection.DEFAULT_BROKER_URL=failover://tcp://localhost:61616;url有几种:

1. TCP:

MQ默认使用的协议,开放性、高效性、可靠性是其主要特点,这个协议可满足一般应用的大部分需求,因此在项目中还是比较常用的。<transportConnector name="nio" uri="nio:localhost:61618" />  

2. NIO:

网络消息的传播效率非常的高。因此,当系统存在高用户量、高并发或网络堵塞时,可考虑用此协议。

3. SSL:

安全套接层协议在MQ目录/conf/activemq.xml文件中修改以下配置Xml代码:<sslContext>   <sslContext keyStore="file:${activemq.base}/conf/broker.ks" keyStorePassword="password"/>   </sslContext>  <transportConnectors>  <transportConnector name="ssl" uri="ssl://localhost:61617"/>  </transportConnectors>  

2. 建立连接

Connection conn = factory.createConnection();

3. 获取session

Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);参数1:是否自动提交参数2:Session.AUTO_ACKNOWLEDGE, Session.CLIENT_ACKNOWLEDGE, Session.DUPS_OK_ACKNOWLEDGE

4. 建立Destination

Destination destination = session.createQueue("queue");

5. 建立MessageProducer

MessageProducer producer = session.createProducer(destination);

6. 产生消息并发送:

ObjectMessage om = session.createObjectMessage(d);producer.send(om);

7. 建立消费者

MessageConsumer consumer = session2.createConsumer(destination2);

8. 注册监听器

consumer.setMessageListener(new MyListener());
发送者完整代码:
<pre name="code" class="java">public class MqSender {private static String JMS_USER_NAME = ActiveMQConnection.DEFAULT_USER;private static String JMS_PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;private static String JMS_URL = ActiveMQConnection.DEFAULT_BROKER_URL;public void send(){Connection conn = null;try{ConnectionFactory factory = new ActiveMQConnectionFactory("system","manager","tcp://localhost:61616");conn = factory.createConnection();conn.start();Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);Destination destination = session.createQueue("queue");MessageProducer producer = session.createProducer(destination);for(int i=0; i<10; i++){Data d = new Data("data"+i);ObjectMessage om = session.createObjectMessage(d);producer.send(om);System.out.println("producer向"+producer.getDestination()+"发送了消息:"+d);Thread.sleep(3000);session.commit();}}catch(Exception e){}finally{if(conn != null){try {conn.close();} catch (JMSException e1) {e1.printStackTrace();}}}}public static void main(String[] args) {MqSender sender = new MqSender();try {sender.send();} catch (Exception e) {e.printStackTrace();}}}



消费者完整代码:

<pre name="code" class="java">public class Receiver {private static String JMS_USER_NAME = ActiveMQConnection.DEFAULT_USER;private static String JMS_PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;private static String JMS_URL = ActiveMQConnection.DEFAULT_BROKER_URL;public void receive(){Connection conn = null;try{ConnectionFactory factory = new ActiveMQConnectionFactory(JMS_USER_NAME,JMS_PASSWORD,JMS_URL);conn = factory.createConnection();conn.start();Session session2 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);Destination destination2 = session2.createQueue("queue");MessageConsumer consumer = session2.createConsumer(destination2);consumer.setMessageListener(new MyListener1());}catch(Exception e){}finally{if(conn != null){try {conn.close();} catch (JMSException e1) {e1.printStackTrace();}}}}public static void main(String[] args) {Receiver re = new Receiver();try {re.receive();} catch (Exception e) {e.printStackTrace();}}}class MyListener1 implements MessageListener{@Overridepublic void onMessage(Message msg) {try {Data d = (Data) ((ObjectMessage)msg).getObject();System.out.println("rec1:"+d);Thread.sleep(500);} catch (Exception e) {e.printStackTrace();}}}


                                             
0 0
原创粉丝点击