Java Messaging Service(JMS)

May 22, 2008

JMS

Distributed
Object Technologies like RMI, CORBA and DCOM areadvanced variants of RPC(Remote-Procedure-Call).EJB(
SessionBeans % Entity Beans)is
essentially built on the RMI-IIOP model,( a combination of RMI with CORBA
style). All these systems are Synchronous in nature. (ie) the client
invokes a method on the remote object and if for some reason , there isdelay in response from the server end, the
line getsblocked.This is
sometimes referred to as 'Tight Coupling'.In
Enterprise level
computing, especially, B2B(Business to Business) a number of
applications are integrated together.Ifeither one of the
applications crashes or if we want to add/remove a section from this integrated
scheme,it leads to a mess.

This is what led
to the development of Messaging systems. Such systems are based on
Asynchronous
processing. (ie) aprogramsendsamessage to another programbut
does not wait for response. This ensures that there is no 'blocking'.
Fundamentally, the idea is similar to 'email'.Such systems are said to
be 'loosely coupled' systems. Wherever there are huge number of players
, loose coupling is beneficial. For this reason, the
Enterprise world
isnowadaysveering round to the Messaging protocol . It
is in response to this shift in requirements, that Sun addedthe JMS ( Java Messaging System) API
andthenMessage-driven bean as a third type
of EJBean in addition to Session Bean & Entity bean. .

A number ofsuch Messaging Systems had been developed by
Enterprise-world majors.These are known as 'Message-oriented-Middleware'.
(MOM
).Also known as JMS
-Provider
.

A list of such
MOM products is given below.

1)WebLogicfrom BEA

2)MQSeriesfromIBM

3)JBossMQfrom JBoss

4)SoniqMQfrom Progress

(David Chappel's company)

5)FiaranoMQfrom Fiarano

6)MSMQfrom Microsoft

—————————————————————————-

JMS is not a messaging system by itself. It
is an abstraction of the underlying principles ofthe major Messaging systems in industry. This
enables the programmer to write code as if there is just one generic MOM
system..( Microsoftdoes not follow
JMS
).

————————————————-

There are two
types of messaging:

a) Publish&Subscribe

b) PointtoPoint

———————————————–

In Publish
& Subscribe
method, the sender sends the message toJMS-Provider.. The JMS-Provider broadcasts
the message. The message will be received by any 'listener'who'
tunes'himself to the broadcast by
registering himself as a listener. In such a method ,the message is known as a 'Topic'.The
sender is known as 'publisher' and the receiver is known as
'subscriber
'.This is sometimes referred to as 'one to many'.

—————————————————————————-

In the Point
to Point
method, the 'Sender' sends the message as before to
the MOM server. These message are meant for a specific 'Receiver'.Such a
message is known as a 'Queue'. The listener registers himself with the
JMS-Provider for a particularmessage
and as soon as a message arrives at the provider,it is sent to the listener.This method is referred to as 'one to one'.

————————————————

For writing
messaging programs, we need to import jms package. ( This is
available in weblogic by default.
).

Thecodingconcepts are almost identical for Topics and Queues.

So we will
illustrate the steps forTopic method
only.

steps:for 'publishdemo' program.

1) connecttothe JMS PROVIDER.

2) locateTopicConnectionFactory

3) create aTopicConnection

4) create aTopicSession

5) locate theTopic

6)create aTopicPublisher

7) create aTextMessage or other types of

message.

8) start the connection

9) publish the message

The 'publishdemo'programthussends the message toaJMS-Provider(ie)
WebLogic.,for publication.

—————————————————————————-

A separate 'subscribedemo'
programconnects to the sameJMS-Provider, and follows a similar
procedure,but this time as a recipient of message.

1)connect to theJMS
PROVIDER
.

2) locateTopicConnectionFactory

3) create aTopicConnection

from
theabove factory

4) create aTopicSession

5) locate theTopic

6)create aTopicSubscriber

7) Register the subscriber as a messageListener

8)Wait to get message from JMS

9) Get the message as and when sent by the sender.

—————————————————————————-

This time , we will
create our programs as 'stand-alone' frames .There are no fancy items in
the GUI (Graphical User Interface). TheGUI for the 'publishdemo''program has just a textfield and a button to 'send' the message.Another
button is provided for 'exit'.

Similarly, the GUI
for the 'subscribedemo' program has justatextarea .

———

We start the
'subscribedemo' program first. ( to start listening!). Then we start the
'publishdemo' program . We type some message in text1of' publishdemo'
window and click the 'send' button. The message goes to the JMS-Provider and it
broadcasts the message to all registered listeners. So our 'subscribedemo' programgets the message and it is displayed in
textarea. We can close the sender and restart . The client side is not
affected. You will remember that in socket method , if you close the sender,
the client will quit because it was synchronous.

Just to make it
interesting, we start another receiver program in a separate window. We
will find that this client also gets the message.Withall this description, it should not be difficult to follow the coding.

——–

If we want
to make use of QUEUE, the steps are
almost identical.

1)connect
toJMS-Provider

2)
locate
QueueConnectionFactory

3)
createQueueConnection

4)
createQueueSession

5)
locate
Queue

6)
createaQueueSender

7) create the message

8)send the message.

—————————————–

Similar steps for
the QueueReceiver.

==============================================================

The following demo illustrates a Publish-Subscribemodel.

You will find
reference to 'ourfactory'& 'ourtopic'in the code.

These are known as 'Administered objects'. (ie) we
set up these names in the relevant section of the Weblogic.propertiesfile. How it is done has been explained
below.

//***************publishdemo.java**************************

import javax.ejb.*;

import javax.jms.*;

import javax.naming.*;

import java.rmi.*;

import java.util.*;

import java.awt.*;

public class publishdemoextends Frame

{

TextFieldtext1;

Buttonbutton1;

Buttonbutton2;

TopicPublisher
publisher;

TextMessagetm;

public
static void main(String args[])

{

publishdemoapp=new publishdemo();

app.resize(300,200);

app.show();

}

public
publishdemo()

{

setLayout(new FlowLayout());

setBackground(Color.red);

text1=new TextField(40);

button1=new Button("send");

button2=new Button("Exit");

add(text1);

add(button1);

add(button2);

System.out.println("Please wait");// diagnostic

Propertiesprops=new Properties();

props.put(Context.INITIAL_CONTEXT_FACTORY,

"weblogic.jndi.WLInitialContextFactory");

props.put(Context.PROVIDER_URL,

"t3://127.0.0.1:7001");

Contextcontext=new InitialContext(props);

System.out.println("context
ok");

//——————————————————–

TopicConnectionFactoryfactory=

(TopicConnectionFactory)context.lookup("ourfactory");

TopicConnectionconnection=

factory.createTopicConnection();

TopicSessionsession=

connection.createTopicSession

(false,Session.AUTO_ACKNOWLEDGE);

Topictopic=

(Topic)context.lookup("ourtopic");

publisher=session.createPublisher(topic);

tm=session.createTextMessage();

}catch(Exception e1){System.out.println(""+e1);}

}

public
boolean action(Event e,Object c)

{

if(e.target==button1)

{

try

{

String text=text1.getText();

tm.setText(text);

publisher.publish(tm);

System.out.println("message published");

}catch(Exception e1){System.out.println(""+e1); }

}

if(e.target==button2)

{

System.exit(0);

}

return
true;

}

}

============================================================================

//subscribedemo.java

import javax.ejb.*;

import javax.jms.*;

import javax.naming.*;

import java.rmi.*;

import java.util.*;

import java.awt.*;

public class subscribedemoextends FrameimplementsMessageListener

{

TextAreaarea1;

public
static void main(String args[])

{

subscribedemoapp = new
subscribedemo();

app.resize(300,300);

app.show();

}

publicsubscribedemo()

{

setBackground(Color.orange);

setLayout(new FlowLayout());

area1 =
new TextArea(10,30);

add(area1);

try

{

area1.append("please waitn");

Properties
props=new Properties();

props.put(Context.INITIAL_CONTEXT_FACTORY,

"weblogic.jndi.WLInitialContextFactory");

props.put(Context.PROVIDER_URL,

"t3://127.0.0.1:7001");

Context
context=new InitialContext(props);

area1.append("context
readyn");

//——————————————————–

TopicConnectionFactoryfactory=

(TopicConnectionFactory)context.lookup("ourfactory");

TopicConnectionconnection=

factory.createTopicConnection();

TopicSessionsession=

connection.createTopicSession

(false,Session.AUTO_ACKNOWLEDGE);

Topictopic=

(Topic)context.lookup("ourtopic");

TopicSubscribersubscriber=

session.createSubscriber(topic);

subscriber.setMessageListener(this);

area1.append("starting connectionn");

connection.start();

area1.append("connection startedn");

area1.append("waiting for messagen");

area1.append("——————–n");

}catch(Exception e){area1.setText(""+e);}

}

public
void onMessage(Message msg)

{

try

{

TextMessage tm=(TextMessage)msg;

String
s=tm.getText();

area1.append(s+"n");

}catch(Exception
e){area1.setText(""+e);}

}

}

============================================================================

How shall we test
this program?

=========================

This program is not connected with EJB but is
preliminary to the study of Message-Driven Bean to be takenup shortly.So far, all our examples are being run with
Weblogic 5.1. It can be run on Windows-98.

Message-Driven bean is based on EJB2.0 specification and
requires Weblogic7.0 . which needs Windows 2000 for proper functioning.

We will now
test our JMS program in Weblogic 5.1 installed in Windows- 98.

———-

Create a
folderas:c:jmsdemo

cd toc:jmsdemo

> SETJAVA_HOME=C:JDK1.3

>SETWL_HOME=C:WEBLOGIC

> set path=c:windowscommand;
c:jdk1.3bin;c:weblogicbin

> set classpath=c:jmsdemo;c:weblogicclasses;c:weblogiclibweblogicaux.jar

——–

Editpublishdemo.javaandsubscribedemo.javainc:jmsdemofolder.

——–

…..jmsdemo
>javac*.java//compile

——

*****************************************************

Editc:weblogicweblogic.propertiesfileasfollows:

You will find a
sectionnamed'Weblogic JMS DEMOPROPERTIES'.

ADD THE FOLLOWING
LINES

————————————————

weblogic.jms.connectionFactoryName.ourfactory=ourfactory

weblogic.jms.topic.ourtopic=ourtopic

***********************************************************

From the 'start'
menu,rememberto start the weblogic server as before.

Minimize the window.

Run the subscriber
program first.

…. jmsdemo >javasubscribedemo

You will geta window withjust a
textarea.

The area will
display :

connection started

waiting for message

———–

Now go to
another dos prompt and set path, classpath etc as before.

Start the
publishdemo program

….jmsdemo>javapublishdemo

<span style="font-family: "Courier New

email

Comments

comments