Skip to content

Commit

Permalink
Added wrap_exceptions to MessageDispatcher and RequestCorrelator
Browse files Browse the repository at this point in the history
  • Loading branch information
belaban committed Oct 2, 2015
1 parent f17549e commit 19bef08
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
2 changes: 0 additions & 2 deletions src/org/jgroups/Global.java
Expand Up @@ -105,8 +105,6 @@ public class Global {
public static final int IPV4_SIZE=4; public static final int IPV4_SIZE=4;
public static final int IPV6_SIZE=16; public static final int IPV6_SIZE=16;


public static final String DONT_WRAP_EXCEPTIONS = "jgroups.dont.wrap.exceptions";



public static boolean getPropertyAsBoolean(String property, boolean defaultValue) { public static boolean getPropertyAsBoolean(String property, boolean defaultValue) {
boolean result = defaultValue; boolean result = defaultValue;
Expand Down
10 changes: 9 additions & 1 deletion src/org/jgroups/blocks/MessageDispatcher.java
Expand Up @@ -44,6 +44,7 @@ public class MessageDispatcher implements AsyncRequestHandler, ChannelListener,
protected MembershipListener membership_listener; protected MembershipListener membership_listener;
protected RequestHandler req_handler; protected RequestHandler req_handler;
protected boolean async_dispatching; protected boolean async_dispatching;
protected boolean wrap_exceptions=true;
protected ProtocolAdapter prot_adapter; protected ProtocolAdapter prot_adapter;
protected volatile Collection<Address> members=new HashSet<>(); protected volatile Collection<Address> members=new HashSet<>();
protected Address local_addr; protected Address local_addr;
Expand Down Expand Up @@ -96,6 +97,12 @@ public MessageDispatcher asyncDispatching(boolean flag) {
return this; return this;
} }


public boolean wrapExceptions() {return wrap_exceptions;}
public MessageDispatcher wrapExceptions(boolean flag) {
wrap_exceptions=flag;
if(corr != null)
corr.wrapExceptions(flag);
return this;}


public UpHandler getProtocolAdapter() { public UpHandler getProtocolAdapter() {
return prot_adapter; return prot_adapter;
Expand Down Expand Up @@ -131,7 +138,8 @@ public void removeChannelListener(ChannelListener l) {


public void start() { public void start() {
if(corr == null) if(corr == null)
corr=createRequestCorrelator(prot_adapter, this, local_addr).asyncDispatching(async_dispatching); corr=createRequestCorrelator(prot_adapter, this, local_addr)
.asyncDispatching(async_dispatching).wrapExceptions(this.wrap_exceptions);
correlatorStarted(); correlatorStarted();
corr.start(); corr.start();


Expand Down
14 changes: 9 additions & 5 deletions src/org/jgroups/blocks/RequestCorrelator.java
Expand Up @@ -66,6 +66,9 @@ public class RequestCorrelator {
/** Whether or not to use async dispatcher */ /** Whether or not to use async dispatcher */
protected boolean async_dispatching=false; protected boolean async_dispatching=false;


// send exceptions back wrapped in an {@link InvocationTargetException}, or not
protected boolean wrap_exceptions=true;

private final MyProbeHandler probe_handler=new MyProbeHandler(requests); private final MyProbeHandler probe_handler=new MyProbeHandler(requests);


protected static final Log log=LogFactory.getLog(RequestCorrelator.class); protected static final Log log=LogFactory.getLog(RequestCorrelator.class);
Expand Down Expand Up @@ -109,10 +112,12 @@ public void setRequestHandler(RequestHandler handler) {






public RpcDispatcher.Marshaller getMarshaller() {return marshaller;} public RpcDispatcher.Marshaller getMarshaller() {return marshaller;}
public void setMarshaller(RpcDispatcher.Marshaller marshaller) {this.marshaller=marshaller;} public void setMarshaller(RpcDispatcher.Marshaller marshaller) {this.marshaller=marshaller;}
public boolean asyncDispatching() {return async_dispatching;} public boolean asyncDispatching() {return async_dispatching;}
public RequestCorrelator asyncDispatching(boolean flag) {async_dispatching=flag; return this;} public RequestCorrelator asyncDispatching(boolean flag) {async_dispatching=flag; return this;}
public boolean wrapExceptions() {return wrap_exceptions;}
public RequestCorrelator wrapExceptions(boolean flag) {wrap_exceptions=flag; return this;}


public void sendRequest(long id, List<Address> dest_mbrs, Message msg, RspCollector coll) throws Exception { public void sendRequest(long id, List<Address> dest_mbrs, Message msg, RspCollector coll) throws Exception {
sendRequest(id, dest_mbrs, msg, coll, new RequestOptions().setAnycasting(false)); sendRequest(id, dest_mbrs, msg, coll, new RequestOptions().setAnycasting(false));
Expand Down Expand Up @@ -449,7 +454,6 @@ private void removeEntry(long id) {
protected void handleRequest(Message req, Header hdr) { protected void handleRequest(Message req, Header hdr) {
Object retval; Object retval;
boolean threw_exception=false; boolean threw_exception=false;
boolean wrap_exception=System.getProperty(Global.DONT_WRAP_EXCEPTIONS, "false").equals("false");


if(log.isTraceEnabled()) { if(log.isTraceEnabled()) {
log.trace(new StringBuilder("calling (").append((request_handler != null? request_handler.getClass().getName() : "null")). log.trace(new StringBuilder("calling (").append((request_handler != null? request_handler.getClass().getName() : "null")).
Expand All @@ -462,7 +466,7 @@ protected void handleRequest(Message req, Header hdr) {
} }
catch(Throwable t) { catch(Throwable t) {
if(rsp != null) if(rsp != null)
rsp.send(wrap_exception ? new InvocationTargetException(t) : t, true); rsp.send(wrap_exceptions ? new InvocationTargetException(t) : t, true);
else else
log.error(local_addr + ": failed dispatching request asynchronously: " + t); log.error(local_addr + ": failed dispatching request asynchronously: " + t);
} }
Expand All @@ -474,7 +478,7 @@ protected void handleRequest(Message req, Header hdr) {
} }
catch(Throwable t) { catch(Throwable t) {
threw_exception=true; threw_exception=true;
retval=wrap_exception ? new InvocationTargetException(t) : t; retval=wrap_exceptions ? new InvocationTargetException(t) : t;
} }
if(hdr.rsp_expected) if(hdr.rsp_expected)
sendReply(req, hdr.id, retval, threw_exception); sendReply(req, hdr.id, retval, threw_exception);
Expand Down
Expand Up @@ -73,7 +73,7 @@ public void testMethodReturningException() throws Exception {
// @Test(expectedExceptions=InvocationTargetException.class) // @Test(expectedExceptions=InvocationTargetException.class)
public void testMethodWithException() throws Exception { public void testMethodWithException() throws Exception {
try { try {
disp.callRemoteMethod(channel.getAddress(),"bar",null,null,RequestOptions.SYNC()); disp.callRemoteMethod(channel.getAddress(), "bar", null, null, RequestOptions.SYNC());
assert false: "method should have thrown an exception"; assert false: "method should have thrown an exception";
} }
catch(Exception ex) { catch(Exception ex) {
Expand All @@ -85,14 +85,19 @@ public void testMethodWithException() throws Exception {


@Test(expectedExceptions=TimeoutException.class) @Test(expectedExceptions=TimeoutException.class)
public void testMethodWithExceptionWithoutWrapping() throws Exception { public void testMethodWithExceptionWithoutWrapping() throws Exception {
System.setProperty(Global.DONT_WRAP_EXCEPTIONS, "true"); disp.wrapExceptions(false);
disp.callRemoteMethod(channel.getAddress(),"bar",null,null,RequestOptions.SYNC()); try {
disp.callRemoteMethod(channel.getAddress(), "bar", null, null, RequestOptions.SYNC());
}
finally {
disp.wrapExceptions(true);
}
} }


// @Test(expectedExceptions=IllegalArgumentException.class) // @Test(expectedExceptions=IllegalArgumentException.class)
public void testMethodWithException2() throws Exception { public void testMethodWithException2() throws Exception {
try { try {
disp.callRemoteMethod(channel.getAddress(),"foobar",null,null,RequestOptions.SYNC()); disp.callRemoteMethod(channel.getAddress(), "foobar", null, null, RequestOptions.SYNC());
} }
catch(Throwable t) { catch(Throwable t) {
System.out.println("t = " + t); System.out.println("t = " + t);
Expand All @@ -103,8 +108,13 @@ public void testMethodWithException2() throws Exception {


@Test(expectedExceptions=IllegalArgumentException.class) @Test(expectedExceptions=IllegalArgumentException.class)
public void testMethodWithException2WithoutWrapping() throws Exception { public void testMethodWithException2WithoutWrapping() throws Exception {
System.setProperty(Global.DONT_WRAP_EXCEPTIONS, "true"); disp.wrapExceptions(false);
disp.callRemoteMethod(channel.getAddress(),"foobar",null,null,RequestOptions.SYNC()); try {
disp.callRemoteMethod(channel.getAddress(), "foobar", null, null, RequestOptions.SYNC());
}
finally {
disp.wrapExceptions(true);
}
} }


// @Test(expectedExceptions=AssertionError.class) // @Test(expectedExceptions=AssertionError.class)
Expand All @@ -121,8 +131,13 @@ public void testMethodWithError() throws Exception {


@Test(expectedExceptions=AssertionError.class) @Test(expectedExceptions=AssertionError.class)
public void testMethodWithErrorWithoutWrapping() throws Exception { public void testMethodWithErrorWithoutWrapping() throws Exception {
System.setProperty(Global.DONT_WRAP_EXCEPTIONS, "true"); disp.wrapExceptions(false);
disp.callRemoteMethod(channel.getAddress(),"foofoobar",null,null,RequestOptions.SYNC()); try {
disp.callRemoteMethod(channel.getAddress(), "foofoobar", null, null, RequestOptions.SYNC());
}
finally {
disp.wrapExceptions(true);
}
} }


// @Test(expectedExceptions=Throwable.class) // @Test(expectedExceptions=Throwable.class)
Expand All @@ -140,7 +155,12 @@ public void testMethodWithThrowable() throws Exception {


@Test(expectedExceptions=Throwable.class) @Test(expectedExceptions=Throwable.class)
public void testMethodWithThrowableWithoutWrapping() throws Exception { public void testMethodWithThrowableWithoutWrapping() throws Exception {
System.setProperty(Global.DONT_WRAP_EXCEPTIONS, "true"); disp.wrapExceptions(true);
disp.callRemoteMethod(channel.getAddress(),"fooWithThrowable",null,null,RequestOptions.SYNC()); try {
disp.callRemoteMethod(channel.getAddress(), "fooWithThrowable", null, null, RequestOptions.SYNC());
}
finally {
disp.wrapExceptions(true);
}
} }
} }

0 comments on commit 19bef08

Please sign in to comment.