ActiveMQ

Name of Innovation

ActiveMQ

February 2, 2017 Message Brokers Tutorial 1

ActiveMQ

  • Open terminal and check the all files
ls /home/woir
  • Start the Activemq
/home/woir/stop_all.sh
/home/woir/start_activemq.sh
  • You should see something similar as below

start

 

activemq

 

  • Click on the Manage ActiveMQ broker
  • Enter the username and password as admin
  • You should see something similar as below

login

 

  • Click on the Queues tab
  • After clicking on queues tab you will see all list of queus
  • You should see something similar as below

queues

 

  • If you want to create a new queue you can create
  • Enter the new Queue name in the above input box and click on create button
  • From Queues List page, You can click on any queue’s name, then you can see the all the messages from  that queue’s name
  • You should see something similar as below

messages

Create a Message manually from web

  • If the destination queue exists, then from the queue list page, click the Send To link. If the destination queue does not exists, you can click Send from the menu, and give a name of the destination, the queue will be create automatically when sending message.

send

 

  • Type the text content, then click send 

send msg

 

  • Click on the message Id, then you can see the details of the message. A message is composed by message header and message body.
  • You should see something similar as below

mssg _details

 

Delete a specified message in a Queue

  • From the message list of the Queue, click Delete to remove message.

delete

 

Delete all messages of a queue

  • From the Queue list, click Purge.

all delete

Deleting the Queue

  • From the Queue list, click on Delete .

queue delete

 

Generating messages programmatically

Producers
Send message to queue “woir”
  • Send one message to queue name “woir”
curl -u admin:admin -d "body='Hello World 1 '" "http://localhost:8161/api/message/woir?type=queue&clientId=consumerA"
  • Send second message
curl -u admin:admin -d "body='Hello World 2'" "http://localhost:8161/api/message/woir?type=queue&clientId=consumerA"

Send third message

curl -u admin:admin -d "body='Hello World 3'" "http://localhost:8161/api/message/woir?type=queue&clientId=consumerA"
Send message to queue “college”

Send one message to queue name “college”

curl -u admin:admin -d "body='Hello World 1 '" "http://localhost:8161/api/message/college?type=queue&clientId=consumerA"
  • Send second message
curl -u admin:admin -d "body='Hello World 2'" "http://localhost:8161/api/message/college?type=queue&clientId=consumerA"
  • Send third message
    curl -u admin:admin -d "body='Hello World 3'" "http://localhost:8161/api/message/college?type=queue&clientId=consumerA"
  • Check the messages from GUI ( You should see something similar as below)

msg create

Consumers

Read Messages from “woir” queue

From command line type following –

  • Read First Message
curl -u admin:admin -X delete "http://localhost:8161/api/message/woir?type=queue&clientId=consumerA"
  • Read Second Message
curl -u admin:admin -X delete "http://localhost:8161/api/message/woir?type=queue&clientId=consumerA"

 

  • Read Third Message

 

  • curl -u admin:admin -X delete "http://localhost:8161/api/message/woir?type=queue&clientId=consumerA"

Read Messages from “college” queue

From command line type following –

  • Read First Message
curl -u admin:admin -X delete "http://localhost:8161/api/message/college?type=queue&clientId=consumerA"
  • Read Second Message
curl -u admin:admin -X delete "http://localhost:8161/api/message/college?type=queue&clientId=consumerA"
  • Read Third Message
  • curl -u admin:admin -X delete "http://localhost:8161/api/message/college?type=queue&clientId=consumerA"
Observe the Order of receiving messages – they must come in the FIFO order.
  • You should see something similar as below

consumer

  • For stopping the ActiveMQ run this command
/home/woir/stop_activemq.sh

 

  • Hands-on interaction
    • sudo pip install stompy
  • Paste following code in your home area ( say p2.py )
  • Run command –
    • python p2.py -q “/queue/test” -P -n 1 -m “WOIR Message One”   ( producing messages  to queue)
    • python p2.py -q “/queue/test” -c -n 5
    • python p2.py -q “/topic/test” -P -n 1 -m “WOIR Message One”   ( producing messages to topic )
    • python p2.py -q “/topic/test” -c -n 5

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
import sys
import itertools
from stompy import Client as StompClient
from itertools import count
from optparse import OptionParser

def callback_func(message):
	print("Using Callback Function")
	print(message.headers.get('message-id'))
	print(message.body)
	stomp.ack(message)

def consume(queue, num=None, callback=None):

	stomp.subscribe(queue, ack="client")

	def _handle_message(frame):
		print(frame.headers.get("message-id"))
		print(frame.body)
		stomp.ack(frame)

	# if num is not set, iterate forever.
	it = xrange(0, num) if num else itertools.count()

	try:
		for i in it:
			if callback:
				stomp.get(callback=callback_func)
			else:
				frame = stomp.get()
				_handle_message(frame)
	finally:
		stomp.disconnect()


def produce(queue, num=1000, message="Hello Friend"):

	for i in xrange(0, num):
		print("Message #%d for message = %s " % (i, message))
		this_frame = stomp.put("Testing %d for %s" % (i,message),
							   destination=queue,
							   persistent=True)
		print("Receipt: %s" % this_frame.headers.get("receipt-id"))

	stomp.disconnect()


if __name__ == '__main__':
	parser = OptionParser()
	parser.add_option('-H', '--host', action='store', default="localhost",
					  type='string', dest='host', help='hostname')
	parser.add_option('-p', '--port', action='store',
					  type='int', dest='port', help='port', default=61613)
	parser.add_option('-q', '--queue', action='store',
					  type='string', dest='queue', help='destination queue')
	parser.add_option('-P', '--produce', action='store_true',
					  default=False, dest='produce', help='produce messages')
	parser.add_option('-c', '--consume', action='store_true',
					  default=False, dest='consume', help='consume messages')
	parser.add_option('-C', '--use-callback', action='store_true',
					  default=False, dest='callback',
					  help='send retrieved message to python callable')
	parser.add_option('-n', '--number', action='store',
					  type='int', dest='number', default="100",
					  help='produce or consume NUMBER messages')
	parser.add_option('-m', '--message', action='store',
					  type='string', dest='message', default="Hello friend",
					  help='Type the message you want to send')

	options, args = parser.parse_args()

	if not options.queue:
		print("Queue name is required!")
		parser.print_help()
		sys.exit(1)

	stomp = StompClient(options.host, options.port)
	# optional connect keyword args "username" and "password" like so:
	# stomp.connect(username="user", password="pass")
	stomp.connect()

	if options.produce:
		print("PRODUCING")
		#produce("/topic/hello", options.number, options.message)
		produce(options.queue, options.number, options.message)
	elif options.consume:
		consume(options.queue, options.number, options.callback)
		#consume("/topic/hello", options.number, options.callback)

One Response

Comments are closed.