How to access Queue for MQ7? - java

qMgr = new MQQueueManager(qManager);
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF
| MQC.MQOO_OUTPUT | MQC.MQOO_INQUIRE;
*queue = qMgr.accessQueue(queueName, openOptions);* //Here i need to change it for MQ7, as for Mq7 their is no Queue Manager Name.
System.out.println("Successfully registered");
Hi all,
I need to monitor the queue for IBM MQ7.. Currently we did for MQ6, but for MQ7 their no Queue Manager name and 'm stuck with this. Can anyone help me

I think I have understood your question:
First be clear that you can not connect to something that does not exist whether it is a server or anything. To connect an object that object must exist.
The same case applies to MQ also. To connect to a queue manager, that queue manager must be existing and running.
Coming to the snippet you mention: MQ Java API does not have a MQQueueManager constructor that does not take a queue manager name parameter. So queue manager name parameter is mandatory and not optional. But you can pass "" (blank) as the name of the queue manager to MQQueueManager constructor. In such a case the application will connect to a queue manager based on the host, port and channel parameters. So you must pass at least host, port and channel parameters.
Hope I have answered your question.
EDIT Sample Code
// Create a connection to the QueueManager
qManager = "";
System.out.println("Connecting to queue manager: " + qManager);
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, "localhost");
props.put(CMQC.PORT_PROPERTY, 1414);
props.put(CMQC.CHANNEL_PROPERTY, "SYSTEM.DEF.SVRCONN");
MQQueueManager qMgr = new MQQueueManager(qManager, props);

Related

Getting JMSException connecting to IBM MQ from Java client

We are trying to connect to IBM MQ from a Java client. We have generated .bindings for JNDI context using the JMSAdmin utility. When connecting to IBM MQ we are getting following exception:
ERR fmbaJMS JMSException: JMSWMQ0018: Failed to connect to queue manager '<queue manager name>' with connection mode 'Client' and host name 'null'
Host name parameter sent is null while .bindings file correctly have
mq/RefAddr/30/Content=localhost(51410)
mq/RefAddr/30/Type=CRSHOSTS
Entries pointing to localhost and port 51410.
Your .bindings file does not look right. What was the JMSAdmin command you used to create it?
MQ0018: Failed to connect to queue manager 'bt.qm.ccxp0'
Is that your queue manager name or QCF? Note: As per the IBM Best Practises, queue manager names should be in uppercase.
To define a QCF (Queue Connection Factory), you would do:
DEFINE QCF(myQCF) QMANAGER(MQA1) CHANNEL(TEST.CHL) HOSTNAME(127.0.0.1) PORT(1414) TRANSPORT(CLIENT) FAILIFQUIESCE(YES)
To define a JMS queue, you would do:
DEFINE Q(mqs.dev.test.q) QUEUE(TEST.Q1) QMANAGER(MQA1) TARGCLIENT(JMS) FAILIFQUIESCE(YES)
Then in your code, you would do the following to load the objects from the MQ JNDI:
Hashtable<String,String> env = new Hashtable<String,String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
env.put(Context.PROVIDER_URL, "file:/C:/JNDI-Directory");
Context ctx = new InitialContext(env);
QueueConnectionFactory cf = (QueueConnectionFactory) ctx.lookup("myQCF");
Queue q = (Queue) ctx.lookup("mqs.dev.test.q");

Getting the list of all the Queues under a Queue Manager?

I am working on a utility to browse/read all the messages from each of the queues in a Queue Manager. I am successful in creating a rest web service which can read the specified number of messages from the specified Queue.I am using MQ APIs in my classes to establish the connections.
MQQueueManager queueManager = new MQQueueManager(queueManagerName);
int openOptions = CMQC.MQOO_INQUIRE + CMQC.MQOO_FAIL_IF_QUIESCING + CMQC.MQOO_INPUT_SHARED;
My url looks like :
http://localhost:8090/queueManager/{queueManagerName}/queue/{queueName}?msgsToRead=-1&&saveMsgs=false
Now I want to create something which doesn't need to specify the queue name instead just take the Queue Manager name and read all the messages from each queue.
I want to a url like this :
http://localhost:8090/queueManager/{queueManagerName}
Can someone guide me to list all the Queues of a QueueManager using MQ API Classes.

How to find local transmission queue of remote MQ queue in Java?

in my Java application I get MQQueue object using
MQQueue tQueue = qManager.accessQueue(tqName, tqOptions);
The queue is a remote queue. Is there way to get corresponding local transmission queue ?
(Using MQ 7.5)
Thanks
Yes, using runmqsc console.
First on a command prompt run
runmqsc <qmgr>.
Once the console opens run
dis qr<remote q> XMITQ
to display the transmit queue used by the remote queue definition.
UPDATE
Another method is to use PCF classes.
PCFMessageAgent pcfma = new PCFMessageAgent("QM");
PCFMessage pcfCmd = new PCFMessage(MQConstants.MQCMD_INQUIRE_Q);
pcfCmd.addParameter(MQConstants.MQCA_Q_NAME, "Q.REMOTE");
PCFMessage[] pcfResponse = pcfma.send(pcfCmd);
String xmitQName = (String) pcfResponse[0].getParameterValue(MQConstants.MQCA_XMIT_Q_NAME);
System.out.println("XmitQ name " + xmitQName);
When MQ opens a queue it runs a name resolution process to resolve which local queue to open. If the app opens a QRemote, it generally resolves to a transmission queue.
Finding out the name of the resolved queue is easy. Just ask MQ for it after the queue is successfully opened:
public java.lang.String getResolvedQName( )

How To count Number of queues in ACTIVEMQ

I am trying to create an application which keeps on checking the number of queues up and running in activemq.
And Any way to check whether queue's are working or not i.e. if corrupted and not able to process messages.
Kindly suggest how to do it.
Thanks in Advance.
You can try following code.
public static void main(String[] args) throws Exception
{
// get the initial context
InitialContext ctx = new InitialContext();
// lookup the queue object
Queue queue = (Queue) ctx.lookup("queue/queue0");
// lookup the queue connection factory
QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.
lookup("queue/connectionFactory");
// create a queue connection
QueueConnection queueConn = connFactory.createQueueConnection();
// create a queue session
QueueSession queueSession = queueConn.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
// create a queue browser
QueueBrowser queueBrowser = queueSession.createBrowser(queue);
// start the connection
queueConn.start();
// browse the messages
Enumeration e = queueBrowser.getEnumeration();
int numMsgs = 0;
// count number of messages
while (e.hasMoreElements()) {
Message message = (Message) e.nextElement();
numMsgs++;
}
System.out.println(queue + " has " + numMsgs + " messages");
// close the queue connection
queueConn.close();
}
You can ask for stats using the statistics plugin on the broker and the plain JMS api. I.e. to count the number of messages on FOO.BAR, send an empty message to ActiveMQ.Statistics.Destination.TEST.FOO and specify the replyTo header. In the response message, which is of typ MapMessage, you can find the message counter.
Another way is to browse only the first message of the queue using a simple queue browser (similar to the way praveen_programmer suggests) and check the timestamp of that message. If it's older than some threshold, you might have a problem with that consumer. I.e. no messages has been processed in the last hour/minute/day.
Yet another way is to use JMX, or preferably the jolokia REST/HTTP management api.
Just query the destination using http and you get a queue depth back:
To query the queue "q" on localhost, use the following api (you need to supply the user/password for the web console):
http://localhost:8161/api/jolokia/read/org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName=q
Take a look at Advisory messages. You need to enable them in your config , but you can get a lot of useful about your current activemq instances info through simple JMS messaging. http://activemq.apache.org/advisory-message.html I was using them to highlight slow producer and consumer scenarios.

Glassfish - get all queues in a session

My question is pretty simple, but I am having a bad time finding a solution.
I want to be able to get all the queues in the server (or session, that would also be ok). Is that possible?
My situation is the following:
I am new to openMQ, Glassfish, activeMQ and so on. I developed a monitoring system for activeMQ in which I get all the existing queues and show to the user, so it can get information about number of messages and so on.
To do that, I have this code:
ActiveMQConnection.makeConnection("tcp://localhost:61616");
activeMQConnection.start();
//Get queues
DestinationSource destinationSource = activeMQConnection.getDestinationSource();
Set<ActiveMQQueue> queues = destinationSource.getQueues();
this last line gets all the queues for the connection, and this is exactly what I need. But this was my code for ActiveMQ.
Now the team decided to change to openMQ, and I have to adapt my monitoring system to be able to handle that. I would like to use LDAP so I can do it technology-independent. After a lot of research I came to this code:
ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup("cn=QueueConnectionFactory");
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue myQueue = session.createQueue("myQueue");
This is good, because it would be completely independente (I think, have to test it). But the problem is that I have to register the queue, something I would not like to do. I would like to get all the existing queues in the server without any need for registering, but I can't find any GetQueues() method or any way to mimic its behavior.
I also found out the the DestinationSource class in activeMQ inherits from MessageListener, but this class doesn't provides any similar method :(
Could you please help me?
thank you,
Oscar
I was able to do that using JMX, here is the code:
HashMap environment = new HashMap();
String[] credentials = new String[] { "user", "pass" };
environment.put(JMXConnector.CREDENTIALS, credentials);
JMXServiceURL url = new JMXServiceURL("URL");
// Get JMX connector, supplying user name and password
JMXConnector jmxc1 = JMXConnectorFactory.connect(url, environment);
// Get MBean server connection
MBeanServerConnection mbsc = jmxc1.getMBeanServerConnection();
ObjectName destMgrConfigName = new ObjectName(MQObjectName.DESTINATION_MANAGER_MONITOR_MBEAN_NAME);
// Create operation's parameter and signature arrays
Object opParams[] = {};
String opSig[] = {};
// Invoke operation
ObjectName[] objectNames = (ObjectName[]) mbsc.invoke(destMgrConfigName, DestinationOperations.GET_DESTINATIONS, opParams, opSig);
for (ObjectName objectName : objectNames) {
System.out.println(objectName.getCanonicalName());
System.out.println(objectName.getKeyProperty("name"));
}
more references here: http://forums.oracle.com/forums/thread.jspa?threadID=2129291&tstart=0

Categories

Resources