sábado, 12 de novembro de 2011

Using Objects in JBoss ESB

Sending objects back and forth in JBoss ESB is actually easy! Let's create a service that receives a List<String> (object) and transform the list into a Set<String> (object). This will remove any duplicate entries:

package data;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jboss.soa.esb.actions.AbstractActionLifecycle;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.listeners.message.MessageDeliverException;
import org.jboss.soa.esb.message.Message;

public class MyListenerAction extends AbstractActionLifecycle
{

protected ConfigTree _config;

public MyListenerAction(ConfigTree config) {
_config = config; 

public Message transform(Message message) throws MessageDeliverException {
System.out.println("---------------------------------- transform ----------------------------------");
@SuppressWarnings("unchecked")
List<String> names = (List<String>) message.getBody().get();
Set<String> nameset = new HashSet<String>(names);
message.getBody().add(nameset);
System.out.println("-------------------------------- end transform --------------------------------");
return message;
}

}


We can define this service as follows in jboss-esb.xml:
<?xml version="1.0"?>
<jbossesb parameterReloadSecs="5"
xmlns="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.3.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.3.0.xsd http://anonsvn.jboss.org/repos/labs/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.3.0.xsd">
<providers>
<jms-provider connection-factory="ConnectionFactory"
name="JMS">
<jms-bus busid="transformEsbChannel">
<jms-message-filter dest-name="queue/transform_Request_esb"
dest-type="QUEUE" />
</jms-bus>
</jms-provider>
</providers>
<services>
<service category="Objects_in_Messages_Service"
description="Ojbect in Messages: Use this service to invoke the service"
name="send">
<listeners>
<jms-listener busidref="transformEsbChannel" name="ESB-Listener" />
</listeners>
<actions>
<action class="data.MyListenerAction" name="transformaction"
process="transform" />
</actions>
</service>
</services>
</jbossesb>

the deployment.xml:
<?xml version="1.0"?>
<jbossesb-deployment>
<jmsQueue>transform_Request_esb</jmsQueue>
<jmsQueue>transform_Request_esb_reply</jmsQueue>
</jbossesb-deployment>

and the jbm-queue-service.xml. Let me point out the detail that we need a queue for the service to reply:
<?xml version="1.0" encoding="UTF-8"?>
<server>

<mbean code="org.jboss.jms.server.destination.QueueService"
name="jboss.esb.quickstart.destination:service=Queue,name=transform_Request_esb"
xmbean-dd="xmdesc/Queue-xmbean.xml">
<depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer
</depends>
</mbean>
<mbean code="org.jboss.jms.server.destination.QueueService"
name="jboss.esb.quickstart.destination:service=Queue,name=transform_Request_esb_reply"
xmbean-dd="xmdesc/Queue-xmbean.xml">
<depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer
</depends>
</mbean>
</server>


This is how the project looks:





And now we need a project for the client (refer to my other message about the Asynchronous Invoker). The difference is that in this example we have a Synchronous Invoker. The method we invoke is now the deliverSync(). So the main method of this project is:
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import org.jboss.soa.esb.message.Message;
import org.jboss.soa.esb.message.format.MessageFactory;
import org.jboss.soa.esb.client.ServiceInvoker;

public class ExchangeObjects
{
public static void main(String args[]) throws Exception
{
// Setting the ConnectionFactory such that it will use scout
System.setProperty("javax.xml.registry.ConnectionFactoryClass","org.apache.ws.scout.registry.ConnectionFactoryImpl");

String names[] = {"Jose", "Joao", "Pedro", "Paula", "Joao"};
List<String> mylist = Arrays.asList(names);

Message esbMessage = MessageFactory.getInstance().getMessage();
esbMessage.getBody().add(mylist);

ServiceInvoker si = new ServiceInvoker("Objects_in_Messages_Service", "send");
Message retMessage = si.deliverSync(esbMessage, 10000L);

@SuppressWarnings("unchecked")
Set<String> resultset = (Set<String>) retMessage.getBody().get();
System.out.println("Results. Only one Joao should exist...");
for (String name : resultset)
System.out.println(name);
}

}

To give you some help, this is how this project looks in my JBoss Developer Studio. Refer to my other message about Asynchronous Invoking for further help:


Sem comentários:

Enviar um comentário