430 likes | 1.71k Views
Introduction to ActiveMQ. Prepared for the Austin Java Users Group by Gerald Cantor. Introduction ActiveMQ and Messaging ActiveMQ Components Basic Administration Enterprise Features of ActiveMQ Camels and Mules Questions and Answers. Agenda. Most popular open source message broker
E N D
IntroductiontoActiveMQ Prepared for the Austin Java Users Group by Gerald Cantor
Introduction • ActiveMQ and Messaging • ActiveMQ Components • Basic Administration • Enterprise Features of ActiveMQ • Camels and Mules • Questions and Answers Agenda
Most popular open source message broker • 100% Java Implementation • Available for download at http://activemq.apache.org • Windows and Unix/Linux distributions available • Numerous connection protocols • Wide range of language clients • Can be run stand alone or embedded in JEE container • Full support of JMS 1.1 specification • Full support of J2EE 1.4 specification • Simple browser based administration • Advancedfeatures • Three simple steps to get going • Download • Extract (.zip or .tgz) • Startup(.bat or .sh) Introduction
Recommended Reading Introduction Recommended Web Sites • http://activemq.apache.org • http://fusesource.com/resources/video-archived-webinars/ • http://www.slideshare.net/search/slideshow?searchfrom=header&q=activemq
Point to Point Messaging Queue Producer Consumer ActiveMQ and Messaging Publish Subscribe Messaging Subscriber Topic Publisher Subscriber Subscriber
Simple Consumer public class SimpleConsumer { private static final Log LOG = LogFactory.getLog(SimpleConsumer.class); private static final Boolean NON_TRANSACTED = false; private static final String CONNECTION_FACTORY_NAME = "myJmsFactory"; private static final String DESTINATION_NAME = "queue/simple"; private static final intMESSAGE_TIMEOUT_MILLISECONDS = 120000; public static void main(String args[]) { Connection connection = null; try { // JNDI lookup of JMS Connection Factory and JMS Destination Context context = new InitialContext(); ConnectionFactory factory = (ConnectionFactory) context.lookup(CONNECTION_FACTORY_NAME); Destination destination = (Destination) context.lookup(DESTINATION_NAME); connection = factory.createConnection(); connection.start(); Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(destination); LOG.info("Start consuming messages from " + destination.toString() + " with " + MESSAGE_TIMEOUT_MILLISECONDS + "ms timeout"); // Synchronous message consumer inti = 1; while (true) { Message message = consumer.receive(MESSAGE_TIMEOUT_MILLISECONDS); if (message != null) { if (message instanceofTextMessage) { String text = ((TextMessage) message).getText(); LOG.info("Got " + (i++) + ". message: " + text); } } else { break; } } . . . ActiveMQ and Messaging
Simple Producer public class SimpleProducer { private static final Log LOG = LogFactory.getLog(SimpleProducer.class); private static final Boolean NON_TRANSACTED = false; private static final long MESSAGE_TIME_TO_LIVE_MILLISECONDS = 0; private static final intMESSAGE_DELAY_MILLISECONDS = 100; private static final intNUM_MESSAGES_TO_BE_SENT = 1000; private static final String CONNECTION_FACTORY_NAME = "myJmsFactory"; private static final String DESTINATION_NAME = "queue/simple"; public static void main(String args[]) { Connection connection = null; try { // JNDI lookup of JMS Connection Factory and JMS Destination Context context = new InitialContext(); ConnectionFactory factory = (ConnectionFactory) context.lookup(CONNECTION_FACTORY_NAME); Destination destination = (Destination) context.lookup(DESTINATION_NAME); connection = factory.createConnection(); connection.start(); Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(destination); producer.setTimeToLive(MESSAGE_TIME_TO_LIVE_MILLISECONDS); for (inti = 1; i <= NUM_MESSAGES_TO_BE_SENT; i++) { TextMessagemessage = session.createTextMessage(i + ". message sent"); LOG.info("Sending to destination: " + destination.toString() + " this text: '" + message.getText()); producer.send(message); Thread.sleep(MESSAGE_DELAY_MILLISECONDS); } . . . ActiveMQ and Messaging
Consume from multiple destinations via wildcards • ActiveMQ destinations follow a hierarchical naming convention • usergroups.java.austin • usergroups.java.miami • usergroups.ruby.austin • meetup.marketing.austin • Three special characters using in naming ActiveMQ destinations • . Used to separate elements in a destination name • * Used to match one element • > Used to match one or all trailing elements ActiveMQ and Messaging
Consume from multiple destinations via wildcards • usergroups.java.austin ActiveMQ and Messaging • usergroups.java.miami Consumer • usergroups.java.* • usergroups.ruby.austin • meetup.marketing.austin
Consume from multiple destinations via wildcards • usergroups.java.austin ActiveMQ and Messaging • usergroups.java.miami Consumer • usergroups.> • usergroups.ruby.austin • meetup.marketing.austin
Consume from multiple destinations via wildcards • usergroups.java.austin ActiveMQ and Messaging • usergroups.java.miami Consumer • *.*.austin • usergroups.ruby.austin • meetup.marketing.austin
Send to multiple destinations via composite destinations • usergroups.java.austin ActiveMQ and Messaging • usergroups.java.miami Producer • usergroups.java.austin, • meetup.marketing.austin • usergroups.ruby.austin • meetup.marketing.austin • Composite destinations include • queues and topics or a mix of both
Send to multiple destinations via composite destinations • usergroups.java.austin ActiveMQ and Messaging • usergroups.java.miami • usergroups.java.austin, • usergroups.ruby.austin, • meetup.marketing.austin Producer • usergroups.ruby.austin • meetup.marketing.austin
Transport Connectors • Used for client to broker connectivity • Variety of protocols • URI format • Network Connectors • Used for broker to broker connectivity • Variety of protocols • URI format • Message Store • Used for message persistence • Used for internal ActiveMQ bookkeeping • Variety of backing stores • Security Plug-ins • Authentication • Authorization • Certificate-based ActiveMQ Components
Transport Connectors • tcp://activemq.gcantor.com:61616?trace=true • vm://localhost?brokerConfig=xbean:activemq.xml • static:(tcp://activemq.gcantor.com:61616,tcp://activemq.tcantor.com:61616) ActiveMQ Components
Transport Configuration – single or multiple . . . <transportConnectors> <transportConnector name="tcp“ uri="tcp://localhost:61616?trace=true"/> </transportConnectors> . . . ActiveMQ Components . . . <transportConnectors> <transportConnector name="tcp“ uri="tcp://localhost:61616?trace=true"/> <transportConnector name=“nio“ uri=“nio:localhost:61618?trace=true"/> </transportConnectors> . . .
Network Connectors ActiveMQ Components
Network Connector Configuration . . . <networkConnectors> <networkConnector uri="static:(tcp://localhost:61616)“ duplex="true"> </networkConnector> </networkConnectors> . . . ActiveMQ Components . . . <networkConnectors> <networkConnectoruri="multicast://default“ dynamicOnly="true" networkTTL="3" prefetchSize="1" decreaseNetworkConsumerPriority="true" /> </networkConnectors> . . . <transportConnectors> <transportConnector name="openwire" uri="tcp://0.0.0.0:61616" discoveryUri="multicast://default" /> </transportConnectors>
Message Store • KahaDB • File based, transactional store • Optimized for storing messages • Recommended message store • AMQ Message Store • File based, transactional store • Optimized for throughput • Not a good choice when using thousands of queues • JDBC Message Store • Messages stored in relational database • Many driver options • Useful for when database is already part of infrastructure • Memory Message Store • Messages stored in memory • Very, very fast • Does not survive broker shutdown or crashes ActiveMQ Components
Message Store Configuration – simple / complex . . . <persistenceAdapter> <kahaDB directory="${activemq.base}/data/kahadb"/> </persistenceAdapter> . . . ActiveMQ Components . . . <persistenceAdapter> <amqPersistenceAdapter directory=“target/Broker2-data/activemq-data” syncOnWrite=“true” indexPageSize=“16kb” indexMaxBinSize=“100” maxFileLength=“10mb” /> </persistenceAdapter> . . .
Security Plug-ins • Authentication • Simple • handles credentials directly in XML configuration or via properties file • requires extra level of security to protect credentials • JAAS • implements the Java Authentication and Authorization Service API • providing more full featured and flexible authentication • can be used to leverage certificate-based authentication ActiveMQ Components
Security Plug-ins • Authorization • Destination level • supports 3 levels of access: read/write/admin • fairly easy to use, but coarse grained • Can be combined with certificate-based authentication • Message level • very granular but is a purely programmatic approach • you have full control • Complete custom security via plug-in architecture • Driver’s choice • Most complex approach, but you can get exactly what you want • Combination of programming and proper configuration setup ActiveMQ Components
Simple Browser-based administration • CRUD Operations on queues / topics • Simple message send • Simple durable subscribers management • Basic views of connections, network configurations • Basic reporting via simple graphs ActiveMQ Administration
Queue View ActiveMQ Administration
JMX Administration • Fully exposed management via JMX API • Accessible via JMX GUI client, such as jconsole • Accessible via JMX programmatic API • Ability to gather very detailed information • Broker statistics • Queue / Topic attributes • In-flight transactions • Important performance indicators • More extensive operations • CRUD-based operations • Specialized operations ActiveMQ Administration
jconsole view ActiveMQ Administration
Fail Over Configuration • Shared Nothing Master / Slave • Master broker and slave broker have independent message stores • Master will replicate information to slave, prior to deeming message process “done” • If master dies, slave can either shutdown or become new master • Simple configuration but incurs extra overhead • Master can have only 1 slave • Manual intervention necessary when master dies • manually making slave master or adding a new slave • Shared Master / Slave • Master broker and slave broker share a message store • No limit to number of slave brokers • Brokers compete for message store lock • Distributed file lock semantics required (e.g. SAN, NFS4) • When master dies, the first slave that obtains the lock becomes the new master ActiveMQ Enterprise Features
Network of Brokers • Brokers can be configured to form network topologies • Messages are stored and forwarded across the broker networks • Allows for horizontal scaling • Care needs to be used when defining networks • Two aspects to networking brokers • What’s passed between brokers • How the networks are formed • A broker’s active and durable consumer destinations are shared • Info can be filtered • Choice of unidirectional message exchange or duplex exchange • Networks can be static or dynamic • Dynamic networks require advisory support enabled • Some limitation with dynamic broker discovery ActiveMQ Enterprise Features
Fault Tolerant Broker Network • Combines the techniques of master / slave brokers and network of brokers • Host 2 • Host 4 • Host 1 • Host 3 ActiveMQ Enterprise Features Master A1 Slave A2 Master C1 Slave C2 Slave B2 Master B1 Slave D2 Master D1 • failover://(tcp://brokerA,tcp://brokerB,tcp://brokerC,tpc://brokerD)?randomize=true Producer
Enterprise Integration Pattern Frameworks • Enterprise Integration Patterns build heavily on messaging • Apache Camel and MuleSoft Mule ESB are comprehensive EIP frameworks • Each provides solid implementations of key EIPs • Wire Tap • Recipient List • Content Based Router • Simple and complex messaging integrations can often be reduced to a few lines of DSL • Robust messaging routing ActiveMQ Camels and Mules !?!?
References ActiveMQ Camels and Mules !?!?
Camel Example Consume messages from queue://test.camel-queue and write to a file under test directory Take files under test directory and publish to topic://test.camel-topic ActiveMQ Camels and Mules !?!? <!-- lets configure the ActiveMQ JMS broker server to listen on TCP 61616 --> <broker:broker id="broker" useJmx="false" persistent="false" brokerName="localhost"> <broker:transportConnectors> <broker:transportConnector name="tcp" uri="tcp://localhost:61616"/> </broker:transportConnectors> </broker:broker> <!-- lets configure the Camel ActiveMQ to use the ActiveMQ broker declared above --> <bean id="test-jms" class="org.apache.activemq.camel.component.ActiveMQComponent"> <property name="brokerURL" value="tcp://localhost:61616"/> </bean> <camel:camelContext id="camelContext"> <camel:route> <camel:fromuri="test-jms:queue:test.camel-queue" /> <camel:touri="file://test" /> </camel:route> <camel:route> <camel:fromuri="file://test" /> <camel:touri="test-jms:topic:test.camel-topic" /> </camel:route> </camel:camelContext> </beans>
IntroductiontoActiveMQ Prepared for the Austin Java Users Group by Gerald Cantor