#ActiveMQ seminar
##Task 1) Preparing environment
- download JBoss A-MQ (registration required)
- If you have JBoss Fuse downloaded you can skip this step
- Extract downloaded file
- add default user by commenting out last line in ${INSTALLATION-FOLDER}/etc/users.properties
- start JBoss A-MQ by executing:
- ${INSTALLATION-FOLDER}/bin/amq - for unix environment
- ${INSTALLATION-FOLDER}\bin\amq.bat - for windows environment
- for fuse you need to use ${INSTALLATION-FOLDER}/bin/fuse (fuse.bat for windows)
- use maven to build seminar code:
mvn clean install
##Task 2) Discovering useful tools
- get familiar with source code of seminar
- get familiar with activemq configuration located in ${INSTALLATION-FOLDER}/etc/activemq.xml
- execute Main class using maven:
mvn exec:java
- execute client using ${INSTALLATION-FOLDER}/bin/client
- use activemq:dstat command to verify that there are messages in the queue seminar.jobs
- you can also use console which was shown when you started A-MQ
- navigate to http://localhost:8181/ in web browser and log in.
- open one of the message in seminar.jobs queue
- delete or purge queue seminar.jobs
- you can use hawtio or console (activemq:purge seminar.jobs)
##Task 3) Implementing consumer
- build and execute application with argument -p only:
mvn exec:java -Dexec.args="-p"
- it will launch producer only so it will only send messages to queue
- implement Consumer#consumeMessages() method using synchronous receive (see comments in consumer for help)
- execute every received job
- build and execute app with -c argument only:
mvn clean install exec:java -Dexec.args="-c"
- it will run consumer and should start consuming messages (you should see job execution on stdout)
##Task 4) Changing consumer to asynchronous
- change consumer that it will use asynchronous receive:
- Create connection in the Main class and pass it to Consumer using constructor (you will have to modify consumer class also)
- change consumer class so it use asynchronous receive * Hint: remove receive loop and add message listener instead * Hint 2: do not close connection in the Consumer class.
- add waitUntilDestinationEmpty() method call to Main class after consumer.ConsumeMessages();
- build and execute app with '-c -p' arguments:
mvn clean install exec:java -Dexec.args="-c -p"
- it should start producer first and then consumer
##Task 5) Message selectors
- Extend producer to put duration into Message property named DURATION.
- Extend consumer so that you can pass message selector (String)
- change consumeMessages method so that it consumes only messages according to selector
- In Main class create two consumers one will consume only jobs short duration (1-5) and other with long duration (6-10)
- run example app with more messages:
mvn clean install exec:java -Dexec.args="-c -p --messageCount 6"
##Task 6) Publis/Subscribe
- change both consumer and producer so it uses topic instead of queue
- launch infinite consumers using:
mvn clean install exec:java -Dexec.args="-c"
- launch producer and verify that all consumers receive message:
mvn clean install exec:java -Dexec.args="-c -m 5"
##Task 7) MQTT
- Add transport connector to activemq for mqtt on port 1883 (edit conf file etc/activemq.xml)
- Finish method produceMessages in class MqttProducer to publish message into topic
- do not forget to configure username and password for mqtt
- mqtt use different destination naming so . is converted to / See documentation)
- Activemq converts MQTT message to JMS Bytes message so change consumer to convert bytes content to String
- run consumer
mvn clean install exec:java -Dexec.args="-c -d mqtt.topic"
- run producer
mvn clean install exec:java -Dexec.args="-p -u tcp://localhost:1883 -d mqtt/topic"
##Task 8) Network of Brokers
- Download Apache ActiveMQ 5.12
- extract file and start activemq using:
- ${INSTALLATION-FOLDER}/bin/activemq start - for unix environment
- ${INSTALLATION-FOLDER}\bin\activemq - for windows environment
- We will call this broker A
- check by executing consumer and producer that it works:
mvn clean install exec:java -Dexec.args="-p -c -u tcp://localhost:61616 -d test"
- Stop activemq broker A and make a copy of activemq folder (Broker B)
- navigate to ${B_FOLDER} and edit conf/activemq.xml:
- delete all transport connectors
- add single openwire transport connector listening on port localhost:61617
- navigate to $B_FOLDER} and edit conf/jetty.xml:
- change port to 8162: Line change to
- Add network connector to broker A which will connect to broker B. (edit ${A-FOLDER}/conf/activemq.xml)
- Start both broker A and broker B
- Send messages to broker A:
mvn clean install exec:java -Dexec.args="-p -u tcp://localhost:61616 -d example.network"
- Attach consumer to broker B:
mvn clean install exec:java -Dexec.args="-c -u tcp://localhost:61617 -d example.network"
- consumer should receive messages which was sent to broker A.
- Stop all running brokers
- remove network connector from broker A
- change kahaDB directory to point on some location outside of activemq installation in both brokers A and B:
- do not forget to add lockKeepAlivePeriod to kahaDB (see: JBoss A-MQ documenation)
- Start first broker A and then broker B (only one should expose transport connector)
- Start producer to broker B (it should throw exception since C is slave)
- Start producer with failover transport produce message broker A:
mvn clean install exec:java -Dexec.args="-p -u failover:(tcp://localhost:61616,tcp://localhost:61617) -d example.masterslave"
- stop broker A
- See that producer reconnects to broker B and continue delivery.