Connecting POJOs to Service Mix

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Connecting POJOs to Service Mix

ychawla-2
Hello All,
I am a service mix newbie and am currently using version 3.2.2.  I currently have an HTTP endpoint is connected to a EIP recipient list.  That list sends the message out to a JMS queue, sends a transformed message to another JMS and dumps it out to a folder.  All done without writing any Java code.  Pretty cool!

I am at the next phase now and have two questions:

1) I want to take this message and send it off to a custom POJO which will stash in a database.  From what I read, I can use servicemix-cxf se, servicemix-bean or other components.  According to this FAQ:
http://servicemix.apache.org/should-i-create-my-own-jbi-components.html

I should not be creating a JBI component.  That makes sense to me, but what existing component should I use?  I looked into servicemix-cxf-se but that seemed to create a web service.  So I figured servicemix-bean would be more appropriate.  I am confused which direction to go.

2) Now that I have message sitting on a JMS queue, I want to do something with them.  For example, I want to have a POJO that pops the latest message off a queue and processes it.  I don't want to listen to the queue and get any new messages that appear, but retrieve message as consumers demand them.  Should I just write a java application in spring that listens to a queue?  Is there a better way to retrieve these message in servicemix?

Thanks,
Yogesh
Reply | Threaded
Open this post in threaded view
|

Re: Connecting POJOs to Service Mix

Jean-Baptiste Onofré
Hi Yogesh,

You can easily add a SU including your POJO using cxfse component.

To do it, first of all, you need a maven pom.xml for your SU containing :

[...]
<packaging>jbi-service-unit</packaging>
[...]
<dependency>
   <groupId>org.apache.servicemix</groupId>
   <artifactId>servicemix-cxf-se</artifactId>
   <version>3.2.2</version>
</dependency>

You place your POJO code in src/main :

public class myPOJO {

   public ResultObject myMethod(ArgObject arg) {
      ...
   }

}

In the src/main/resources, you place the xbean.xml to use your POJO via cxf-se component :

<beans xmlns:cxfse="http://servicemix.apache.org/cxfse/1.0"
       xmlsns:myService="http://myservice">
   <cxfse:endpoint>
      <cxfse:pojo>
         <bean class="myPOJO"/>
      </cxfse:pojo>
   </cxfse:endpoint>
</beans>

Regards

On Wednesday 29 October 2008 - 12:33, ychawla wrote:

>
> Hello All,
> I am a service mix newbie and am currently using version 3.2.2.  I currently
> have an HTTP endpoint is connected to a EIP recipient list.  That list sends
> the message out to a JMS queue, sends a transformed message to another JMS
> and dumps it out to a folder.  All done without writing any Java code.
> Pretty cool!
>
> I am at the next phase now and have two questions:
>
> 1) I want to take this message and send it off to a custom POJO which will
> stash in a database.  From what I read, I can use servicemix-cxf se,
> servicemix-bean or other components.  According to this FAQ:
> http://servicemix.apache.org/should-i-create-my-own-jbi-components.html
>
> I should not be creating a JBI component.  That makes sense to me, but what
> existing component should I use?  I looked into servicemix-cxf-se but that
> seemed to create a web service.  So I figured servicemix-bean would be more
> appropriate.  I am confused which direction to go.
>
> 2) Now that I have message sitting on a JMS queue, I want to do something
> with them.  For example, I want to have a POJO that pops the latest message
> off a queue and processes it.  I don't want to listen to the queue and get
> any new messages that appear, but retrieve message as consumers demand them.
> Should I just write a java application in spring that listens to a queue?
> Is there a better way to retrieve these message in servicemix?
>
> Thanks,
> Yogesh
> --
> View this message in context: http://www.nabble.com/Connecting-POJOs-to-Service-Mix-tp20234290p20234290.html
> Sent from the ServiceMix - User mailing list archive at Nabble.com.
>

--
Jean-Baptiste Onofré (Nanthrax)
BuildProcess/AutoDeploy Project Leader
http://buildprocess.sourceforge.net
[hidden email]
PGP : 17D4F086
Reply | Threaded
Open this post in threaded view
|

Re: Connecting POJOs to Service Mix

ychawla-2
Hi Jean,
Thanks for the reply.  I added the service unit and it autogenerated an ExampleService.  When I attempt to start service mix I get the following message:

INFO: Creating Service {http://example.com/exampleService}ExampleServiceService from class org.apache.servicemix.samples.bridge.ExampleService
Oct 29, 2008 4:51:48 PM org.apache.cxf.configuration.spring.ConfigurerImpl getBeanName
INFO: Could not determine bean name for instance of class org.apache.cxf.transport.jbi.JBIDestination.
Oct 29, 2008 4:51:48 PM org.apache.cxf.endpoint.ServerImpl initDestination
INFO: Setting the server's publish address to be jbi://ID-165-189-102-106-11d45501dd3-39-3

Here is what the xbean.xml looks like:

<beans xmlns:cxfse="http://servicemix.apache.org/cxfse/1.0"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://servicemix.apache.org/cxfse/1.0 http://servicemix.apache.org/schema/servicemix-cxfse-3.2.2.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <cxfse:endpoint>
        <cxfse:pojo>
          <bean name="exampleServiceBean" class="org.apache.servicemix.samples.bridge.ExampleService" />
        </cxfse:pojo>
    </cxfse:endpoint>

</beans>

Also, I am wondering in my eip configuration, how would I call this POJO.  Here is a simple static route list, I have set up:

        <eip:static-recipient-list service="b:recipients" endpoint="endpoint">
                <eip:recipients>
                        <eip:exchange-target service="b:jms" />
                        <eip:exchange-target service="b:file" />
                        <eip:exchange-target service="WHAT DO I PUT HERE TO CALL THE POJO?" />
                </eip:recipients>
        </eip:static-recipient-list>

Thanks for the assistance!

-Yogesh

Reply | Threaded
Open this post in threaded view
|

Re: Connecting POJOs to Service Mix

Jean-Baptiste Onofré
In reply to this post by ychawla-2
Morning Yogesh

Do you have created a interface for your bean containing something like :

import javax.jws.WebService;

@WebService(targetNamespace = "http://www.exemple.com/myExample", name="MyPojo")
public interface MyPojo {

  public MyObjectResult myMethod(MyObjectArg arg);

}

The class that you put in the bean tag of the cxfse:pojo must implements this
interface.

The interface is required by CXF to be able to generate the WSDL corresponding to
your bean. Another way to avoid this issue is to create the WSDL by hand.

Regards
JB
--
Jean-Baptiste Onofré
[hidden email]
BuildProcess/AutoDeploy Project Leader
http://buildprocess.sourceforge.net

On Wed 29/10/08 22:57, "ychawla" [hidden email] wrote:
>
> Hi Jean,
> Thanks for the reply.  I added the service unit and it autogenerated
> an
ExampleService.  When I attempt to start service mix I get the
> following
message:
>
> INFO: Creating Service
> {http://example.com/exampleService}ExampleServiceService from class
org.apache.servicemix.samples.bridge.ExampleService
> Oct 29, 2008 4:51:48 PM
> org.apache.cxf.configuration.spring.ConfigurerImpl
getBeanName
> INFO: Could not determine bean name for instance of class
> org.apache.cxf.transport.jbi.JBIDestination.
> Oct 29, 2008 4:51:48 PM org.apache.cxf.endpoint.ServerImpl
> initDestination
INFO: Setting the server's publish address to be
> jbi://ID-165-189-102-106-11d45501dd3-39-3
> Here is what the xbean.xml looks like:
>
> <beans xmlns:cxfse="
> http://servicemix.apache.org/cxfse/1.0"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
> http://servicemix.apache.org/cxfse/1.0
http://servicemix.apache.org/schema/servicemix-cxfse-3.2.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xs

> d">
> <cxfse:endpoint>
> <cxfse:pojo>
> <bean name="exampleServiceBean"
> class="org.apache.servicemix.samples.bridge.ExampleService" />
> </cxfse:pojo>
> </cxfse:endpoint>
>
> </beans>
>
> Also, I am wondering in my eip configuration, how would I call this POJO.
>
Here is a simple static route list, I have set up:
>
> <eip:static-recipient-list service="b:recipients"
> endpoint="endpoint">
<eip:recipients>
> <eip:exchange-target service="b:jms" />
> <eip:exchange-target service="b:file" />
> <eip:exchange-target service="WHAT DO I PUT HERE TO CALL THE POJO?"
> />
</eip:recipients>
> </eip:static-recipient-list>
>
> Thanks for the assistance!
>
> -Yogesh
>
>
> --
> View this message in context:
http://www.nabble.com/Connecting-POJOs-to-Service-Mix-tp2023429
> 0p20236733.html
Sent from the ServiceMix - User mailing list archive at Nabble.com.
>
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Connecting POJOs to Service Mix

gertv
In reply to this post by ychawla-2
L.S.,

You can do this with servicemix-camel and the Camel components.  An example: you could combine a camel-jaxb endpoint (to unmarshal the XML document into a POJO) and a camel-jpa endpoint (to store the POJO in the DB).  This way, you would avoid having to do much coding yourself in cxf/bean, you could simply leverage the Camel components' functionality.

For the second question, you could create a simple bean that retrieves a single message from the queue using JMS and either use that bean with servicemix-camel (to add e.g. marshalling to XML) or expose it to the NMR directly using servicemix-bean/servicemix-cxfse.

Regards,

Gert


ychawla wrote
Hello All,
I am a service mix newbie and am currently using version 3.2.2.  I currently have an HTTP endpoint is connected to a EIP recipient list.  That list sends the message out to a JMS queue, sends a transformed message to another JMS and dumps it out to a folder.  All done without writing any Java code.  Pretty cool!

I am at the next phase now and have two questions:

1) I want to take this message and send it off to a custom POJO which will stash in a database.  From what I read, I can use servicemix-cxf se, servicemix-bean or other components.  According to this FAQ:
http://servicemix.apache.org/should-i-create-my-own-jbi-components.html

I should not be creating a JBI component.  That makes sense to me, but what existing component should I use?  I looked into servicemix-cxf-se but that seemed to create a web service.  So I figured servicemix-bean would be more appropriate.  I am confused which direction to go.

2) Now that I have message sitting on a JMS queue, I want to do something with them.  For example, I want to have a POJO that pops the latest message off a queue and processes it.  I don't want to listen to the queue and get any new messages that appear, but retrieve message as consumers demand them.  Should I just write a java application in spring that listens to a queue?  Is there a better way to retrieve these message in servicemix?

Thanks,
Yogesh
Reply | Threaded
Open this post in threaded view
|

Re: Connecting POJOs to Service Mix

ychawla-2
I am in business now.  I went with the servicemix-bean.  Here are the steps that I took:

1) Create servicemix service unit with Maven:
mvn archetype:create -DarchetypeGroupId=org.apache.servicemix.tooling -DarchetypeArtifactId=servicemix-bean-service-unit -DarchetypeVersion=3.2.2 -DgroupId=org.apache.servicemix.samples.bridge -DartifactId=bridge-bean-su

2) Update xbean.xml generated service unit (it generates a handy MyBean example for you) and my example below calls a custom bean I have set up named 'Stash':

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:bean="http://servicemix.apache.org/bean/1.0"
       xmlns:b="http://servicemix.apache.org/samples/bridge"
       xmlns:xsi="http://http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://servicemix.apache.org/bean/1.0 http://servicemix.apache.org/schema/servicemix-bean-3.2.2.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

  <bean:endpoint service="b:beanService" endpoint="endpoint" bean="#myBean"/>

  <bean id="myBean" class="org.apache.servicemix.samples.bridge.MyBean">
     <property name="stashBean">
         <ref local="stash"/>
     </property>
  </bean>
 
  <bean id="stash" class="org.apache.servicemix.samples.bridge.Stash"/>

</beans>

3) Update the example bean that was generate by Maven:
public class MyBean implements MessageExchangeListener {

    @Resource
    private DeliveryChannel channel;
   
    //inject some bean here to do my business logic
    private Stash stashBean;

    public Stash getStashBean() {
                return stashBean;
        }

        public void setStashBean(Stash stashBean) {
                this.stashBean = stashBean;
        }

        public void onMessageExchange(MessageExchange exchange) throws MessagingException {
        System.out.println("Received exchange: " + exchange);
       
//get the message out
        NormalizedMessage message = exchange.getMessage("in");
        Source content = message.getContent();
                //process content according to your logic
                //e.g. to access the message body as a String use
       
        String body = null;
       
                try {
                        body = (new SourceTransformer()).toString(content);
                } catch (TransformerException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

        //call my function that does something
        stashBean.stashToRepository(body);
       
        exchange.setStatus(ExchangeStatus.DONE);
        channel.send(exchange);
    }

}

4) Update the service assembly with my service unit:
          <dependency>
                  <groupId>org.apache.servicemix.samples.bridge</groupId>
                  <artifactId>bridge-bean-su</artifactId>
                  <version>1.0-SNAPSHOT</version>
          </dependency>

5) Update my EIP service unit to call this, unnecessary routing removed for brevity:

<?xml version="1.0"?>
<beans xmlns:eip="http://servicemix.apache.org/eip/1.0"
       xmlns:b="http://servicemix.apache.org/samples/bridge"
       >
        <eip:static-recipient-list service="b:recipients" endpoint="endpoint">
                <eip:recipients>
                        <eip:exchange-target service="b:beanService" />
                </eip:recipients>
        </eip:static-recipient-list>
</beans>

6) Write a simple java class for stash:
package org.apache.servicemix.samples.bridge;

public class Stash {

        public void stashToRepository(String inputMessage)
        {
               
                System.out.println("I am going to stash some stuff right now");
                System.out.println("Let me do something with this: " + inputMessage);
        }
       
}


7) Run maven clean install at the project root
8) Run maven jbi:projectDeploy in the service assembly directory

It should also be noted that I have an http service unit set up that maps incoming requests to the eip 'recipients' service.  This was provided in the bridge example.

Thanks for the assistance.  The servicemix-bean component does the job.