Running into an issue where connection.start() fails due to
java.net.ConnectException: Connection refused: connect
when I wrap my javax.jms.TopicConnection
in a ThreadLocal
, as follows:
private ThreadLocal<TopicConnection> createThreadLocalTopicConnection(final TopicConnectionFactory cf)
{
return new ThreadLocal<TopicConnection>() {
public TopicConnection result;
protected synchronized TopicConnection initialValue() {
try {
// Returns a javax.jms.TopicConnection object.
result = cf.createTopicConnection();
result.start();
return result;
} catch (JMSException e) {
throw new RuntimeException(e);
}
}
protected void finalize() throws Throwable {
if(result!=null) result.close();
}
};
}
If I just create the TopicConnection
as a bare static
variable, it connects without any issues. Can anybody explain why it would work as a bare variable but when wrapped in a ThreadLocal
it would fail? Google has failed me pretty spectacularly this time around.