| Connecting to JBoss ESB from Tomcat |
| Written by F.Marchioni | |||||||||||||||||||||||||||||
|
In this tutorial we assume you have already a minimal knowledge of JBoss Service Bus. If you are new to this topic I suggest you reading this start-up tutorial: http://www.mastertheboss.com/en/soa-a-esb/78-jboss-esb.html 1) Deploy the HelloWorld Service on your JBoss ESBOk the first thing to do is deploying the Helloworld service on your JBoss ESB. Move to the samples directory and compile first and deploy your service on the Service Bus. C:\jbossesb-server-4.4.GA\samples\quickstarts\helloworld>ant compile C:\jbossesb-server-4.4.GA\samples\quickstarts\helloworld>ant deploy Now start JBoss ESB. C:\jbossesb-server-4.4.GA\bin\run 2) Write your JMS ClientYour JMS Client will be a standard Servlet which init the JMS connection in its init() method and closes the connection in the destroy() callback method.
package test;
import java.io.IOException;
import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class for Servlet: TestServlet
*
*/
public class TestServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
QueueConnection conn;
QueueSession session;
Queue que;
public TestServlet() {
super();
}
public void setupConnection() throws JMSException, NamingException
{
Properties properties1 = new Properties();
properties1.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
properties1.put(Context.URL_PKG_PREFIXES,
"org.jboss.naming:org.jnp.interfaces");
properties1.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099");
InitialContext iniCtx = new InitialContext(properties1);
Object tmp = iniCtx.lookup("ConnectionFactory");
QueueConnectionFactory qcf = (QueueConnectionFactory) tmp;
conn = qcf.createQueueConnection();
que = (Queue) iniCtx.lookup("queue/quickstart_helloworld_Request_gw");
session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
conn.start();
System.out.println("Connection Started");
}
public void init(ServletConfig config) throws ServletException {
try {
setupConnection();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
sendAMessage("Hello World");
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
public void destroy()
{
try {
conn.stop();
session.close();
conn.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void sendAMessage(String msg) throws JMSException {
QueueSender send = session.createSender(que);
ObjectMessage tm = session.createObjectMessage(msg);
send.send(tm);
send.close();
}
}
Now create a mapping for this Servlet in your web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>TestServlet</servlet-name> <servlet-class> test.TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/testServlet</url-pattern> </servlet-mapping> </web-app>
|
| concurrent.jar | JBOSS/CLIENT |
| javassist.jar | JBOSS/CLIENT |
| jbossall-client.jar | JBOSS/CLIENT |
| jboss-aop-jdk50.jar | JBOSS/deploy/JBoss-aop-jdk50.deployer |
| jboss-common-client.jar | JBOSS/CLIENT |
| jboss-j2ee.jar | JBOSS/server/default/lib |
| jboss-messaging.jar | JBOSSESB/server/default/lib |
| jbossmq-client.jar | JBOSS/CLIENT |
| jboss-system-client.jar | JBOSS/CLIENT |
| jnp-client.jar | JBOSS/CLIENT |
| log4j.jar | JBOSS/CLIENT |
| trove.jar | JBOSS/CLIENT |
Ok Now deploy your Web application and try sending messages....
JBoss & Tomcat versions used in this sample:
In order to run this sample, the following configurations were used:Jbossesb-server-4.4.GA
Tomcat 6.0
The JDK used was the latest stable release of 1.5
JBoss.org Search
Custom Search
| Comments |
|
Only registered users can write comments!
Powered by !JoomlaComment 3.26


