|
http://in.geocities.com/rsramsam/mdb.htm |
A
Simple Demo for MessageDrivenBean
by
R.S.RAMASWAMY
Published in DeveloperIQ
...JANUARY-2004 ..in continuation after
JAVA MESSAGING SERVICE (JMS).
**********************************************************************************************
As
Message-Driven bean, can be run only in WebLogic7.0,we should
familiarize ourselves with Weblogic7.0, first.
Whatever we have studied regarding the code
for Sessionbeans and Entity beans , using WebLogic5.1, holds good for
WebLogic7 also.
ejb-jar.xml
and weblogic-ejb-jar.xml files also are same.
The method of creating the jar file for deployment also is
same.
>jar cf greeter.jar
*.class Meta-inf\*.xml
In the next step, instead of the following
command:
>ejbc greeter.jar greeter1.jar, (in weblogic 5.1)
we should give the command
>java weblogic.ejbc greeter.jar greeter1.jar
This will create greeter1.jar
It may be remembered that after creating greeter1.jar, we edited the
weblogic.properties file (in WebLogic5.1).It is not necessary in
Weblogic7.0.Actually, there is no such file . Deployment is easier in Weblogic
7. We just copy the greeter1.jar to
the "Applications" folder ( explained below in the context
of MDB).With this brief note on Weblogic7.0, let us take up MDB demo.
--------------------------------------------------------------------
We have already seen a demo for JMS.
We had a publishdemo program,
which published a text-message and a
subscribedemo program , which
displays the message as soon as it is
received.
The MessageDrivenBean, the third type of
EJB is just a simple variation on the theme.
So far as the publisher program is concerned there is no change.The
program sends the message, as before to the Topic in the JMS-Provider.( in our
case...WebLogic Server ).
But, instead of providing a separate 'Subscriber', we just write
a MessageDrivenBean. This bean is
located in the EJBServer itself and
it can be programmed to take further steps
as soon as a message is received by the JMS-Provider.
The code for publisher.java has been given below.(code-1)
For easy comparison, the same gui has been retained.
-------------------------------------------------------------
// ***********
publisher.java *****************
import javax.ejb.*;
import javax.jms.*;
import javax.naming.*;
import java.rmi.*;
import java.util.*;
import java.awt.*;
public class publisher
extends Frame
{
TextField text1;
Button button1;
Button button2;
TopicPublisher topicpub;
TextMessage tm;
public static void main(String args[])
{
publisher app =new publisher();
app.resize(400,500);
app.show();
}
publisher()
{
try
{
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");
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);
System.out.println("context
ok");
//--------------------------------------------------------
TopicConnectionFactory factory=
(TopicConnectionFactory)context.lookup("ourfactory");
TopicConnection connection=
factory.createTopicConnection();
System.out.println("factory
ok");
TopicSession session=
connection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
Topic
topic= (Topic)context.lookup("ourtopic");
System.out.println("Topic
ok");
topicpub = 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 s=text1.getText();
tm.setText(s);
publish.publish(tm);
System.out.println("message
published");
}catch(Exception
e1){System.out.println(""+e1); }
}
if(e.target==button2)
{
System.exit(0);
}
return true;
}
}
*********************************************************************
The
code for the MessageDrivenBean follows: (code-2)
// ******* mdb.java
********************************
import java.rmi.*;
import javax.jms.*;
import javax.ejb.*;
import javax.naming.*;
import java.util.*;
import java.io.*;
public class mdb implements MessageDrivenBean,MessageListener
{
public void ejbCreate()
{}
public void ejbRemove()
{}
public void
setMessageDrivenContext(MessageDrivenContext mdc)
{}
public void onMessage(Message msg)
{
try
{
System.out.println("Ready");
TextMessage tm=(TextMessage)msg;
String
s=tm.getText();
System.out.println(s);
}catch(Exception
e1){System.out.println(""+e1);}
}
}
==================================================================
It
can be readily seen that the code for the receiving end has
become ultra-simple. The details about the
topic are placed in the
deployment-descriptor files.
-----------------------------------------------------.
************** ejb-jar.xml
********************
===========
<?xml version="1.0" ?>
<!DOCTYPE ejb-jar PUBLIC
'-//Sun Microsystems,
Inc.//DTD Enterprise JavaBeans 2.0//EN'
'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
<ejb-jar>
<enterprise-beans>
<message-driven>
<ejb-name>mdb</ejb-name>
<ejb-class>mdb</ejb-class>
<transaction-type>Container</transaction-type>
<message-driven-destination>
<destination-type>javax.jms.Topic</destination-type>
<subscription-durability>NonDurable</subscription-durability>
</message-driven-destination>
</message-driven>
</enterprise-beans>
</ejb-jar>
===========================================================
************* weblogic-ejb-jar.xml **************
====================
<?xml
version="1.0" ?>
<!DOCTYPE weblogic-ejb-jar
PUBLIC
'-//BEA Systems, Inc.//DTD
WebLogic 7.0.0 EJB//EN'
'http://www.bea.com.servers/wls700/dtd/weblogic-ejb-jar.dtd'>
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>mdb</ejb-name>
<message-driven-descriptor>
<destination-jndi-name>ourtopic</destination-jndi-name>
</message-driven-descriptor>
<jndi-name>mdbJndi</jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
=====================================================================
Procedure for MessageDrivenBean deployment.
------------------
Let us create a folder as follows:
c:\mdbdemo
mdbdemo>set JAVA_HOME=D:\JDK1.3
>set WL_HOME=d:\bea\weblogic700
>set Application=d:\bea\user_projects\mydomain\Applications
>set path=c:\windows\command;
d:\jdk1.3\bin;
d:\bea\weblogic700\server\bin;
>set classpath=c:\mdbdemo;
d:\bea\weblogic700\server\lib\weblogic.jar
Create jar file
>jar cf mdb.jar
*.class META-INF\*.xml
After creating the jar,
>java weblogic.ejbc mdb.jar mdb1.jar
(there may be a warning message. It can be
ignored).
-------------
That completes the deployment.
Start the weblogic7 server .
(startmenu->BEAWeblogic7platform
->userprojects->mydomain->serverr
===================================================================
publisher.java is not a bean. So no xml file & jar for that file..
============================================================
>copy
mdb1.jar %Applications% ( when server is running)
You will get a message on the server
"--mdb1_jar Running"
"--mdb_mdb1_jar completed"
after getting the "completed"
message in server window
you can run the publisher
>java publisher
This command, publishes a message to the JMSProvider. As soon as it is
published, the message is displayed in Weblogic server's console.
according to our coding.
======================================================
Thus, we have seen simple examples for all the five types of Enterprise
Java Beans, namely,
1) Stateless Session Bean
2) Stateful Session Bean
3) Entity Bean (Container-Managed Persistence)
4) Entity Bean (Bean-Managed Persistence)
5) Message-Driven Bean.
We saw how these beans can be invoked from stand-alone programs and
through servlets. Since, invoking the EJbean by using another EJbean is more
efficient from the viewpoint of network resources, we saw an example for that
too. (linker demo).
As
a bonus, since a knowledge of JMS is required to understand MDB,
we saw a simple demo for JMS in Weblogic5.1,
as well.( You would have noticed that except MDB, everything else can be done
in Weblogic5.1.
For learning purposes, it is easier because
it runs on Windows-98 even).
====================================================================
It is time now to take up the JAX pack in J2EE basket and see some very interesting
examples of interconnecting a J2EE
service with DotNet and consuming ASP.net service from a java standalone program. In
the process, we will be getting nearer to the recent developments such as APACHE SOAP2.2 , WSDL, UDDI, JAX,WEBLOGIC
WORKSHOP,AXIS etc.
These, we will take up in our next lesson.
********************************************************************