I'm trying to reuse a JMSContext to send multiple messages using the same context as shown in this IBM MQ tutorial.
context = cf.createContext();
destination = context.createQueue(QUEUE_NAME);
producer = context.createProducer();
for (int i = 1; i <= 5000; i++) {
try {
TextMessage message = context.createTextMessage("Message " + i + ".\n");
producer.send(destination, message);
} catch (Exception ignore) {}
}
context.close();
Say the connection is dropped at some point. Will the context auto recovers or will I need to reconstruct the context again?
UPDATE --
This is how the current connection factory is being constructed:
JmsFactoryFactory ff = JmsFactoryFactory.getInstance(JmsConstants.WMQ_PROVIDER);
JmsConnectionFactory cf = ff.createConnectionFactory();
cf.setStringProperty (CommonConstants.WMQ_HOST_NAME, config.getHost());
cf.setIntProperty (CommonConstants.WMQ_PORT, config.getPort());
cf.setStringProperty (CommonConstants.WMQ_CHANNEL, config.getChannel());
cf.setIntProperty (CommonConstants.WMQ_CONNECTION_MODE, CommonConstants.WMQ_CM_CLIENT);
cf.setStringProperty (CommonConstants.WMQ_QUEUE_MANAGER, config.getQueueManager());
cf.setBooleanProperty (JmsConstants.USER_AUTHENTICATION_MQCSP, false);
cf.setIntProperty (JmsConstants.PRIORITY, 0);
return cf.createContext();
Reconnect works like this (see also comment of #JoshMc):
On the client, set the reconnect option like this:
cf.setIntProperty(CommonConstants.WMQ_CLIENT_RECONNECT_OPTIONS, CommonConstants.WMQConstants.WMQ_CLIENT_RECONNECT);
On the server, stop the queue manager like this:
endmqm -r
Have u tried with creating JMSContext from existing one?
JMSContext#createContext(int sessionMode)
It will create new JMSContext but reuse the same connection.
Reference:
https://www.ibm.com/support/knowledgecenter/en/SSFKSJ_9.1.0/com.ibm.mq.pro.doc/intro_jms_model.htm
https://docs.oracle.com/javaee/7/api/javax/jms/JMSContext.html
Related
I have been reading the documentation for virtual destinations here: http://activemq.apache.org/virtual-destinations.html
But I hit a bit of a snag, when I send to a topic it does not seem to follow the client id name as described on the document
My setup on the active mq is:
<destinationInterceptors>
<virtualDestinationInterceptor>
<virtualDestinations>
<virtualTopic name="Destination.>" prefix="Target.*." selectorAware="false" />
</virtualDestinations>
</virtualDestinationInterceptor>
</destinationInterceptors>
The code above describes that when I send to a Destination.Status topic with a ClientId of CustomerA.
It should send only to Target.CustomerA.Destination.Status if understand correctly, but what's happening is it's sending to Target.CustomerA.Destination.Status and Target.CustomerB.Destination.Status so basically fanning out messages to queues and ignoring the client id.
I did not see any further documentation about how to configure it, i was wondering if anyone else encountered this ?
Am I missing something here ?
Below is my producer if it's helpful.
public static class HelloWorldProducer implements Runnable {
public void run() {
try {
// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61617");
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.setClientID("CustomerA");
connection.start();
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createTopic("Destination.Status");
// Create a MessageProducer from the Session to the Topic or Queue
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Create a messages
String text = "Hello world! From: " + Thread.currentThread().getName() + " : " + this.hashCode();
TextMessage message = session.createTextMessage(text);
// Tell the producer to send the message
System.out.println("Sent message: "+ message.hashCode() + " : " + Thread.currentThread().getName());
producer.send(message);
// Clean up
session.close();
connection.close();
}
catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}
}
Any inputs will be beneficial.
The sender in this scenario has no real effect on the routing at the broker whether or not you've set a ClientID as it is just sending to a named Topic, in this case "Destination.Status". The configuration on the broker controls the routing and in your case you've configured "Destination.>" so any Queue consumer that comes along and subscribes to a Queue that matches the configuration you've set. So in your case I'd guess you have one consumer subscribing to Queue (Target.CustomerA.Destination.Status) and one to Queue (Target.CustomerB.Destination.Status) which then causes any message sent to the Topic to be fanned out to both.
If you want competing consumers then you'd need to subscribe both to Target.CustomerA.Destination.Status and then the broker would round-robin dispatch the sent message to either of the active subscribers.
I have used .net C# (IBM MQ version 9.1.5) to pull messages from the queue. So I have no issues connecting to the queue and getting messages.
I have read that there is the concept of transactions Distributed Transactions.
I tried the following:
var getMessageOptions = new MQGetMessageOptions();
getMessageOptions = new MQGetMessageOptions();
getMessageOptions.Options += MQC.MQGMO_WAIT + MQC.MQGMO_SYNCPOINT;
getMessageOptions.WaitInterval = 20000; // 20 seconds wait
Transaction oldAmbient = Transaction.Current;
using (var tx = new CommittableTransaction())
{
try
{
int i = queue.CurrentDepth;
Log.Information($"Current queue depth is {i} message(s)");
var message = new MQMessage();
queue.Get(message, getMessageOptions);
string messageStr = message.ReadString(message.DataLength);
Log.Information(messageStr);
tx.Commit();
}
catch (MQException e) when (e.Reason == 2033)
{
// Report exceptions other than "no messages in the queue"
Log.Information("No messages in the queue");
tx.Rollback();
}
catch (Exception ex)
{
Log.Error($"Exception when trying to capture a message from the queue: {ex.Message}");
tx.Rollback();
}
I am getting an error code of 2035.
Looking at the documents on Recovering Transactions, where does the "SYSTEM.DOTNET.XARECOVERY.QUEUE" live, is it on the queuemanger?
Do I need to get permissions enabled on this?
Also I see that Microsoft Distributed Transaction Manager is mentioned, is this something that we need to have running on the local host in order for distributed transactions to work?
If MQ Distributed transactions feature is being used then the user running the application should have the authority to "SYSTEM.DOTNET.XARECOVERY.QUEUE".If a transaction is incomplete "SYSTEM.DOTNET.XARECOVERY.QUEUE" queue holds the information of incomplete transaction as message in that queue,which later can be used to resolve the transaction.
Based on your scenario which you had put in comments i.e "we want to just save the message to a file. My thinking is if there is a problem with that, I could roll back the transaction." .If MQ is the only resource manager then you don't have to use Distributed transactions. Getting a message under syncpoint can also be used instead of Distributed Transactions. Distributed Transactions will be useful if more than one resource manager is being used.
To get a message under syncpoint following sample code can be used by updating hostname,channel,port,queue and queue manager name:
var getMessageOptions = new MQGetMessageOptions();
getMessageOptions = new MQGetMessageOptions();
getMessageOptions.Options += MQC.MQGMO_WAIT + MQC.MQGMO_SYNCPOINT;
getMessageOptions.WaitInterval = 20000; // 20 seconds wait
Hashtable props = new Hashtable();
props.Add(MQC.HOST_NAME_PROPERTY, "localhost");
props.Add(MQC.CHANNEL_PROPERTY, "DOTNET.SVRCONN");
props.Add(MQC.PORT_PROPERTY, 3636);
MQQueueManager qm = new MQQueueManager("QM", props);
MQQueue queue = qm.AccessQueue("Q1", MQC.MQOO_INPUT_AS_Q_DEF);
try
{
var message = new MQMessage();
queue.Get(message, getMessageOptions);
//to commit the message
qm.Commit();
string messageStr = message.ReadString(message.DataLength);
}
catch (MQException e) when (e.Reason == 2033)
{
// Report exceptions other than "no messages in the queue"
Log.Information("No messages in the queue");
}
catch (Exception ex)
{
Log.Error($"Exception when trying to capture a message from the queue:
}
I am trying to connect to remote IBM MQ server. But i receive the error Cannot load the library mqjbnd.dll. I am not sure why bindings mode is getting used. A snippet of code used is pasted below. After reading the replies at various including stack overflow found out that client mode should be used for my scenario. But i am unable to configure the client mode. Any help on this will be greatly appreciated
// Create a connection to the QueueManager
System.out.println("Connecting to queue manager: " + qManager);
MQQueueManager qMgr = new MQQueueManager(qManager);
// Set up the options on the queue we wish to open
int openOptions = MQConstants.MQOO_INPUT_AS_Q_DEF | MQConstants.MQOO_OUTPUT;
// Now specify the queue that we wish to open and the open options
System.out.println("Accessing queue: " + qName);
MQQueue queue = qMgr.accessQueue(qName, openOptions);
When you are using client mode , series of properties will need to be set as you are connecting using TCP/IP . This for example will include , host , port and details that are needed for the program to connect to a QM over the network. An indicative example is here.
Hashtable<String, Object> mqKeyValueProps = new Hashtable<String, Object>();
mqKeyValueProps.put(CMQC.HOST_NAME_PROPERTY, hostName);
mqKeyValueProps.put(CMQC.PORT_PROPERTY, new Integer(portNumber));
mqKeyValueProps.put(CMQC.CHANNEL_PROPERTY, channelName);
mqKeyValueProps.put(CMQC.USER_ID_PROPERTY, userID);
mqKeyValueProps.put(CMQC.PASSWORD_PROPERTY, password);
try
{
MQQueueManager qMgr = new MQQueueManager(qManager,mqKeyValueProps);
int openOptions = MQConstants.MQOO_INPUT_AS_Q_DEF | MQConstants.MQOO_OUTPUT;
// Now specify the queue that we wish to open and the open options
System.out.println("Accessing queue: " + qName);
MQQueue queue = qMgr.accessQueue(qName, openOptions);
}
catch (com.ibm.mq.MQException mqex)
{
System.out.println("MQException cc=" +mqex.completionCode + " : rc=" + mqex.reasonCode);
}
I`m trying to look at the messages from a queue using a Browser.
Code is like:
javax.naming.InitialContext ctx = new javax.naming.InitialContext();
javax.jms.QueueConnectionFactory qcf = (javax.jms.QueueConnectionFactory)ctx.lookup('java:/XAConnectionFactory');
javax.jms.QueueConnection connection = qcf.createQueueConnection('admin', 'admin'); // qcf.createQueueConnection();
javax.jms.QueueSession session = connection.createQueueSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
connection.start();
// It is a "special" queue and it is not looked up from JNDI but constructed directly
javax.jms.Queue queue = (javax.jms.Queue)ctx.lookup('/queue/myQueue');
javax.jms.QueueBrowser browser = session.createBrowser(queue);
TreeMap<Date, javax.jms.Message> messageMap = new TreeMap<Date, javax.jms.Message>();
int counter = 0;
Enumeration<javax.jms.Message> enumeration = browser.getEnumeration();
while (enumeration.hasMoreElements()) {
counter++;
javax.jms.Message message = enumeration.nextElement();
messageMap.put(new Date(message.getJMSTimestamp()), message);
}
connection.stop();
ctx.close();
session.close();
connection.close();
The problem is that I always get only 1 message in the enumeration, even though when looking with the jmx-console and invoke listMessagesAsJSON I get tons of messages.
Any ideas on what am I doing wrong ?
It could be that you are hitting a bug as Sergiu said.
You could as a workaround define consumer-window-size on your connection factory differently. Maybe have a connection factory just for this use-case... or maybe upgrade the version of HornetQ.
When setting the consumer-window-size (like I did in my app) it seems that you can hit bug https://issues.jboss.org/browse/HORNETQ-691 .
I have a simple code to put 2 messages into a queue.
1) I set the connectionNameList with two servers.
2) Those two servers are independent, but have the same Queue Manager and Queue defined with same name, such as "QMgr" and "TEST.IN"
3) I set the setClientReconnectOptions(WMQConstants.WMQ_CLIENT_RECONNECT);
I hope when the first server is down, it should send the messages to 2nd one.
The test I did:
a) I send first message, sender.send(message); It worked.
b) sleep 30 seconds.
During this time, I shutdown the first server
c) then sleep done, try to send 2nd message, but it failed to send immediately
Further more, I tried more, I did try{} catch{} for 2nd message, and in the catch{}, I try to sender.send(message), it still fails.
Any idea why it is different than what I expected. I will really appreciate your reply.
public static void main(String[] args) throws Exception
{
MQQueueConnectionFactory cf = new MQQueueConnectionFactory();
cf.setConnectionNameList("10.230.34.191(1418),10.230.34.169(1418)");
cf.setQueueManager("QMgr");
cf.setTransportType(WMQConstants.WMQ_CM_CLIENT);
cf.setClientReconnectOptions(WMQConstants.WMQ_CLIENT_RECONNECT);
cf.setClientReconnectTimeout(600);
System.out.println("connect list " + cf.getConnectionNameList());
MQQueueConnection connection = (MQQueueConnection) cf
.createQueueConnection("mqm", "passwd");
MQQueueSession session = (MQQueueSession) connection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
MQQueue queue = (MQQueue) session.createQueue("queue:///TEST.IN");
MQQueueSender sender = (MQQueueSender) session.createSender(queue);
long uniqueNumber = System.currentTimeMillis() % 1000;
JMSTextMessage message = (JMSTextMessage) session.createTextMessage("SimplePTP "
+ uniqueNumber);
// Start the connection
connection.start();
sender.send(message);
System.out.println("Sent message:\\n" + message);
System.out.println("sleep 30 seconds");
Thread.sleep(30000);
uniqueNumber = System.currentTimeMillis() % 1000;
message = (JMSTextMessage) session.createTextMessage("SimplePTP " + uniqueNumber);
sender.send(message);
sender.close();
session.close();
connection.close();
System.out.println("\\nSUCCESS\\n");
}
Well this is the simplest test case and should have worked. How did you bring down the first queue manager? Did you down it with a -r option. Remember, without the -r option the clients will not reconnect when queue manager is ended with endmqm command.
endmqm -r <qm name>
Assuming you used -r option and it still did not work, then my suggestion would be to try the following:
Set an exception listener to know what is going on with reconnection. Exception listener would be invoked when the connection is broken and reconnection attempt starts till either reconnection is successful or fails. Exception listener sample code would be something like this:
conn.setExceptionListener(new ExceptionListener() {
public void onException(JMSException e) {
System.out.print(e);
}
});