0
votes

I am trying to run a demo class (JMSJNDIProducer.java) that comes with Websphere MQ 7.0 installation at 'websphere installation location'\tools\jms\samples. I have Websphere up and running, below are my websphere configurations...

  • Queue Manager Name: JMSDEMO
  • Queue Manager Port: 1414
  • Channel (default): SYSTEM.DEF.CLNTCONN
  • Queue Name: JMSDEMO.QL
  • let me know if more info needed...

My code is failing during the initial context creation, I'm very new to Websphere MQ and not sure what the initialContextUrl needs to be?

public class JMS_JNDI_Websphere_Sample {

private static String initialContextUrl = "tcp://localhost:1414";

public static void main(String args[]) {

    // Instantiate the initial context
    String contextFactory = "com.sun.jndi.fscontext.RefFSContextFactory";
    Hashtable<String, Object> environment = new Hashtable<String, Object>();
    environment.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory);
    environment.put(Context.PROVIDER_URL, initialContextUrl);
    try {
        Context context = new InitialDirContext(environment);
    } catch (NamingException e) {
        e.printStackTrace();
    }
    System.out.println("Initial context found!");

}
}

I am getting the below exception.

    javax.naming.InvalidNameException: tcp://localhost:1414 [Root exception is java.net.MalformedURLException: unknown protocol: tcp]
    at com.sun.jndi.fscontext.FSContextFactory.getFileNameFromURLString(FSContextFactory.java:119)
    at com.sun.jndi.fscontext.RefFSContextFactory.createContext(RefFSContextFactory.java:41)
    at com.sun.jndi.fscontext.RefFSContextFactory.createContextAux(RefFSContextFactory.java:47)
    at com.sun.jndi.fscontext.FSContextFactory.getInitialContext(FSContextFactory.java:49)
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
    at javax.naming.InitialContext.init(InitialContext.java:223)
    at javax.naming.InitialContext.<init>(InitialContext.java:197)
    at javax.naming.directory.InitialDirContext.<init>(InitialDirContext.java:82)
    at com.hcsc.jms.websphere.jndi.JMS_JNDI_Websphere_Sample.main(JMS_JNDI_Websphere_Sample.java:32)
Caused by: java.net.MalformedURLException: unknown protocol: tcp
    at java.net.URL.<init>(URL.java:574)
    at java.net.URL.<init>(URL.java:464)
    at java.net.URL.<init>(URL.java:413)
    at com.sun.jndi.fscontext.FSContextFactory.getFileNameFromURLString(FSContextFactory.java:117)
    ... 9 more
2

2 Answers

1
votes

private static String initialContextUrl = "tcp://localhost:1414";

First off, I don't think "tcp" is a valid value and if it is, do you have something running on port 1414 to reply JNDI lookup requests?

Secondly, I think you are confusing MQ port 1414 with JNDI lookup.

Third, why don't you just follow the example in JmsJndiProducer.java and use a file-based JNDI.

i.e. Use MQ Explorer and select "JMS Administered Objects" then do file-based JNDI.

Once you create your file-based JNDI then that value for your initial context.

1
votes

You need to separate out the concept of messaing as provided by the JMS API, and the lookup of an object via JNDI. As Roger said the issue is confusion between the MQ listener and the JNDI URL

JNDI is split into the interface used to bind and lookup objects in the directory and the 'service providers' that take the object and put into some persistent store. The com.sun.jndi.fscontext.RefFSContextFactory is a service provider that uses the file system, so the URL for this needs to be an EXISTING directory. When objects are 'bound' into that the .bindings file is created (or updated if objects are already there). You don't need to create the .bindings file; that file is created for you by the File System Context. Also don't modify this by hand.

Typically you would use a LDAP backed JNDI service provider for production usage. The 'lookup' APIs used in the application are the same; what would change are the provider URL (as the location of the LDAP server would be provided) and possible the object name.

Would suggest reviewing Oracle's JNDI tutorial ..