ActiveMQ

来源:互联网 发布:in java.library.path 编辑:程序博客网 时间:2024/06/02 02:35

activemq

JMS

Java Message Service - Message Oriented MiddleWare
- asynchronous communication (send - receive)
- pub/sub (one to many)

term

  • Provider
  • Consumer
  • P2P point to point (queue)
  • Pub/Sub publish/subscribe (topic)
  • ConnectionFactory
  • Connection app <–> mqServer
  • Session create with connection
  • Destination create with session

activemq

conf:
activemq.xml、jetty.xml、jetty-realm.properties
data:
message persistence, default use kahadb also can use leveldb or jdbc to mysql etc

message component

head(router) + attribute(selector) + body(5 kind)

messageSelector

message.setIntProperty("xx",1);message.setIntProperty("xx",2);message.setIntProperty("xx",3);----------String condition = "xx >= 2";session.createMessageConsumer(destination,condition);

acknowledgeMode

  • AUTO_ACKNOWLEDGE 1
    when consumer receive message simultaneously sign up
  • CLIENT_ACKNOWLEDGE 2
    with manual sign up when message be handled successfully(in actual uesd)
  • DUPS_OK_ACKNOWLEDGE 3
    never matter

priority

theoretically, not guaranteed the the messages with high priority is comsumed earlier than the lower ones;

  • 0-4 normal, default is 4
  • 5-9 urgent

async receive

Dead loop: MessageConsumer.receive()/receive(long)/receiveNoWait
listener: MessageConsumer.setMessageListener(MessageListener - onMessage)

durable sub

When subscriber offline with reboot or other event, some message will lost because the sub-thread must be always running. To avoid the case, use durable sub to keep the message in MQ server when subscriber online.

  1. modify conf/activemq.xml
<bean id="mqDataSource" class="org.apache.commons.dbcp.BasicDataSource">    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>    <property name="url" value="jdbc:mysql://localhost:3306/activemq?releaseAutoCommit=true"/>    <property name="username" value="root"/>    <property name="password" value="xxx"/>    <property name="maxActive" value="200"/>    <property name="poolPreparedStatements" value="true"/></bean>----------<persistenceAdapter>      <jdbcPersistenceAdapter dataSource="#mqDataSource" /></persistenceAdapter>

ps: check “lib” with mysql-connector-java-5.1.31.jar and commons-dbcp2-2.1.1.jar

code impl(pub/sub)

activemq api:

connection.setClientID("xxx");session.createDurableSubscriber(destination,"xxx");

spring-integrate(deliveryPersistent or deliveryMode, clientId):

<property name="deliveryPersistent" value="true" /> or <property name="deliveryMode" value="2" />CachingConnectionFactory:<property name="clientId" value="Client-A" />DefaultMessageListenerContainer:<property name="clientId" value="Client-A" /><property name="durableSubscriptionName" value="clientA"/>
  • how to use spring-activemq to impl durable *

persist to mysql

use api impl P2P/Pub-Sub

connectionFactory = new ActiveMQConnectionFactory(Consumer.USERNAME, Consumer.PASSWORD,                Consumer.BROKEURL);connection = connectionFactory.createConnection();connection.start();session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);destination = session.createQueue("HelloWorld");destination = session.createTopic("testTopic");messageProducer = session.createProducer(destination);messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);TextMessage message = session.createTextMessage("ActiveMQ Send msg" + i);messageProducer.send(message);session.commit();connecttion.close();messageConsumer = session.createConsumer(destination);messageConsumer.receive(100000);
原创粉丝点击