sábado, 12 de novembro de 2011

Services with one-way requests (without reply) & how to reply later

Now, let's play with actions that don't return any result. It's quite easy. All you need to do is to add the property mep="OneWay" in the actions:

<actions mep="OneWay">

<action class="action.MyListenerAction" name="helloaction" process="hello" />

What happens as you run the service:

16:35:38,855 INFO  [STDOUT] ---------------------------------- Initialize ----------------------------------
16:35:38,855 INFO  [STDOUT] Initialize got the following data. User = Paul. Text = I would like to say good morning to everybody!
16:35:38,856 INFO  [STDOUT] -------------------------------- end initialize --------------------------------

16:35:39,107 INFO  [STDOUT] MessageActionHandler: Going to the first state!
16:35:39,130 INFO  [InquiryHelper] uddi:juddi.apache.org:737e3ce5-cb96-4c3c-9146-00d9c7baa300 is modified Sat Nov 12 16:35:27 WET 2011 1321115727960
16:35:39,253 INFO  [STDOUT] ---------------------------------- hello ----------------------------------
16:35:39,254 INFO  [STDOUT] -------------------------------- end hello --------------------------------

You don't get back to MessageActionHandler2. Why? Because the My_Demo_Service/Hello never returns to the first state of the orchestrator.

Remember: in the first state we would call My_Demo_Service/Hello and get some response. As we now have a OneWay service, no response ever comes back. How do we pass the process to the next state? We need to signal the process in the jbpm console. Just like here: 

This will let the process go to the final state and finish. At this point, the MessageActionHandler2 is finally invoked and you can see the following in the JBoss esb console:

16:51:21,059 INFO  [STDOUT] Inside MessageActionHandler2
16:51:21,063 INFO  [STDOUT] I would like to say good morning to everybody!

You should notice that the changes introduced by the My_Demo_Service/Hello service are not received by the process.

Next, we would like to have the process suspended until some action occurred (possibly much later) and still reply to automatically let the process go to the final state with all the information. That turns out to be very easy. A few lines in the MyListenerActionClass will "manually" send the reply message back to the "first" state of the orchestrator:

package action;
 * JBoss, Home of Professional Open Source
 * Copyright 2006, JBoss Inc., and others contributors as indicated
 * by the @authors tag. All rights reserved.
 * See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License, v. 2.1.
 * This program is distributed in the hope that it will be useful, but WITHOUT A
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License,
 * v.2.1 along with this distribution; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 * (C) 2005-2006,
 * @author JBoss Inc.

import org.jboss.soa.esb.actions.AbstractActionLifecycle;
import org.jboss.soa.esb.addressing.eprs.LogicalEPR;
import org.jboss.soa.esb.client.ServiceInvoker;
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 hello(Message message) throws MessageDeliverException {
System.out.println("---------------------------------- hello ----------------------------------");
String who = (String) message.getBody().get("whoami");
String textinbody = (String) message.getBody().get();
message.getBody().add("Hello " + who + ". You said: " + textinbody);

LogicalEPR lepr = new LogicalEPR(message.getHeader().getCall().getReplyTo());
ServiceInvoker si = lepr.getServiceInvoker();

System.out.println("-------------------------------- end hello --------------------------------");
return message;


Sem comentários:

Enviar um comentário