Examples
Complete Examples
Basic Queue Lookup
Demonstrates how to set up the SwiftMQ JNDI InitialContext and look up a queue using the required 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<String, String> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.swiftmq.jndi.InitialContextFactoryImpl");
env.put(Context.PROVIDER_URL, "smqp://user:password@localhost:4001");
Context ctx = null;
try {
ctx = new InitialContext(env);
Queue queue = (Queue) ctx.lookup("orders@router1");
System.out.println("Queue: " + queue);
} finally {
if (ctx != null) ctx.close();
}
}
}
Reconnection with Multiple Provider URLs
Shows how to configure the environment for automatic reconnect and failover to a secondary broker.
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.jms.Queue;
import java.util.Hashtable;
public class SwiftMQReconnectExample {
public static void main(String[] args) throws Exception {
Hashtable<String, String> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.swiftmq.jndi.InitialContextFactoryImpl");
env.put(Context.PROVIDER_URL, "smqp://user:password@primaryhost:4001");
env.put(Context.PROVIDER_URL + "_2", "smqp://user:password@secondaryhost:4002");
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);
Queue queue = (Queue) ctx.lookup("orders@router1");
System.out.println("Queue: " + queue);
} finally {
if (ctx != null) ctx.close();
}
}
}
Topic Lookup
Looks up a topic using the correct SwiftMQ naming convention (no @routername for topics).
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.jms.Topic;
import java.util.Hashtable;
public class SwiftMQTopicLookupExample {
public static void main(String[] args) throws Exception {
Hashtable<String, String> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.swiftmq.jndi.InitialContextFactoryImpl");
env.put(Context.PROVIDER_URL, "smqp://user:password@localhost:4001");
Context ctx = null;
try {
ctx = new InitialContext(env);
Topic topic = (Topic) ctx.lookup("news");
System.out.println("Topic: " + topic);
} finally {
if (ctx != null) ctx.close();
}
}
}
ConnectionFactory Lookup
Demonstrates how to look up a JMS ConnectionFactory using the required 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<String, String> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.swiftmq.jndi.InitialContextFactoryImpl");
env.put(Context.PROVIDER_URL, "smqp://user:password@localhost:4001");
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".