Part 1 - Using JBOSS Messaging from a JavaCAPS6 BPEL

Overview


For a POC, I am working with Jboss Messaging to send/receive JMS Message. As I don't find a lot of how-to-guide available, here is a description this activity.


Environment Setup


JavaCaps6-GA


Jboss 4.2.3.GA-jdk6 + jboss-messaging-1.4.0-SP3


 Step 1 : Test JavaCaps6/Netabeans6.1 _ JBOSS messaging connectivity


The following Sender & Listener Main Java classes are useful to test your setup.


The JARs to be added :





Sender.java









 /\*
Use source code downloads, example commands,
and any other techniques at your own risk.
No warranty is provided.
\*/

import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Sender {

    String url_;
    String name_;
    TopicConnection conn = null;
    TopicSession session = null;
    Topic topic = null;

    public Sender(String url, String name) throws JMSException, NamingException {

        url_ = url;
        name_ = name;

            this.initializeSender();

        }

    private void initializeSender() throws JMSException, NamingException {


        Properties props = new Properties();
        props.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory";);
        props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming";);
        props.setProperty("java.naming.provider.url", url_);

        Context context = new InitialContext(props);

        TopicConnectionFactory tcf = (TopicConnectionFactory) context.lookup("ConnectionFactory";);
        conn = tcf.createTopicConnection();
        topic = (Topic) context.lookup(name_);

        session = conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
        conn.start();


    }

     public void send(String text) throws JMSException, NamingException {

         // Send a text msg
         TopicPublisher send = session.createPublisher(topic);
         TextMessage tm = session.createTextMessage(text);
         send.publish(tm);
         send.close();
     }


    public void disconnect() throws JMSException {
        if(conn != null) {
            conn.stop();
        }

        if(session != null) {
            session.close();
        }

        if(conn != null) {
            conn.close();
        }
    }

    public String getTopicName() {
        return name_;
    }

    public String getTopicURL() {
        return url_;
    }

    public static void main(String args[]) throws Exception {

        System.out.println("Starting JMS Example Sender";);

            Sender sender = new Sender("localhost:1099", "topic/example";);


            System.out.println("Sending list of Adam Sandler Movies";);

            sender.send("Billy Madison 1995";);
            sender.send("Happy Gilmore 1996";);
            sender.send("The Waterboy 1998";);
            sender.send("Bid Daddy 1999";);
            sender.send("Mr.Deeds 2002";);
            sender.send("Eight Crazy Nights 2002";);
                sender.send("Anger Management 2003";);

            sender.disconnect();

                System.out.println("JMS Example Sender Complete - list sent";);

     }

}


 Listener.java









 /\*
Use source code downloads, example commands,
and any other techniques at your own risk.
No warranty is provided.
\*/

import java.util.Properties;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Listener implements MessageListener {

    String url_;
    String name_;
    TopicConnection conn = null;
        TopicSession session = null;
        Topic topic = null;

        public Listener(String url, String name) {
            super();

            url_ = url;
            name_ = name;

            try {
                this.initializeListener();
            } catch (Exception e) {
                System.out.println("Error creating listener: " + e);
            e.printStackTrace();
        }

    }

    public void onMessage(Message msg) {

        TextMessage tm = (TextMessage) msg;

        try {
            System.out.println("Incoming message: " + tm.getText());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initializeListener() throws JMSException, NamingException {

        Properties props = new Properties();
        props.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory";);
        props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming";);
        props.setProperty("java.naming.provider.url", url_);

        Context context = new InitialContext(props);
        System.out.println("performing lookup...";);

        Object tmp = context.lookup("ConnectionFactory";);
        System.out.println("lookup completed, making topic";);

        TopicConnectionFactory tcf = (TopicConnectionFactory) tmp;
        conn = tcf.createTopicConnection();
        topic = (Topic) context.lookup(name_);

        session = conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
        conn.start();

        TopicSubscriber recv = session.createSubscriber(topic);
        recv.setMessageListener(this);
    }


    public void disconnect() throws JMSException {
        if(conn != null) {
            conn.stop();
        }

        if(session != null) {
            session.close();
        }

        if(conn != null) {
            conn.close();
        }
    }

    public static void main(String args[]) {

        System.out.println("Starting JMS Example Listener";);
        System.out.println("Program will be active for 1 minute.";);

        //change these values to your situtation:
        Listener listener = new Listener("localhost:1099", "topic/example";);


        //leave it open for 2 minutes:
        try {
            Thread.sleep(120000);
        } catch(Exception e) {
            System.out.println("Error sleeping: " + e);
            e.printStackTrace();
        }

        try {
            listener.disconnect();
        } catch(Exception e) {
            System.out.println("Error terminating listener JMS objects: " + e);
            e.printStackTrace();
        }

        System.out.println("Done listening";);
    }

}



Step 2 :Create a JavaCaps-BPEL to send JMS messages to JBOSS messaging


Develop a BPEL module such as :




The key points are :



  • deploy the JARs in JavaCAPS6/Glassfish

  • define the JBOSS wsld as








 <?xml version="1.0" encoding="UTF-8"?>
<definitions name="jbosswsdl" targetNamespace="http://j2ee.netbeans.org/wsdl/jbosswsdl"
    xmlns="http://schemas.xmlsoap.org/wsdl/"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://j2ee.netbeans.org/wsdl/jbosswsdl" xmlns:plnk="http://docs.oasis-open.org/wsbpel/2.0/plnktype" xmlns:jms="http://schemas.sun.com/jbi/wsdl-extensions/jms/">
    <types/>
    <message name="jbosswsdlOperationRequest">
        <part name="part1" type="xsd:string"/>
    </message>
    <portType name="jbosswsdlPortType">
        <operation name="jbosswsdlOperation">
            <input name="input1" message="tns:jbosswsdlOperationRequest"/>
        </operation>
    </portType>
    <binding name="jbosswsdlBinding" type="tns:jbosswsdlPortType">
        <jms:binding/>
        <operation name="jbosswsdlOperation">
<jms:operation destination="example" destinationType="Topic"/>
            <input name="input1">
                <jms:message messageType="TextMessage" textPart="part1"/>
            </input>
        </operation>
    </binding>
    <service name="jbosswsdlService">
        <port name="jbosswsdlPort" binding="tns:jbosswsdlBinding">
          <jms:address
    connectionURL="jndi://"
    connectionFactoryName="ConnectionFactory"
    providerURL="localhost:1099"
    initialContextFactory="org.jnp.interfaces.NamingContextFactory"
/>

        </port>
    </service>
    <plnk:partnerLinkType name="jbosswsdl">
        <!-- A partner link type is automatically generated when a new port type is added. Partner link types are used by BPEL processes.
In a BPEL process, a partner link represents the interaction between the BPEL process and a partner service. Each partner link is associated with a partner link type.
A partner link type characterizes the conversational relationship between two services. The partner link type can have one or two roles.-->
        <plnk:role name="jbosswsdlPortTypeRole" portType="tns:jbosswsdlPortType"/>
    </plnk:partnerLinkType>
</definitions>



Step 3 :Create a JavaCaps-CASA




Build, Deploy & Run.


It should be OK.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

This blog is about Software How-to & Best Practices

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today