diff options
Diffstat (limited to 'sca-java-2.x/trunk')
3 files changed, 82 insertions, 21 deletions
diff --git a/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/context/ThreadMessageContext.java b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/context/ThreadMessageContext.java index 136f226781..ea1b8b6d30 100644 --- a/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/context/ThreadMessageContext.java +++ b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/context/ThreadMessageContext.java @@ -18,6 +18,12 @@ */ package org.apache.tuscany.sca.context; +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.invocation.Message; /** @@ -26,8 +32,49 @@ import org.apache.tuscany.sca.invocation.Message; * @version $Rev$ $Date$ */ public final class ThreadMessageContext { + + // TUSCANY-3770: Used as a marker for detecting when thread context information can be removed + private static final Message msg = new Message() { + private Map<String, Object> headers = new HashMap<String, Object>(); + public void setTo(Endpoint to) {} + public void setOperation(Operation op) {} + public void setMessageID(Object messageId) {} + public void setFrom(EndpointReference from) {} + public <T> void setFaultBody(T fault) {} + public <T> void setBody(T body) {} + public <T> void setBindingContext(T bindingContext) {} + public boolean isFault() { + return false; + } + public Endpoint getTo() { + return null; + } + public Operation getOperation() { + return null; + } + public Object getMessageID() { + return null; + } + public Map<String, Object> getHeaders() { + return headers; + } + public EndpointReference getFrom() { + return null; + } + public <T> T getBody() { + return null; + } + public <T> T getBindingContext() { + return null; + } + }; - private static final ThreadLocal<Message> CONTEXT = new ThreadLocal<Message>(); + private static final ThreadLocal<Message> CONTEXT = new ThreadLocal<Message>(){ + @Override + protected synchronized Message initialValue() { + return msg; + } + }; private static final ThreadLocal<Message> PREVIOUS_CONTEXT = new ThreadLocal<Message>(); @@ -53,6 +100,12 @@ public final class ThreadMessageContext { Message old = CONTEXT.get(); CONTEXT.set(context); PREVIOUS_CONTEXT.set(old); + + // TUSCANY-3770: Remove thread context information when the request invocation has completed + if (context == msg) { + removeMessageContext(); + removePreviousMessageContext(); + } return old; } @@ -70,14 +123,16 @@ public final class ThreadMessageContext { } /** - * Removes and state from the current thread to ensure that + * Removes any state from the current thread to ensure that * any associated classloaders can be GCd */ + // TUSCANY-3770: The thread context information is removed implicitly above public static void removeMessageContext() { CONTEXT.remove(); } + //for performance concerns - set to null rather than call remove public static void removePreviousMessageContext() { - PREVIOUS_CONTEXT.remove(); + PREVIOUS_CONTEXT.set(null); } } diff --git a/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/RequestContextImpl.java b/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/RequestContextImpl.java index 5d0527682e..6aaa6186b9 100644 --- a/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/RequestContextImpl.java +++ b/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/context/impl/RequestContextImpl.java @@ -65,20 +65,22 @@ public class RequestContextImpl implements RequestContext { public String getServiceName() { Message msgContext = ThreadMessageContext.getMessageContext(); - if (msgContext != null){ + if (msgContext != null && + msgContext.getTo() != null){ return msgContext.getTo().getService().getName(); } else { - // message in thread context could be null if the user has - // spun up a new thread inside their component implementation + // message in thread context could be null (or the default message where to == null) + // if the user has spun up a new thread inside their component implementation return null; } } public <B> ServiceReference<B> getServiceReference() { Message msgContext = ThreadMessageContext.getMessageContext(); - if (msgContext == null){ - // message in thread context could be null if the user has - // spun up a new thread inside their component implementation + if (msgContext == null || + msgContext.getTo() == null){ + // message in thread context could be null (or the default message where to == null) + // if the user has spun up a new thread inside their component implementation return null; } // FIXME: [rfeng] Is this the service reference matching the caller side? @@ -101,9 +103,10 @@ public class RequestContextImpl implements RequestContext { @SuppressWarnings("unchecked") public <CB> ServiceReference<CB> getCallbackReference() { Message msgContext = ThreadMessageContext.getMessageContext(); - if (msgContext == null){ - // message in thread context could be null if the user has - // spun up a new thread inside their component implementation + if (msgContext == null || + msgContext.getTo() == null){ + // message in thread context could be null (or the default message where to == null) + // if the user has spun up a new thread inside their component implementation return null; } diff --git a/sca-java-2.x/trunk/testing/itest/performance/performance-test/src/test/java/org/apache/tuscany/sca/test/performance/PerformanceTestCase.java b/sca-java-2.x/trunk/testing/itest/performance/performance-test/src/test/java/org/apache/tuscany/sca/test/performance/PerformanceTestCase.java index bd6c349877..454ad9dad8 100644 --- a/sca-java-2.x/trunk/testing/itest/performance/performance-test/src/test/java/org/apache/tuscany/sca/test/performance/PerformanceTestCase.java +++ b/sca-java-2.x/trunk/testing/itest/performance/performance-test/src/test/java/org/apache/tuscany/sca/test/performance/PerformanceTestCase.java @@ -56,7 +56,7 @@ import com.sun.management.HotSpotDiagnosticMXBean; */
public class PerformanceTestCase {
- public boolean writeHeapDump = false;
+ public boolean writeHeapDump = true;
private static BufferedWriter resultsFile = null;
@@ -81,7 +81,7 @@ public class PerformanceTestCase { @Test
public void testInstallUninstall() {
- dumpHeapStart();
+ dumpHeapStart("testInstallUninstall");
printRuntimeStats("createRuntime");
@@ -92,6 +92,8 @@ public class PerformanceTestCase { createNode();
callInstallUninstallRepeatedly(10);
+
+ dumpHeapEnd("testInstallUninstall_postUninstall");
checkCacheStatus();
@@ -113,13 +115,13 @@ public class PerformanceTestCase { printRuntimeStats("End");
- dumpHeapEnd();
+ dumpHeapEnd("testInstallUninstall_postStop");
}
@Test
public void testCall() {
- dumpHeapStart();
+ dumpHeapStart("TestCall");
printRuntimeStats("createRuntime");
@@ -148,6 +150,7 @@ public class PerformanceTestCase { //printRuntimeStats("uninstallContribution");
//uninstallContribution();
+ dumpHeapEnd("TestCall_postUninstall");
printRuntimeStats("stopNode");
@@ -167,7 +170,7 @@ public class PerformanceTestCase { printRuntimeStats("End");
- dumpHeapEnd();
+ dumpHeapEnd("TestCall_postStop");
}
public void checkCacheStatus(){
@@ -208,15 +211,15 @@ public class PerformanceTestCase { // ============================================================
- public void dumpHeapStart(){
+ public void dumpHeapStart(String name){
if (writeHeapDump){
- dumpHeap("heap_start.bin");
+ dumpHeap("heap_start_" + name + ".bin");
}
}
- public void dumpHeapEnd(){
+ public void dumpHeapEnd(String name){
if (writeHeapDump){
- dumpHeap("heap_stop.bin");
+ dumpHeap("heap_stop_" + name + ".bin");
System.out.println("You can watch a JVM run using \n" +
" jconsole \n" +
|