How to connect ibmmq 9.1 with any application i.e like (java, vb.net, python etc..) - ibm-mq

In order to connect any application with ibmmq 9.1, what are the steps we need to follow , how to connect ibmmq with any application.

There are many samples shipped with product that you can refer to develop applications in Java and .NET. One simple sample written in Java JMS that receives message from a queue.
private void receiveWithContext(){
JmsConnectionFactory cf = null;
JMSContext msgContext = null;
try {
JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
cf = ff.createConnectionFactory();
cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, "QM");
cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_BINDINGS);
msgContext = cf.createContext("youruserid","yourpassword",JMSContext.AUTO_ACKNOWLEDGE);
Queue jmsQ = msgContext.createQueue("Q1");
JMSConsumer msgCons = msgContext.createConsumer(jmsQ);
Message msg = msgCons.receiveNoWait();
}catch(JMSException jmsEx){
System.out.println(jmsEx);
}
}

Related

How to receive MQMessage using JmsListener

I am upgrading my application from a regular java application to Spring boot application.
I am connecting with an external application, using IBM MQ.
At the old version i got an MQMessage object and used only the feedback field. like this:
MQQueue currQ = this.qMgr.accessQueue("myQueue",MQC.MQOO_INQUIRE + MQC.MQOO_INPUT_SHARED + MQC.MQOO_FAIL_IF_QUIESCING);
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.options = MQC.MQGMO_SYNCPOINT;
MQMessage mqMsg = new MQMessage();
currQ.get(mqMsg, gmo);
System.out.println("feedback: " + mqMsg.feedback);
In my new code, I am using JmsListener:
#JmsListener(destination = "myQueue", containerFactory = "jmsListenerContainerFactory")
public void myQueueListener(String message) {
System.out.println("feedback: " + ???);
}
How do i get the feedback field?
I have no control on the external application that sends the message.
The chapter JMS fields and properties with corresponding MQMD fields explains the header mapping. You have two options to access the headers (see also this answer), here is one of it:
#JmsListener(destination = "myQueue", containerFactory = "jmsListenerContainerFactory")
public void myQueueListener(String message, #Header("JMS_IBM_Feedback") Integer feedback) {
System.out.println("feedback: " + feedback);
}

Spring Boot ActiveMQJMSConnectionFactory to connect to a JBoss EAP 6.1 HornetQ

I'm not sure if what I'm trying to do is possible but basically I'm trying to update the current implementation from HornetQ to ActiveMQ making use of Artemis.
My system is a JMS consumer from the HornetQ.
The current implementation works if I use "HornetQJMSConnectionFactory" but when I change to ActiveMQJMSConnectionFactory it is not able to connect.
In order to test the new implementation, I've spun up a local instance of ActiveMQ and works with the new implementation.
So I've tried multiple different things including forcing protocol =HORNETQ and nothing works.
No compilation error, "only":
ERROR o.s.j.l.DefaultMessageListenerContainer.refreshConnectionUntilSuccessful - Could not refresh JMS Connection for destination 'QueueX' - retrying using FixedBackOff{interval=5000, currentAttempts=1, maxAttempts=unlimited}. Cause: Failed to create session factory; nested exception is ActiveMQConnectionTimedOutException[errorType=CONNECTION_TIMEDOUT message=AMQ219013: Timed out waiting to receive cluster topology. Group:null]
Old Implementation
private ConnectionFactory createConnectionFactory(SyncProperties.SmpJmsServer jmsServer) {
final String className = "org.hornetq.core.remoting.impl.netty.NettyConnectorFactory";
Map<String, Object> params = new HashMap<String, Object>();
params.put("host", getJmsHost());
params.put("port", getJmsPort());
TransportConfiguration transportConfiguration = new TransportConfiguration(className, params);
HornetQJMSConnectionFactory hornetQJMSConnectionFactory = new HornetQJMSConnectionFactory(false, transportConfiguration);
hornetQJMSConnectionFactory.setConnectionTTL(300000);
hornetQJMSConnectionFactory.setConsumerWindowSize(0);
UserCredentialsConnectionFactoryAdapter adapter = new UserCredentialsConnectionFactoryAdapter();
adapter.setTargetConnectionFactory(hornetQJMSConnectionFactory);
adapter.setUsername(getJmsUsername());
adapter.setPassword(getJmsPassword());
CachingConnectionFactory smpCachingConnectionFactory = new CachingConnectionFactory(adapter);
return smpCachingConnectionFactory;
}
New Implementation
public ConnectionFactory createActiveMQJMSConnectionFactory() {
ActiveMQJMSConnectionFactory activeMQJMSConnectionFactory = new ActiveMQJMSConnectionFactory(false, amqTransportConfiguration());
activeMQJMSConnectionFactory.setConnectionTTL(300000);
activeMQJMSConnectionFactory.setConsumerWindowSize(0);
UserCredentialsConnectionFactoryAdapter adapter = new UserCredentialsConnectionFactoryAdapter();
adapter.setTargetConnectionFactory(activeMQJMSConnectionFactory);
adapter.setUsername(getJmsUsername());
adapter.setPassword(getJmsPassword());
CachingConnectionFactory smpCachingConnectionFactory = new CachingConnectionFactory(adapter);
return smpCachingConnectionFactory;
}
#Bean("amqTransportConfiguration")
public TransportConfiguration amqTransportConfiguration() {
return new TransportConfiguration("org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnectorFactory", getParams());
}
static Map<String, Object> getParams() {
Map<String, Object> params = new HashMap<String, Object>();
params.put("host", getJmsHost());
params.put("port", getJmsPort());
return params;
}
Thanks for the help.
Although ActiveMQ Artemis is based on the HornetQ code-base an ActiveMQ Artemis client won't be able to talk with a HornetQ broker. Each client sends a protocol/client identifier when it connects. This ID is different between ActiveMQ Artemis clients and HornetQ clients. A HornetQ broker will not recognize the ID sent by the ActiveMQ Artemis client and therefore will not complete a handshake.
That said, we have worked to ensure that HornetQ clients can still talk to an ActiveMQ Artemis broker. That's what the org.apache.activemq.artemis.core.protocol.hornetq.HornetQProtocolManager provides.
In any case, upgrading your client won't really do much for you anyway. If you want to upgrade anything I recommend you upgrade EAP or even move to a standalone version of ActiveMQ Artemis so you can get the latest fixes & features.

Connect to MQ using the secure keys (public & private key)

I am able to establish connection to QManager using unsecure channel and was able to do what ever I want to do.
But now I am trying to connect to the same QManage through secure channel; I have got the Security keys ( both public and private key ) generate from MQ server but I am not sure how to incorporate the key and establish MQ connection.
I googled and found some answer which suggested the below approach but it didn't work.
System.setProperty("javax.net.ssl.trustStore","path to public key");
System.setProperty("javax.net.ssl.keyStore","path to private key");
Exception I got.
com.ibm.mq.MQException : MQJE001: Completion Code '2', Reason '2537'.
I would appreciate some guidance or sample code on how to connect to QManager using security key.
#JoshMc -- please find answer to your question below
Are you using IBM MQ Classes for Java or IBM MQ Classes for JMS?
I am using IBM MQ Classes for Java
What version of IBM MQ are the jar files you are using from?
Version 7
What version of MQ is the queue manager you are attempting to connect to?
Version 8
Are you attempting to have MQ validate a client cert (Check the value of SSLCAUTH on the SVRCONN channel)
Yes. I have public and private key generated from MQ server by MQ Admin (I have no access to MQ server) and need to use it to connect to the MQ server.
Paste any errors that show up in the queue managers AMQERR01.LOG when you attempt to connect.
I don't have access to the log file.
Below is working code; I am able to connect to unsecure channel and send message
public void MQSender(){
MQQueueManager QMgr = null;
try {
MQEnvironment.hostname = "hostname";
MQEnvironment.channel = "UNSECURE";
//MQEnvironment.channel = "SECURE";
MQEnvironment.port = 8080;
QMgr = new MQQueueManager("QManager");
int openOptions = MQConstants.MQOO_OUTPUT;
MQQueue queue = QMgr.accessQueue("QNAME",openOptions);
MQPutMessageOptions pmo = new MQPutMessageOptions();
pmo.options = MQConstants.MQPMO_LOGICAL_ORDER | MQConstants.MQPMO_SYNCPOINT;
MQMessage message = new MQMessage();
message.writeString("TEST");
queue.put(message, pmo);
QMgr.commit();
} catch (Exception e) {
if(QMgr!=null){
try {
QMgr.backout();
} catch (MQException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
}
}
You are missing the option specifying the ciphersuite to use from your code:
MQEnvironment.sslCipherSuite
This knowledge center article may help: https://www.ibm.com/support/knowledgecenter/SSFKSJ_7.5.0/com.ibm.mq.dev.doc/q031220_.htm
And do you have your trusted certs and private cert/key pair in JKS stores?
These should be more like:
System.setProperty("javax.net.ssl.trustStore","path to JKS file containing certificates required to validate server certificate");
System.setProperty("javax.net.ssl.keyStore","path to JKS file containing certificate and private key of the client");

Connection is not established between Eclipse Paho and IBM Websphere MQ

I am novice user to IBM MQ. Basically I want to establish the connection between Client(Eclipse Paho) and IBM MQ Queue Manager.
I have performed the following steps:
I have installed the IBM MQ v.9.0
Created a Queue Manager
Started a Queue Manager as a service with the port number(1414)
Create a Server channel and assigns this with created Queue Manager.
At Client side:
Downloadd Eclipse Paho, which is MQTT Java client.
try to small program to connect with started Queue Manager.
Followinig is the program.
import java.util.logging.Logger;
import org.eclipse.paho.client.mqttv3.MqttAsyncClient;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
public class MQMTTFactory {
private static Logger log = Logger.getLogger(MQMTTFactory.class.getName());
private MQMTTFactory() {
}
static final String BROKER_URL = "tcp://<<Ipaddress>>:1234";
static final String M2MIO_DOMAIN = "<Insert m2m.io domain here>";
static final String M2MIO_STUFF = "things";
static final String M2MIO_USERNAME = "Guest";
static final String M2MIO_PASSWORD_MD5 = "<m2m.io password (MD5 sum of password)>";
static MqttClient myClient = null;
public static MqttClient getMqttClient() {
MqttConnectOptions connOpt;
if (myClient == null) {
connOpt = new MqttConnectOptions();
connOpt.setCleanSession(true);
connOpt.setKeepAliveInterval(3000);
connOpt.setUserName(M2MIO_USERNAME);
// connOpt.setPassword(M2MIO_PASSWORD_MD5.toCharArray());
// Connect to Broker
try {
myClient = new MqttClient(BROKER_URL,
MqttAsyncClient.generateClientId(), new MemoryPersistence());
myClient.connect(connOpt);
} catch (MqttException e) {
log.severe("Client connection to the MQTT Broker is failed");
e.printStackTrace();
System.exit(-1);
}
}
return myClient;
}
}
But the above program is failed to establish the connection with the server.
the following is the error while running the above program.
Unable to connect to server (32103) - java.net.ConnectException: Connection refused: connect
Could any body tell me what might be the wrong? or Any suggestions.
The Eclipse Paho client only works with the MQTT Protocol. This is a topic based pub/sub protocol and does not support Message Queues.
While IBM-MQ can support MQTT it is not enabled by default.
I suggest you read the following 2 articles to get a better understanding
https://www.ibm.com/developerworks/community/blogs/aimsupport/entry/what_is_mqtt_and_how_does_it_work_with_websphere_mq?lang=en
https://www.ibm.com/support/knowledgecenter/en/SS9D84_1.0.0/com.ibm.mm.tc.doc/tc00110_.htm

Issue connecting to a remote JMS queue from standalone client

I am new to JMS and have an issue connecting to a remote JMS queue from my standalone client. Any hints on resolving this issue would be highly appreciated.
Right now I have a JavaFX standalone application that runs on multiple clients and a glassfish server 3.1.2.2 running on a remote Unix machine. I am having a hard time pushing messages from my standalone app on to the queue that is residing on the server.
Client Mc: Windows PC (No server installed)
Remote Mc: Unix (GlassFish 3.1.2.2 installed)
JMS resources on the server:
JMS Destination Resource
JNDI Name: jms/ReferralQueue
Physical Destination Name: ReferralQueue
Resource Type: javax.jms.Queue
JMS Connection Factory
Pool Name: jms/ReferralConnectionFactory
JNDI Name: jms/ReferralConnectionFactory
Resource Type: javax.jms.QueueConnectionFactory
JMS Service Type: Embedded
JMS Message Store Type: File
Client Side Code to connect to the server:
jms.properties:
org.omg.CORBA.ORBInitialHost=UNIX MC URL
org.omg.CORBA.ORBInitialPort=7676
Service Locator design to implement resource caching
public class JMSServiceLocator {
private static JMSServiceLocator singletonService = null;
private static QueueConnectionFactory qFactory;
private static Queue queue;
private InitialContext context;
private static Properties properties = new Properties();
private Map cache;
static {
try {
singletonService = new JMSServiceLocator();
} catch (Exception e) {
//error handling
}
}
private JMSServiceLocator() {
try {
loadProperties();
context = new InitialContext(properties);
cache = Collections.synchronizedMap(new HashMap());
} catch (Exception e) {
//error handling
}
}
public static JMSServiceLocator getInstance() {
return singletonService;
}
public QueueConnectionFactory getQueueConnectionFactory() {
String qConnFactoryName = "jms/ReferralConnectionFactory";
qFactory = null;
try {
System.out.println("/********************Comment after Testing*****************************/");
Hashtable env = context.getEnvironment();
System.out.println("**env.size::" + env.size());
Enumeration names = env.keys();
while (names.hasMoreElements()) {
String str = (String) names.nextElement();
System.out.println("**" + str + "=" + env.get(str));
}
System.out.println("/**********************************************************************/");
if (cache.containsKey(qConnFactoryName)) {
qFactory = (QueueConnectionFactory) cache.get(qConnFactoryName);
} else {
qFactory = (QueueConnectionFactory) context.lookup(qConnFactoryName);
cache.put(qConnFactoryName, qFactory);
}
} catch (Exception e) {
//error handling
}
return qFactory;
}
public Queue getQueue() {
String queueName = "jms/ReferralQueue";
queue = null;
try {
if (cache.containsKey(queueName)) {
queue = (Queue) cache.get(queueName);
} else {
queue = (Queue) context.lookup(queueName);
cache.put(queueName, queue);
}
} catch (Exception e) {
//error handling
}
return queue;
}
private static void loadProperties() {
//Load jms properties
}
}
Eventually sending message to the server:
JMSServiceLocator jmsLocator = JMSServiceLocator.getInstance();
QueueConnectionFactory qConnFactory = jmsLocator.getQueueConnectionFactory();
qConnection = qConnFactory.createQueueConnection();
session = qConnection.createSession(false, ession.AUTO_ACKNOWLEDGE);
queue = jmsLocator.getQueue();
// Push and publish the message
messageProducer = session.createProducer(queue);
textMessage = session.createTextMessage();
textMessage.setText(message);
messageProducer.send(textMessage);
Hmmm... Now I observe a strange behavior...
I created a new GlassFish 3.1.2.2 server instance on the client machine with no jndi, no connection factories, and no jms queues what so ever.
I have started this server instance and executed the standalone client application. Strangely, everything works fine and the message is directly being pushed to the remote queue.
Did any one come across this kind of issue? I am suspecting that probably the application is loading the dependent GlassFish jars in the classpath only when a server instance (could be any random instance, totally unrelated) is started.
I have the following jars in my standalone application classpath:
*C:\Program Files\glassfish-3.1.2.2\glassfish\lib\gf-client.jar
*C:\Program Files\glassfish-3.1.2.2\glassfish\lib\appserv-rt.jar
*C:\Program Files\glassfish-3.1.2.2\glassfish\lib\install\applications\jmsra\imqbroker.jar
*C:\Program Files\glassfish-3.1.2.2\glassfish\lib\install\applications\jmsra\imqjmsra.jar
I have also posted this on Oracle JMS and GlassFish forums and haven't got a solution. Any help on this issue would be highly appreciated.
Thanks.
I think you found out by now what the problem was:
the JMS client jars were missing on the client (Client Mc: Windows PC (No server installed)).
You don't need a full Glassfish installation on the clients but only the JMS client jars (gf-client.jar) plus all the other jars referenced by gf-client.jar.

Resources