Message Driven Bean (Updated)
Step 1: Create the Message Queue and its Factory
There are wo ways to do this task.
(a) Do it on the Glassfish commandline
This is GUI and you can do it interactively.
Note if you do it via port 4848:
The JMS destination resource:
JNDI name: jms/wzy/NGS/Queue1
Physical Destination Name: Queue1 (it does not allow / or _ etc. Make it simple here!!)
Step 2: Create your Message Driven Bean
In your Eclipse -> New Project -> Message Driven Bean
In the dialogue, set the message queue you defined in step 1 ("jms/wzy/Queue")
In your MDB code, you will see the injection code:
@MessageDriven(
activationConfig = { @ActivationConfigProperty(
propertyName = "destinationType", propertyValue = "javax.jms.Queue"
) },
mappedName = "jms/wzy/NGS/Queue1")
You need to define your OnMessage method like follows:
public void onMessage(Message message) {
// TODO Auto-generated method stub
TextMessage textMessage = (TextMessage) message;
try {
System.out.println(textMessage.getText()+ "\n");
} catch (JMSException e) {
e.printStackTrace();
}
}
Then deploy your message driven bean to make sure it is successful.
Step 3: Create another project with the type of Dynamic Web Project.
In this project, you need to code your Servlet and corresponding JSP page.
For example, you can design a form in your JSP page like:
<form action="MainServlet">
<h1>Please enter the updated information</h1>
<table>
<tr>
<td>UserName:</td>
<td><input type="text" name="username" size="30"></td>
</tr>
<tr>
<td>UserID:</td>
<td><input type="text" name="userid" size="30"></td>
</tr>
<tr>
<td>Old Address:</td>
<td><input type="text" name="oldaddress" size="30"></td>
</tr>
<tr>
<td>New Address:</td>
<td><input type="text" name="newaddress" size="30"></td>
<tr>
</table>
<input type="submit" value="Submit">
Here MainServelt is your servlet halding this JSP form. Then you create your servlet with this name and code your DoGet method like:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("MainServlet says hello.");
if(connectionFactory == null) {
out.println("Connection Factory lookup has failed");
return;
}
if(queue == null) {
out.println("Queue lookup has failed");
return;
}
Connection connection;
try{
connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
TextMessage message = session.createTextMessage();
Enumeration arr=request.getParameterNames();
while(arr.hasMoreElements())
{
String fields= (String)arr.nextElement();
String paramname[]=request.getParameterValues(fields);
String s=null;
int i;
for (i=0; i<paramname.length;i++)
{
s=fields+":" + paramname[i];
message.setText(s);
producer.send(message);
out.println("message sent: "+s);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
out.println("ok...done");
}
In this servlet, remember to do the following Injection (along with other fields in the same servlet):
@Resource(mappedName="jms/wzy/ConnectionFactory")
private ConnectionFactory connectionFactory;
@Resource(mappedName="jms/wzy/NGS/Queue1")
private Queue queue;
In your servlet java file, remember to import the Message Driven Bean (in the packaged named mdb):
import mdb.TestMDB;
You can set the CLASSPATH so that your Dynamic Web project will know where the MDB package is (the other MDB project shown above).
Now deploy your Dynamic Web project to Glassfish. Activate your Message Driven Bean by filling up and submit the JSP form.
Watching the printout messages on Glassfish console.
reference tutorial on a different platform:-
http://www.hascode.com/2011/06/message-driven-beans-in-java-ee-6/
https://cwiki.apache.org/GMOxDOC21/jms-application-with-message-driven-bean.html
There are wo ways to do this task.
(a) Do it on the Glassfish commandline
asadmin> create-jms-resource --restype javax.jms.ConnectionFactory jms/wzy/ConnectionFactory
Command create-jms-resource executed successfully.
asadmin> create-jms-resource --restype javax.jms.Queue jms/wzy/NGS/Queue1
Command create-jms-resource executed successfully.
asadmin> list-jms-resources
jms/hascode/Queue
jms/hascode/ConnectionFactory
Command list-jms-resources executed successfully.
(b) Do it using the Glassfish admin panel at localhost:4848This is GUI and you can do it interactively.
Note if you do it via port 4848:
The JMS destination resource:
JNDI name: jms/wzy/NGS/Queue1
Physical Destination Name: Queue1 (it does not allow / or _ etc. Make it simple here!!)
Step 2: Create your Message Driven Bean
In your Eclipse -> New Project -> Message Driven Bean
In the dialogue, set the message queue you defined in step 1 ("jms/wzy/Queue")
In your MDB code, you will see the injection code:
@MessageDriven(
activationConfig = { @ActivationConfigProperty(
propertyName = "destinationType", propertyValue = "javax.jms.Queue"
) },
mappedName = "jms/wzy/NGS/Queue1")
You need to define your OnMessage method like follows:
public void onMessage(Message message) {
// TODO Auto-generated method stub
TextMessage textMessage = (TextMessage) message;
try {
System.out.println(textMessage.getText()+ "\n");
} catch (JMSException e) {
e.printStackTrace();
}
}
Then deploy your message driven bean to make sure it is successful.
Step 3: Create another project with the type of Dynamic Web Project.
In this project, you need to code your Servlet and corresponding JSP page.
For example, you can design a form in your JSP page like:
<form action="MainServlet">
<h1>Please enter the updated information</h1>
<table>
<tr>
<td>UserName:</td>
<td><input type="text" name="username" size="30"></td>
</tr>
<tr>
<td>UserID:</td>
<td><input type="text" name="userid" size="30"></td>
</tr>
<tr>
<td>Old Address:</td>
<td><input type="text" name="oldaddress" size="30"></td>
</tr>
<tr>
<td>New Address:</td>
<td><input type="text" name="newaddress" size="30"></td>
<tr>
</table>
<input type="submit" value="Submit">
Here MainServelt is your servlet halding this JSP form. Then you create your servlet with this name and code your DoGet method like:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("MainServlet says hello.");
if(connectionFactory == null) {
out.println("Connection Factory lookup has failed");
return;
}
if(queue == null) {
out.println("Queue lookup has failed");
return;
}
Connection connection;
try{
connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
TextMessage message = session.createTextMessage();
Enumeration arr=request.getParameterNames();
while(arr.hasMoreElements())
{
String fields= (String)arr.nextElement();
String paramname[]=request.getParameterValues(fields);
String s=null;
int i;
for (i=0; i<paramname.length;i++)
{
s=fields+":" + paramname[i];
message.setText(s);
producer.send(message);
out.println("message sent: "+s);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
out.println("ok...done");
}
In this servlet, remember to do the following Injection (along with other fields in the same servlet):
@Resource(mappedName="jms/wzy/ConnectionFactory")
private ConnectionFactory connectionFactory;
@Resource(mappedName="jms/wzy/NGS/Queue1")
private Queue queue;
In your servlet java file, remember to import the Message Driven Bean (in the packaged named mdb):
import mdb.TestMDB;
You can set the CLASSPATH so that your Dynamic Web project will know where the MDB package is (the other MDB project shown above).
Now deploy your Dynamic Web project to Glassfish. Activate your Message Driven Bean by filling up and submit the JSP form.
Watching the printout messages on Glassfish console.
reference tutorial on a different platform:-
http://www.hascode.com/2011/06/message-driven-beans-in-java-ee-6/
https://cwiki.apache.org/GMOxDOC21/jms-application-with-message-driven-bean.html
No comments:
Post a Comment