Capítulo 3. El sacramento de la reconciliación como encuentro
3.2. El sacramento de la reconciliación como encuentro con la comunidad eclesial
The JMS API is based on Java and it is a part of the Java platform, which means that Java must be used in order to access the ActiveMQ in a native way. However, some programmers might prefer to use other programming languages to access the ActiveMQ. Thus, ActiveMQ also supports a language agnostic protocol called STOMP. This protocol allows the ActiveMQ to be accessed by different programming languages, rather than just through the use of Java.
In this section, we will use popular script programming language Python as an example to demonstrate how to publish and receive VOEvent packets to and from the
“moa.voevent” topic in ActiveMQ.
The Python version of STOMP API is a necessary component that needs to be downloaded in order to achieve the above goal. The STOMP official website provides a list of STOMP APIs that correspond to different programming languages. In our example, the API we use is called stompest, it is a full featured STOMP API typically designed for Python. This API can also be found on the STOMP official website. We can write a Python program to access the ActiveMQ via the STOMP protocol after the API has been downloaded.
Note that the STOMP protocol uses a different port to the Openwire protocol (Openwire is the default protocol used by ActiveMQ), thus, the port number must be correctly specified in the codes. Otherwise, a connection exception will be thrown when the application attempts to access ActiveMQ via the STOMP protocol. The code example 14 illustrates Python versions of VOEvent subscriber:
132
1 from stompest.config import StompConfig
2 from stompest.protocol import StompSpec
3 from stompest.sync import Stomp
4 port = '61613'
5 config = StompConfig('tcp://' + hostname + ':' + port)
6 channel = '/topic/moa.voevent'
7 client = Stomp(config)
8 client.connect(headers={'login': username,
9 'passcode': password, 'client-id': 'clientid'})
10 #defining the subscriber header
11 subhdrs = {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL}
12 #subscribing the “moa.voevent” topic
13 client.subscribe(channel, headers=subhdrs)
14 #waiting for new VOEvent packet arrival
15 while True:
16 #receiving new arrival VOEvent packet arrival, the receiveFrame
17 #method works in similar way as onMessage() method in Java
18 frame = client.receiveFrame()
19 #displaying the body of VOEvent packet
20 print frame.body
21 #close connection
22 client.disconnect()
Code example 14: Python version of VOEvent subscriber
As we can see, the processes to access the ActiveMQ via the STOMP protocol are very similar to those for the default Openwire protocol. We will not discuss too much detail about the above codes, because the main purpose of this section is to demonstrate that the ActiveMQ can be very easily accessed by using other programming languages.
Another very important benefit offered by the STOMP protocol is cross language communication, which means that the subscriber application and publisher application can be implemented in completely different program languages, yet they are still able to communicate with each other. For example, we can use Python to subscribe and receive new transient event information from the “moa.voevent” topic, even though the publisher application is implemented in Java.
However, Python is not the only programming language supported by STOMP; STOMP supports a very wide range of programming languages (almost all modern programming languages). The example code 15 show how to use Perl to subscribe to the
133
1 use IO::Prompt;
2 use Net::STOMP::Client;
3
4 #Callback function supplied to wait_for_frames().
5 #This function will be invoked whenever a new frame arrives
6 sub doframe {
7 my ($self, $frame) = @_;
8 print "Got a frame: ";
9 print $frame->header("message-id") . "\n";
10 print $frame->body() . "\n";
11 return(0);
12 }
13
14 #If you don't have IO::Prompt module installed,
15 #you can just hard code your username and password
16 #(but not good practice)
17 $username = prompt("Username > ");
18 $password = prompt("Password for $username > ", -e => '*');
19
20 $stomp = Net::STOMP::Client->new(
21 #host IP address
22 host => 'ec2-184-72-17-222.us-west-1.compute.amazonaws.com',
23 port => 61613,
24 );
25
26 #connecting to activemq and login
27 $stomp->connect( 28 login => $username, 29 passcode => $password, 30 ); 31 $stomp->subscribe( 32 destination => "/topic/moa.voevent",
33 id => "clientid", # Apparently required in STOMP 1.1
34 ack => "client"
35 );
36
37 #Now just wait for frames, invoking the given callback
38 #function whenever a new frame arrives
39 $stomp->wait_for_frames(callback => \&doframe);
40
41 #unsubscribe from topic
42 $stomp->unsubscribe(id => "clientid");
43 $stomp->disconnect();
Code example 15: Perl version of VOEvent subscriber
In fact, ActiveMQ is not only useful in a desktop based application; we can also access ActiveMQ in a web browser in order to develop real time web applications. To communicate with ActiveMQ in a web browser, we must first enable WebSocket transport (the procedures for enabling WebSocket transport can be found in Section 4.3.3).
134
The WebSocket transport implements STOMP over web socket functionalities. We can then use stomp.js API to access ActiveMQ via STOMP in a pure web environment. The example code 16 shows how to use Javascript to subscribe to the “moa.voevent” packet in web development:
1 <html>
2 <script src="stomp.js"></script>
3 <script type="text/javascript">
4
5 var client = Stomp.client("ws://ec2-184-72-17-222.us-west-
6 1.compute.amazonaws.com:61614/stomp");
7
8 //connecting to the ActiveMQ
9 client.connect("username", "password",
10 function() { //Callback function, called after client
11 //is sucessful connect to the ActiveMQ
12
13 //subscribes "moa.voevent" topic
14 client.subscribe("/topic/moa.voevent",
15 function(message) { //Callback function, called when client
16 //receives new message from ActiveMQ
17
18 alert(message.body);}) //display message content
Code example 16: Accessing ActiveMQ through using Javascript in web development.
We do not list all of the different programming language examples here, but all of the supported programming language API and code examples can be found on the STOMP official website. Moreover, the STOMP protocol is not only supported by ActiveMQ; we can also use STOMP API to access any JMS message oriented middleware that supports the STOMP protocol.
4.7 Performance Benchmark of Transient Notification Architecture