I want to receive some messages from IBM MQ based on java. So I write a very simple code to test what I want. Here is the sample code below.
Const.MQ_QMANAGER = "QM.CREDITWEB3T.PC";
Const.MQ_QUEUE_RECEIVE = "MQ.AIRPORTS";
MQSimpleConnectionManager myConnMan = new MQSimpleConnectionManager();
myConnMan.setActive(MQSimpleConnectionManager.MODE_AUTO);
MQQueueManager qMgr = new MQQueueManager(Const.MQ_QMANAGER,
myConnMan);
When I new the MQQueueManager. It’s throw an error message.
The error message is
MQJE001: Completion Code '2', Reason '2495'
May someone help me to figure out what’s wrong here? Thanks a lot.
(1) Your 1st posting is requesting that the connection to the queue manager be in "bindings mode". This means you MUST run the code on the same server where the queue manager is running.
(2) Your 2nd posting is requesting that the connection to the queue manager be in "client mode" (over the network i.e.TCP/IP). This means you can run the code on a remote server and connect to the remote queue manager using the network.
After many times error and searched for the answer. Finally I found the solution. You needed to settle environmental parameter for MQ. I am sorry about that because I am so new about MQ. Here is the sample code below.
// Host
MQEnvironment.properties.put(MQConstants.HOST_NAME_PROPERTY,
Const.MQ_HOST_NAME_PROPERTY);
// Port
MQEnvironment.properties.put(MQConstants.PORT_PROPERTY,
Const.MQ_PORT_PROPERTY);
// Channel
MQEnvironment.properties.put(MQConstants.CHANNEL_PROPERTY,
Const.MQ_CHANNEL_PROPERTY);
MQEnvironment.properties.put(MQConstants.CCSID_PROPERTY, XXX);
Related
In my Java application I am using the failover transport to connect to a local ActiveMQ broker:
failover:(tcp://0.0.0.0:61616)
I create one single connection that I reuse in the rest of the application:
ActiveMQConnection connection = (ActiveMQConnection) connectionFactory.createConnection();
In another part of the application when I receive some external call I need to send a message to the broker, and so, for doing that I create a new "Session":
Session locSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
When the broker is down my app tries to reconnect to the broker forever (this is the expected behavior I really want to have).
However, the problem is that if the broker is down and I receive a call that invokes the code that executes the connection.createSession(false, Session.AUTO_ACKNOWLEDGE) then my app hangs forever on this line of code waiting for the app to reconnect successfully to the broker and then create the session.
Please, do you know any way to check before I execute createSession if the connection object is trying to reconnect or it is really connected? If I am able to know this I could avoid the creation of the session if the app is not connected to the broker (only trying to reconnect) and therefore I would avoid to hanging on connection.createSession forever (I would raise an exception).
I wasn't able to find any property or method on ActiveMQConnection to gather this information.
The failover: url provides a setting startupMaxReconnectAttempts to prevent infinite retry when connecting to the broker the first time.
Also note-- If you want an exception to bubble up, that conflicts with requirement to have infinite retry. You would need to adjust the failover settings to match your intended behavior, by setting a max count or max time to perform retry, then throw an exception and unblock your caller.
For example, you could indicate you only want to retry for 5 minutes, then receive an exception to handle in the code to prevent the infinite blocking.
Thank you all for your help and suggestions. They helped me a lot in re-focusing the problem.
However I f found the answer to my question using the method "getTransport().isConnected()".
In our java mail (using Java Mail API) application we first connect to the mail server, fetch messages, process headers and then afterwards process the message bodies and attachments using pop3 as usual.
Session session = Session.getInstance(props, null);
Store store = session.getStore(urln);
store.connect();
Folder f = store.getFolder("INBOX");
f.open(READ);
Messages m = f.getMessages(..);
for (Message m : messages) {
if (!store.isConnected()) {
//raise exception
}
processSubject();
processFrom();
processBodyAndAttachments();
..
}
The implementation works fine on most environments, but on some customer the storeconnection gets lost during the process in the for loop. We can see the raises exception in the logs. My questions:
AFAIK, the mail server can sometimes reject new connections, but does
it terminate current living connections (may be becasue of too much
connections or disconnects old ones to give access to the new ones?)
When the store is disconnected, does the folder gets closed too?
Is it better to check the folder?
The connection may be lost everywhere in the for loop and it does not
seem to be a good practise to put isConnected check everywhere in the
loop, it will make the code dirty and also cause performance issues,
is it a good practise to put in a try catch block and check for
IOExceptions? (Folder closed) Or other suggestions? Which exceptions
should be handled? There may be some cases where the message is not
parseable but connection is healthy.
What about adding a disconnect listener?
Network connections can be broken for a variety of reasons. Your program always has to be prepared for the connection to drop at any time.
With POP3, there is only one connection, so if the connection is dropped the store should be disconnected and the folder should be closed.
If the Folder is open, check the Folder. Otherwise check the Store.
You need a strategy for handling failures. If you keep track of what messages have been successfully processed you may be able to restart the processing at the next message after a failure. A lot of the details depend on your environment and your application requirements.
A disconnect listener won't make this easier.
I need some help with the following problem:
I open a tcp-socket in the constructor then proceed to provide a object over an object output-stream to the server. I have no control over the server and don't get any response back.
How can I detect that the connection was lost? Will I always get the IOExeption-Error when trying to write? Because according to javadoc once a connection was successfully made most of the checks are basically useless to me.
Additionally what is the best way to reconnect a socket? Set the reference to "Null" then create a new one?
Here is my current approach:
I have a status-list in which I have the following statuses:
SocketSuccess; SocketFailure; MessageSuccess; MessageFailure;
My idea is kind of like a state-machine so check first what the last status was. If the connection was successfull or the last message was successfull then try to send the message. When I get a IOExeption then set the status MessageFailure, save the Message locally till I get a successfull connection again.
Or are there any recommended patterns for this kind of situation?
Clearing all your douts. If the connection with the server is lost then the client will throw IOException and that will kill the application but if you have handled the exception and tried to reconnect with the server and Re-establish the input output stream the your message function will start again. The predefined messages you are using will travel only when there is a connection between server and client. So when the connection is lost you will get IOException and when you handle that exception and try to reconnect a new input output stream should be established that will carry your messaging service.
I am attempting to send multiple commands to a device using the SSHJ library. There is the option of sending multiple commands in this format:
Command command = sshSession.exec("show line; show ip interface brief;");
This works, but it is not always usable in my case. I have found other suggestions such as the second answer here.
When I attempt this suggestion the first command works fine and then it cycles between this error:
net.schmizz.sshj.connection.ConnectionException: Broken transport; encountered EOF
...
Caused by: net.schmizz.sshj.transport.TransportException: Broken transport; encountered EOF
or
Exception in thread "main" java.lang.IllegalStateException: Not connected
at net.schmizz.sshj.SSHClient.checkConnected(SSHClient.java:713)
at net.schmizz.sshj.SSHClient.startSession(SSHClient.java:651)
The code used is:
sshSession = sshClient.startSession();
Command command = sshSession.exec("sho ip int brie");
System.out.println(IOUtils.readFully(command.getInputStream()));//Just to see the reply while testing
command.join(5, TimeUnit.SECONDS);
sshSession = sshClient.startSession();
Command command2 = sshSession.exec("sho line");
System.out.println(IOUtils.readFully(command2.getInputStream()));//Just to see the reply while testing
A note if needed, the device I am connecting to, and majority of devices that it will connect to are Cisco networking equipment.
Thank you for any assistance.
-Jarrod
Never found a resolution for the exact problem. But I worked around the issue by using the DefaultPTY and providing my own streams with all the data I wanted to send. Playing around with this example.
I am trying to connect to the MQ7 Server below is the code :
Hashtable props = new Hashtable();
// Change the host name to your host name. Leave it as it is if
// queue manager is on the same machine
props.put(CMQC.HOST_NAME_PROPERTY, serverConfig.server);
props.put(CMQC.PORT_PROPERTY, serverConfig.port);
props.put(CMQC.CHANNEL_PROPERTY, serverConfig.sChannel);
qMgr = new MQQueueManager(qManager, props);
System.out.println("Queue Manager : "+qMgr+" null");
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF
| MQC.MQOO_OUTPUT | MQC.MQOO_INQUIRE;
queue = qMgr.accessQueue(queueName, openOptions);
System.out.println("Successfully registered");
//creating msg
message = new MQMessage();
it's getting connected but after sending one message its throwing a error
" MQJE001: Completion Code '1', Reason '2068'.
com.ibm.mq.MQException: MQJE001: Completion Code '1', Reason '2068'.
strngError: No valid counters.
strngError: No valid counters.
at com.ibm.mq.MQManagedObject.inquire(MQManagedObject.java:269)
at com.ibm.mq.MQManagedObject.getInt(MQManagedObject.java:479)
at com.ibm.mq.MQQueue.getCurrentDepth(MQQueue.java:995)
at middlewarex.IBMmq.getQueueCount(IBMmq.java:227)
at middlewarex.ThreadScenario.run(ThreadScenario.java:94)
at java.lang.Thread.run(Thread.java:722)
pointing on the line
" public long getQueueCount() throws Exception{
return queue.getCurrentDepth();
} "
Kindly Help me
Did you look up Reason Code 2068 in the manual? Because if you did, you would have seen that using option MQOO_INQUIRE is invalid.
Secondly, why are you opening the queue for BOTH putting and getting messages? This is a VERY bad practice. You say you are sending (putting) a message to a queue, therefore, you should be opening the queue for output only.
Due to firewall I was facing the exception when sending the message. Please try telnet and check whether the firewall is blocking you from accessing the machine.