diff --git a/activemq-optional/src/main/java/org/apache/activemq/transport/http/HttpTransport.java b/activemq-optional/src/main/java/org/apache/activemq/transport/http/HttpTransport.java index 2ba65a2909..b9674e958f 100755 --- a/activemq-optional/src/main/java/org/apache/activemq/transport/http/HttpTransport.java +++ b/activemq-optional/src/main/java/org/apache/activemq/transport/http/HttpTransport.java @@ -52,8 +52,17 @@ public class HttpTransport extends HttpTransportSupport { public void oneway(Command command) throws IOException { try { - if (command.getDataStructureType()==ConnectionInfo.DATA_STRUCTURE_TYPE) + if (command.getDataStructureType()==ConnectionInfo.DATA_STRUCTURE_TYPE) { + boolean startGetThread = clientID==null; clientID=((ConnectionInfo)command).getClientId(); + if( startGetThread && isStarted() ) { + try { + super.doStart(); + } catch (Exception e) { + throw IOExceptionSupport.create(e); + } + } + } HttpURLConnection connection = getSendConnection(); String text = getTextWireFormat().toString(command); @@ -98,13 +107,16 @@ public class HttpTransport extends HttpTransportSupport { } } } - catch (Exception e) { + catch (Throwable e) { if (!isStopped()) { log.error("Failed to perform GET on: " + remoteUrl + " due to: " + e, e); } else { log.trace("Caught error after closed: " + e, e); } + } finally { + safeClose(receiveConnection); + receiveConnection=null; } } } @@ -167,35 +179,43 @@ public class HttpTransport extends HttpTransportSupport { } protected void setSendConnection(HttpURLConnection conn) { - if (sendConnection != null) { - sendConnection.disconnect(); - } + safeClose(sendConnection); sendConnection = conn; } protected void setReceiveConnection(HttpURLConnection conn) { - if (receiveConnection != null) { - receiveConnection.disconnect(); - } + safeClose(receiveConnection); receiveConnection = conn; } - protected void doStop(ServiceStopper stopper) throws Exception { - if (sendConnection != null) { - stopper.run(new Callback() { - public void execute() throws Exception { - sendConnection.disconnect(); - } - }); - sendConnection = null; + protected void doStart() throws Exception { + // Don't start the background thread until the clientId has been established. + if( clientID != null ) { + super.doStart(); } - if (receiveConnection != null) { - stopper.run(new Callback() { - public void execute() throws Exception { - receiveConnection.disconnect(); - } - }); - receiveConnection = null; + } + + protected void doStop(ServiceStopper stopper) throws Exception { + stopper.run(new Callback() { + public void execute() throws Exception { + safeClose(sendConnection); + } + }); + sendConnection = null; + stopper.run(new Callback() { + public void execute() { + safeClose(receiveConnection); + } + }); + } + + /** + * @param connection TODO + * + */ + private void safeClose(HttpURLConnection connection) { + if( connection!=null ) { + connection.disconnect(); } } diff --git a/activemq-optional/src/main/java/org/apache/activemq/transport/http/HttpTunnelServlet.java b/activemq-optional/src/main/java/org/apache/activemq/transport/http/HttpTunnelServlet.java index edb0af2e87..f8f86455ca 100755 --- a/activemq-optional/src/main/java/org/apache/activemq/transport/http/HttpTunnelServlet.java +++ b/activemq-optional/src/main/java/org/apache/activemq/transport/http/HttpTunnelServlet.java @@ -16,18 +16,11 @@ */ package org.apache.activemq.transport.http; -import edu.emory.mathcs.backport.java.util.concurrent.ArrayBlockingQueue; -import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; - -import org.apache.activemq.command.Command; -import org.apache.activemq.command.ConnectionInfo; -import org.apache.activemq.command.KeepAliveInfo; -import org.apache.activemq.command.WireFormatInfo; -import org.apache.activemq.transport.TransportAcceptListener; -import org.apache.activemq.transport.util.TextWireFormat; -import org.apache.activemq.transport.xstream.XStreamWireFormat; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -35,11 +28,17 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; -import java.io.BufferedReader; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; +import org.apache.activemq.command.Command; +import org.apache.activemq.command.ConnectionInfo; +import org.apache.activemq.command.WireFormatInfo; +import org.apache.activemq.transport.TransportAcceptListener; +import org.apache.activemq.transport.util.TextWireFormat; +import org.apache.activemq.transport.xstream.XStreamWireFormat; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import edu.emory.mathcs.backport.java.util.concurrent.ArrayBlockingQueue; +import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; /** * A servlet which handles server side HTTP transport, delegating to the @@ -56,7 +55,6 @@ public class HttpTunnelServlet extends HttpServlet { private TextWireFormat wireFormat; private Map clients = new HashMap(); private long requestTimeout = 30000L; - private KeepAliveInfo ping = new KeepAliveInfo(); public void init() throws ServletException { super.init(); @@ -76,6 +74,7 @@ public class HttpTunnelServlet extends HttpServlet { try { BlockingQueueTransport transportChannel = getTransportChannel(request); if (transportChannel == null) { + response.sendError(HttpServletResponse.SC_BAD_REQUEST, "clientID not specified."); log("No transport available! "); return; }