Setting the a connect timeout on an URLConnection

JDK1.5 allows you to set connect and read timeouts on URLConnection (HttpURLConnection, etc.).

Here is an example:

Main.java:

import java.net.*;

public class Main
{
   public static void main(String []args) throws Exception {
      if (args.length != 1) {
         System.err.println("Usage: java Main <URL>");
         System.exit(0);
      }
          
      URL url = new URL(args[0]);
      URLConnection connection = url.openConnection();
 
      connection.setConnectTimeout(5000);
      connection.connect();
   }
}

Publishing a Websphere MQ ConnectionFactory in an LDAP

The following working code sample allows you to create a QueueConnectionFactory (MQQueueConnectionFactory) and register it as an administered object in an LDAP tree.

I used iPlanet Directory Server 5.0 to test it out.

RegisterWebsphereMQ.java:

import  com.ibm.mq.jms.MQQueueConnectionFactory;
import  javax.naming.*;
import  javax.jms.*;
 
import  java.util.*;
 
public class RegisterWebsphereMQ
{
   public static void main(String args[])
   {
      if (args.length != 5) {
         System.err.println("Usage: RegisterWebsphereMQ LDAP-url base queue-manager username password");
         System.err.println("  eg.:      ldapurl        ldap://127.0.0.1:389");
         System.err.println("            providername   ou=dev,ou=test,o=esus.com");
         System.err.println("            queue-manager  name of the queue manager, eg. MQS1");
         System.err.println("            username       uid=user,ou=People,o=esus.com");
         System.err.println("            password       pw");
         System.exit(1);
      }
      String ldapurl=args[0];
      String base=args[1];
      String queuemgr=args[2];
      String username=args[3];
      String password=args[4];
 
      try {
         Context context = getInitialContext(ldapurl, username, password);
 
         createLDAPObject(context, base, "QueueConnectionFactory");
         createQueueConnectionFactory(context, base, queuemgr);
 
         System.out.println("Registration successful");
      } catch(Exception e) {
         System.out.println("Registration failed: " + e);
         e.printStackTrace();
      }
   }
 
   public static Context getInitialContext(String ldapurl, String username, String password)
                            throws NamingException
   {
      Properties env=System.getProperties();
      env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
      env.put(Context.PROVIDER_URL, ldapurl);
      env.put(Context.SECURITY_PRINCIPAL, username);
      env.put(Context.SECURITY_CREDENTIALS, password);
 
      // Retrieve the initial context
      return new InitialContext(env);
   }
 
   public static void createLDAPObject(Context context, String base, String ouName)
                        throws NamingException
   {
      try {
         context.bind("ou=" + ouName + "," + base, new LDAPObject(ouName));
         System.out.println("Created OU [" + ouName + "]");
      }
      catch(NameAlreadyBoundException e) {
         System.out.println("Name " + ouName + " is already bound under " + base);
      }
   }
 
   public static void createQueueConnectionFactory(Context context, String base, String queuemgr)
                         throws NamingException, JMSException
   {
      QueueConnectionFactory qcf = new MQQueueConnectionFactory();
      ((MQQueueConnectionFactory) qcf).setQueueManager(queuemgr);
 
      // Bind the connection factory into the JNDI repository
      context.rebind("cn=queuefactory,ou=QueueConnectionFactory,"+base, qcf);
   }   
}

LDAPObject.java:

import javax.naming.*;
import javax.naming.directory.*;
import java.util.*;
 
class LDAPObject implements DirContext {
   Attributes attributes;
   String name;
    
   public LDAPObject(String name) {
      this.name = name;
 
      attributes = new BasicAttributes(true);
      // case ignore
      Attribute oc = new BasicAttribute("objectclass");
      oc.add("organizationalunit");
      oc.add("top");
 
      attributes.put(oc);
   }
 
   public Attributes getAttributes(String name) throws NamingException {
      if (!name.equals("")) {
         throw new NameNotFoundException();
      }
      return (Attributes) attributes.clone();
   }
 
   public Attributes getAttributes(Name name) throws NamingException {
      return getAttributes(name.toString());
   }
 
   public Attributes getAttributes(String name, String[] ids)
          throws NamingException {
      if (!name.equals("")) {
         throw new NameNotFoundException();
      }
 
      Attributes answer = new BasicAttributes(true);
      Attribute target;
      for (int i = 0; i < ids.length; i++) {
         target = attributes.get(ids[i]);
         if (target != null) {
            answer.put(target);
         }
      }
      return answer;
   }
 
   public Attributes getAttributes(Name name, String[] ids)
          throws NamingException {
      return getAttributes(name.toString(), ids);
   }
 
   public String toString() {
      return name;
   }
 
   public Object lookup(Name name) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public Object lookup(String name) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public void bind(Name name, Object obj) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public void bind(String name, Object obj) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public void rebind(Name name, Object obj) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public void rebind(String name, Object obj) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public void unbind(Name name) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public void unbind(String name) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public void rename(Name oldName, Name newName) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public void rename(String oldName, String newName) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public NamingEnumeration list(Name name) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public NamingEnumeration list(String name) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public NamingEnumeration listBindings(Name name) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public NamingEnumeration listBindings(String name) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public void destroySubcontext(Name name) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public void destroySubcontext(String name) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public Context createSubcontext(Name name) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public Context createSubcontext(String name) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public Object lookupLink(Name name) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public Object lookupLink(String name) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public NameParser getNameParser(Name name) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public NameParser getNameParser(String name) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public String composeName(String name, String prefix)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public Name composeName(Name name, Name prefix)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public Object addToEnvironment(String propName, Object propVal)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public Object removeFromEnvironment(String propName)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public Hashtable getEnvironment() throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public void close() throws NamingException {
      throw new OperationNotSupportedException();
   }
 
// -- DirContext
   public void modifyAttributes(Name name, int mod_op, Attributes attrs)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public void modifyAttributes(String name, int mod_op, Attributes attrs)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public void modifyAttributes(Name name, ModificationItem[] mods)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public void modifyAttributes(String name, ModificationItem[] mods)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public void bind(Name name, Object obj, Attributes attrs)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public void bind(String name, Object obj, Attributes attrs)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public void rebind(Name name, Object obj, Attributes attrs)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public void rebind(String name, Object obj, Attributes attrs)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public DirContext createSubcontext(Name name, Attributes attrs)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public DirContext createSubcontext(String name, Attributes attrs)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public DirContext getSchema(Name name) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public DirContext getSchema(String name) throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public DirContext getSchemaClassDefinition(Name name)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public DirContext getSchemaClassDefinition(String name)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public NamingEnumeration search(Name name,
         Attributes matchingAttributes,
         String[] attributesToReturn)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public NamingEnumeration search(String name,
         Attributes matchingAttributes,
         String[] attributesToReturn)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public NamingEnumeration search(Name name,
         Attributes matchingAttributes)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public NamingEnumeration search(String name,
         Attributes matchingAttributes)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public NamingEnumeration search(Name name,
         String filter,
         SearchControls cons)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public NamingEnumeration search(String name,
         String filter,
         SearchControls cons)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public NamingEnumeration search(Name name,
         String filterExpr,
         Object[] filterArgs,
         SearchControls cons)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public NamingEnumeration search(String name,
         String filterExpr,
         Object[] filterArgs,
         SearchControls cons)
          throws NamingException {
      throw new OperationNotSupportedException();
   }
 
   public String getNameInNamespace() throws NamingException {
      throw new OperationNotSupportedException();
   }
}

Sample run:

C:myldap>java RegisterWebsphereMQ ldap://localhost:2000 ou=dev,ou=epais,o=philips.com MQS1 uid=user,ou=People,o=esus.com pw
Created OU [QueueConnectionFactory]
Registration successful