Examples
Complete Examples
Basic Queue Lookup
Demonstrates how to create an InitialContext and look up a queue using the correct naming convention.
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.jms.Queue;
import java.util.Hashtable;
public class SwiftMQQueueLookupExample {
public static void main(String[] args) throws Exception {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.swiftmq.jndi.InitialContextFactoryImpl");
env.put(Context.PROVIDER_URL, "smqp://user:pass@localhost:4001/type=com.swiftmq.net.PlainSocketFactory;timeout=5000");
Context ctx = null;
try {
ctx = new InitialContext(env);
Queue queue = (Queue) ctx.lookup("testqueue@router1");
System.out.println("Queue: " + queue);
} finally {
if (ctx != null) ctx.close();
}
}
}
Reconnection with Multiple Provider URLs
Shows how to configure the context for automatic reconnection and failover to multiple SwiftMQ routers.
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.jms.Topic;
import java.util.Hashtable;
public class SwiftMQReconnectionExample {
public static void main(String[] args) throws Exception {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.swiftmq.jndi.InitialContextFactoryImpl");
env.put(Context.PROVIDER_URL, "smqp://user:pass@primaryhost:4001/type=com.swiftmq.net.PlainSocketFactory;reconnect=true;maxretries=5;retrydelay=2000");
env.put(Context.PROVIDER_URL + "_2", "smqp://user:pass@backuphost:4002/type=com.swiftmq.net.PlainSocketFactory");
env.put("swiftmq.jndi.reconnect", "true");
env.put("swiftmq.jndi.reconnect.max.retries", "5");
env.put("swiftmq.jndi.reconnect.retry.delay", "2000");
Context ctx = null;
try {
ctx = new InitialContext(env);
Topic topic = (Topic) ctx.lookup("mytopic");
System.out.println("Topic: " + topic);
} finally {
if (ctx != null) ctx.close();
}
}
}
ConnectionFactory Lookup with Authentication
Looks up a ConnectionFactory using authentication in the provider URL and the correct naming convention.
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.jms.ConnectionFactory;
import java.util.Hashtable;
public class SwiftMQConnectionFactoryLookupExample {
public static void main(String[] args) throws Exception {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.swiftmq.jndi.InitialContextFactoryImpl");
env.put(Context.PROVIDER_URL, "smqp://bob:secret@routerhost:4001/type=com.swiftmq.net.PlainSocketFactory");
Context ctx = null;
try {
ctx = new InitialContext(env);
ConnectionFactory cf = (ConnectionFactory) ctx.lookup("ConnectionFactory@router1");
System.out.println("ConnectionFactory: " + cf);
} finally {
if (ctx != null) ctx.close();
}
}
}
JNDI Naming Conventions
- Queue lookup:
"queuename@routername"— e.g."testqueue@router1". The@routerpart is MANDATORY for queues. - Topic lookup:
"topicname"— e.g."testtopic". Topics do NOT use@routernotation. Just the plain topic name. - ConnectionFactory lookup:
"factoryname@routername"— e.g."plainsocket@router1".