消息机制
一、mq
1、发送
创建mq连接工厂 ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);
创建连接 Connection connection = factory.createConnection();
创建session Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
创建消息生成者 MessageProducer producer = session.createProducer(session.createQueue(subscribe));
消息发送 producer.send(message)
2、接收
创建消息消费者 MessageConsumer consumer = session.createConsumer(session.createTopic(subscribe));
3、查看消息的生成者和消费者
./activemq query -QTopic="com.ailk.fba.userdig.msgdistpatcher.bi.gprs.beha" --view Name,EnqueueCount,DequeueCount,ConsumerCount,ProducerCount
4、配置文件
打开conf/activemq.xml文件
<transportConnectors>
<transportConnector name="openwire" uri="tcp://10.42.220.72:61618"discoveryUri="multicast://default"/>
</transportConnectors>
打开conf/jetty.xml文件
<bean id="jettyPort"class="org.apache.activemq.web.WebConsolePort"init-method="start">
<property name="port" value="8162"/>
</bean>
5、优化措施
生产者启动事务快,消费者不启动事务快 Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
消息非持久化 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
Non-persistent传递消息比Persistents传递消息速度更快,原因如下:
1)Non-persistent发送消息是异步的,Producer不需要等待Consumer的receipt消息。
2)Persisting 传递消息是需要把消息存储起来。然后在传递,这样很慢 。
ActiveMQ使用TCP协议时 tcpNoDelay=默认是false ,设置为true可以提高性能
启用 NIO Transport Connector