cxf-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mayank Thakore" <thakore.may...@gmail.com>
Subject ABR Communication Pattern
Date Sun, 13 Jan 2008 17:39:03 GMT
Hi,

I was trying to achieve Asynchronous Batch Response (ABR) pattern with
CXF for JMS transport. ABR means user can send multiple responses to a
single request.

Please read this and let me know what you think.

Hazard info: I haven't finished the client for this yet, so don't know
wheather it is working. It does print the cxf logs correctly.

Below is my interceptor code. It attaches to the end of the
interceptor chain and executes the previous two interceptors which
invoke the service implementation and send the out message
respectively.
======================================================

package ws.v1.tmf854;

import java.util.ListIterator;

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;

public class ABRInterceptor extends AbstractPhaseInterceptor<Message> {

	public ABRInterceptor() {
		super(Phase.POST_INVOKE);
		getAfter().add(
				org.apache.cxf.interceptor.OutgoingChainInterceptor.class
						.getName());
	}

	@Override
	public void handleMessage(Message message) throws Fault {
		System.out.println("ABRInterceptor invoked");
		if (ABRSession.isEnabled()) {
			executeReRuns(message);
		}
	}

	private void executeReRuns(Message message) {
		ListIterator<Interceptor<? extends Message>> iterator =
prepareReRunIterator(message);
		while (ABRSession.isEnabled()) {
			ABRSession.disable();
			for (int i = 0; i < 2; i++) {
				iterator.previous();
			}
			for (int i = 0; i < 2; i++) {
				Interceptor currentInterceptor = iterator.next();
				currentInterceptor.handleMessage(message);
			}
		}
	}

	private ListIterator<Interceptor<? extends Message>> prepareReRunIterator(
			Message message) {
		ListIterator<Interceptor<? extends Message>> iterator = message
				.getInterceptorChain().getIterator();
		while (iterator.hasNext()) {
			iterator.next();
		}
		iterator.previous();
		return iterator;
	}

}



======================================================



Below is the session control device. It uses a thread local variable
to remember if ABR session is enabled or disabled.
======================================================

package ws.v1.tmf854;

public class ABRSession {

	private static ThreadLocal<Boolean> status = new ThreadLocal<Boolean>() {
		@Override
		protected synchronized Boolean initialValue() {
			return new Boolean(false);
		}
	};

	public static synchronized boolean isEnabled() {
		return status.get().booleanValue();
	}

	public static synchronized void disable() {
		System.out.println("Disabling ABRSession");
		status.set(new Boolean(false));
		
	}

	public static synchronized void enable() {
		System.out.println("Enabling ABRSession");
		status.set(new Boolean(true));
		
	}

}



======================================================

So, if user wants ABR mode, they just need to invoke
ABRSession.enable() inside the service implementation.

Here is the server main for completion:
======================================================

package ws.v1.tmf854;

import javax.xml.ws.Endpoint;

import org.apache.cxf.jaxws.EndpointImpl;

/**
 * This class was generated by Apache CXF (incubator) 2.0.3-incubator Sat Jan 12
 * 11:10:39 IST 2008 Generated source version: 2.0.3-incubator
 *
 */

public class AlarmRetrieval_AlarmRetrievalJms_Server {

	protected AlarmRetrieval_AlarmRetrievalJms_Server() throws Exception {
		System.out.println("Starting Server");
		Object implementor = new AlarmRetrievalImpl();
		String address = "jms://";
		EndpointImpl ep = (EndpointImpl) Endpoint.publish(address, implementor);
		ep.getServer().getEndpoint().getInInterceptors().add(
				new ABRInterceptor());
	}

	public static void main(String args[]) throws Exception {
		new AlarmRetrieval_AlarmRetrievalJms_Server();
		System.out.println("Server ready...");

	}
}


=========================================================


So, what do you think?

Thanks for any and all comments. Feel free to be critical.

Mayank

Mime
View raw message