Skip to content

Commit

Permalink
Merge branch 'release/12.4.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
iitsoftware committed May 27, 2022
2 parents 7170eb9 + 93b54a8 commit 0bea73e
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 90 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>com.swiftmq</groupId>
<artifactId>swiftmq-client</artifactId>
<version>12.4.0</version>
<version>12.4.1</version>

<name>SwiftMQ Client</name>
<description>Client for SwiftMQ Messaging System with JMS, AMQP 1.0 and file transfer over JMS.</description>
Expand Down
69 changes: 56 additions & 13 deletions src/main/java/com/swiftmq/jndi/v400/ContextImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,30 @@
import com.swiftmq.jndi.protocol.v400.*;
import com.swiftmq.swiftlet.jndi.JNDISwiftlet;
import com.swiftmq.tools.dump.Dumpalizer;
import com.swiftmq.tools.timer.TimerEvent;
import com.swiftmq.tools.timer.TimerListener;
import com.swiftmq.tools.timer.TimerRegistry;
import com.swiftmq.tools.util.DataByteArrayOutputStream;
import com.swiftmq.tools.versioning.Versionable;
import com.swiftmq.tools.versioning.Versioned;

import javax.jms.*;
import javax.naming.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

public class ContextImpl implements Context, java.io.Serializable {
public class ContextImpl implements Context, java.io.Serializable, AutoCloseable, TimerListener {
Hashtable env = null;
JNDIInfo jndiInfo = null;
ConnectionFactory cf = null;
Connection connection = null;
Session session = null;
MessageProducer producer = null;
boolean closed = true;
boolean debug = false;
long lastAccessTime = 0;

public ContextImpl(Hashtable env)
throws NamingException {
Expand All @@ -48,6 +55,7 @@ public ContextImpl(Hashtable env)
if (url == null)
throw new NamingException("missing JNDI environment property: Context.PROVIDER_URL (" + Context.PROVIDER_URL + ")");
jndiInfo = URLParser.parseURL(url);
debug = jndiInfo.isDebug();
Map props = new HashMap();
if (jndiInfo.isIntraVM()) {
try {
Expand All @@ -72,11 +80,10 @@ public ContextImpl(Hashtable env)
}
}
try {
ConnectionFactory cf = (ConnectionFactory) SwiftMQConnectionFactory.create(props);
connection = cf.createConnection(jndiInfo.getUsername(), jndiInfo.getPassword());
session = connection.createSession(false, 0);
producer = session.createProducer(null);
connection.start();
cf = SwiftMQConnectionFactory.create(props);
createConnection();
if (jndiInfo.getIdleclose() > 0)
TimerRegistry.Singleton().addTimerListener(1000, this);
closed = false;
} catch (Exception e) {
if (connection != null) {
Expand All @@ -92,6 +99,38 @@ public ContextImpl(Hashtable env)
}
}

private void createConnection() throws JMSException {
connection = cf.createConnection(jndiInfo.getUsername(), jndiInfo.getPassword());
session = connection.createSession(false, 0);
producer = session.createProducer(null);
connection.start();
lastAccessTime = System.currentTimeMillis();
if (debug)
System.out.println(new Date() + " " + toString() + "/createConnection: " + env.get(Context.PROVIDER_URL));
}

private void checkConnection() throws JMSException {
if (!closed && connection == null)
createConnection();
lastAccessTime = System.currentTimeMillis();
}

@Override
public synchronized void performTimeAction(TimerEvent evt) {
long delta = System.currentTimeMillis() - lastAccessTime - jndiInfo.getIdleclose();
if (debug)
System.out.println(new Date() + " " + toString() + "/performTimeAction, connection=" + connection + ", delta=" + delta + ", lastAccessTime=" + lastAccessTime);
if (connection != null && lastAccessTime + jndiInfo.getIdleclose() < System.currentTimeMillis()) {
try {
if (debug)
System.out.println(new Date() + " " + toString() + "/createConnection, close connection (idle close)");
connection.close();
} catch (JMSException ignored) {
}
connection = null;
}
}

public Object addToEnvironment(String name, Object value)
throws NamingException {
throw new OperationNotSupportedException("not supported");
Expand All @@ -118,6 +157,7 @@ public synchronized void bind(String name, Object obj)
if (!(obj instanceof TemporaryTopicImpl || obj instanceof TemporaryQueueImpl))
throw new OperationNotSupportedException("bind is only supported for TemporaryQueues/TemporaryTopics!");
try {
checkConnection();
TemporaryTopic tt = session.createTemporaryTopic();
MessageConsumer consumer = session.createConsumer(tt);

Expand Down Expand Up @@ -145,9 +185,15 @@ public void bind(Name p0, Object p1)
public synchronized void close()
throws NamingException {
try {
connection.close();
if (connection != null) {
if (debug)
System.out.println(new Date() + " " + toString() + "/close");
connection.close();
}
} catch (Exception ignored) {
}
if (!jndiInfo.isIntraVM() && jndiInfo.getIdleclose() > 0)
TimerRegistry.Singleton().removeTimerListener(1000, this);
closed = true;
}

Expand Down Expand Up @@ -236,6 +282,7 @@ public synchronized Object lookup(String name)
boolean connectionClosed = false;
Object obj = null;
try {
checkConnection();
TemporaryQueue tq = session.createTemporaryQueue();
MessageConsumer consumer = session.createConsumer(tq);

Expand Down Expand Up @@ -297,6 +344,7 @@ public synchronized void rebind(String name, Object obj)
if (!(obj instanceof TemporaryTopicImpl || obj instanceof TemporaryQueueImpl))
throw new OperationNotSupportedException("rebind is only supported for TemporaryQueues/TemporaryTopics!");
try {
checkConnection();
TemporaryTopic tt = session.createTemporaryTopic();
MessageConsumer consumer = session.createConsumer(tt);

Expand Down Expand Up @@ -342,7 +390,7 @@ public synchronized void unbind(String name)
if (closed)
throw new NamingException("context is closed!");
try {

checkConnection();
Versionable versionable = new Versionable();
versionable.addVersioned(400, createVersioned(400, new UnbindRequest(name)), "com.swiftmq.jndi.protocol.v400.JNDIRequestFactory");
BytesMessage request = createMessage(versionable, null);
Expand All @@ -356,10 +404,5 @@ public void unbind(Name p0)
throws NamingException {
throw new OperationNotSupportedException("not supported");
}

protected void finalize() throws Throwable {
close();
super.finalize();
}
}

31 changes: 15 additions & 16 deletions src/main/java/com/swiftmq/jndi/v400/JNDIInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class JNDIInfo implements java.io.Serializable {
String factory;
long timeout;
long keepalive;
long idleclose;
boolean intraVM = false;
boolean reconnect = false;
long reconnectDelay = 0;
Expand All @@ -34,29 +35,15 @@ public class JNDIInfo implements java.io.Serializable {
boolean debug = false;
boolean hasParameters = false;

protected JNDIInfo(String username, String password, String hostname, int port, String factory, long timeout, boolean intraVM) {
this(username, password, hostname, port, factory, timeout, 0, intraVM);
}

protected JNDIInfo(String username, String password, String hostname, int port, String factory, long timeout, long keepalive, boolean intraVM) {
this.username = username;
this.password = password;
this.hostname = hostname;
this.port = port;
this.factory = factory;
this.timeout = timeout;
this.keepalive = keepalive;
this.intraVM = intraVM;
}

protected JNDIInfo(String username, String password, String hostname, int port, String factory, long timeout, long keepalive, boolean intraVM, boolean reconnect, long reconnectDelay, int maxRetries, String hostname2, int port2, boolean debug, boolean hasParameters) {
protected JNDIInfo(String username, String password, String hostname, int port, String factory, long timeout, long keepalive, long idleclose, boolean intraVM, boolean reconnect, long reconnectDelay, int maxRetries, String hostname2, int port2, boolean debug, boolean hasParameters) {
this.username = username;
this.password = password;
this.hostname = hostname;
this.port = port;
this.factory = factory;
this.timeout = timeout;
this.keepalive = keepalive;
this.idleclose = idleclose;
this.intraVM = intraVM;
this.reconnect = reconnect;
this.reconnectDelay = reconnectDelay;
Expand Down Expand Up @@ -95,6 +82,10 @@ public long getKeepalive() {
return keepalive;
}

public long getIdleclose() {
return idleclose;
}

public boolean isIntraVM() {
return intraVM;
}
Expand Down Expand Up @@ -161,6 +152,12 @@ public String getProviderURL(String host, int port) {
b.append(keepalive);
semiRequired = true;
}
if (idleclose != 0) {
if (semiRequired)
b.append(";");
b.append("idleclose=");
b.append(idleclose);
}
}
}
return b.toString();
Expand All @@ -182,6 +179,8 @@ public String toString() {
s.append(timeout);
s.append(", keepalive=");
s.append(keepalive);
s.append(", idleclose=");
s.append(idleclose);
s.append(", intraVM=");
s.append(intraVM);
s.append(", reconnect=");
Expand Down
Loading

0 comments on commit 0bea73e

Please sign in to comment.