Writing Java classes to perform IBM MQ queue manager operations


As you saw in earlier posts IBM WebSphere MQ is messaging for applications. It sends messages across networks of diverse components. Your application connects to IBM WebSphere MQ to send or receive messages.IBM WebSphere MQ have a special set of structures called queue managers. Channels,queues,topics and all the components that we are going to discuss belong to a specific queue manager.So the first step of our programme is to establish a connection with a queue manager.In order to do this first we need to add the following three jars to our project.

  • com.ibm.mq.allclient.jar
  • providerutil.jar
  • fscontext.jar

These jars contain all the methods and constants that we need to initialize a connection with a queue manager.First we need to set up the environment for the connection.Look at the following code segment.

MQEnvironment.hostname = "localhost";
MQEnvironment.channel = "mychannel";
MQEnvironment.port = 1414;

MQEnvironment contains static fields that control the environment in which an MQQueueManager object (and its corresponding connection to WebSphere MQ) is constructed. As values set in the MQEnvironment class take effect when the MQQueueManager constructor is called, you must set the values in the MQEnvironment class before you construct an MQQueueManager object.Let's talk about the attributes one by one.

channel  - The name of the channel to connect to on the target queue manager.
hostname - The TCP/IP hostname of the machine on which the WebSphere MQ server resides.
port - The port to be used.

Next thing you have to do is setting up the username and password fields in the MQEnvironment.In the first post about IBM WebSphere MQ I have discussed about the username and the password fields and how to setup them.Now all you have to do is set those fields in the MQEnvironment. In addition to this you also need to setup the channel name property in the environment.


MQEnvironment.put(CMQC.USER_ID_PROPERTY,"username"); MQEnvironment.put(CMQC.PASSWORD_PROPERTY, "password");
MQEnvironment.put(CMQC.CHANNEL_PROPERTY, "channel_name");

Make sure the channel is not set up for any SSL connections.Otherwise the programme will raise an exception.Now everything is okay and all you have to do is initializing the connection with the queue manager using following constructor.


MQQueueManager qmanager = new MQQueueManager("queue_manager_name");

At this point you may encounter a problem with the connection.When you examine the error log file of the queue manager you may notice that it's because of "CHLAUTH" and "CONAUTH".The main reason for this is that IBM MQ product now takes a stance to have security features turned on by default. In order to avoid these errors you can turn off the security or configure the channels to use the security using some standard queue manager commands.To turn off the security you can use,


ALTER QMGR CHLAUTH(DISABLED) CONNAUTH(' ') REFRESH SECURITY TYPE(CONNAUTH)

or to configure the channel with security you can use,


DEFINE CHANNEL(MY.CHANNEL) CHLTYPE(SVRCONN) SET CHLAUTH(MY.CHANNEL) TYPE(BLOCKUSER) USERLIST('nobody') DESCR('Allow privileged users on this channel') SET CHLAUTH('*') TYPE(ADDRESSMAP) ADDRESS('*') USERSRC(NOACCESS) DESCR('BackStop rule')
SET CHLAUTH(MY.CHANNEL) TYPE(ADDRESSMAP) ADDRESS('*') USERSRC(CHANNEL) CHCKCLNT(REQUIRED)
ALTER AUTHINFO(SYSTEM.DEFAULT.AUTHINFO.IDPWOS) AUTHTYPE(IDPWOS) ADOPTCTX(YES)
REFRESH SECURITY TYPE(CONNAUTH)

Now everything is okay and you can test a successful connection with the queue manager.As I stated above "the MQEnvironment contains static fields that control the environment".So you may have a question what if another person tries to access the programme using a different thread?.There may be a conflict between the ongoing session and the new session as MQEnvironment is already setup for a connection.To avoid these types of errors you can use a HashTable to override the settings of the MQEnvironment as follows.

HashTable mqEnvironment = new HashTable();

mqEnvironment.put(CMQC.USER_ID_PROPERTY, "username");
mqEnvironment.put(CMQC.PASSWORD_PROPERTY,"password");
mqEnvironment.put(CMQC.CHANNEL_PROPERTY,"channel_name");
mqEnvironment.put(MQConstants.HOST_NAME_PROPERTY,"localhost");
mqEnvironment.put(MQConstants.PORT_PROPERTY, 1414);

MQQueueManager qmanager = new MQQueueManager("queue_manager_name",mqEnvironment);

Comments