Queue connection - java

environment.put(Context.INITIAL_CONTEXT_FACTORY,QUEUE_CONTEXT);
System.out.println("QUEUE_URL -> " + QUEUE_URL);
environment.put(Context.PROVIDER_URL,QUEUE_URL);
try{
ctx = new InitialDirContext(environment);
String MYCF_LOOKUP_NAME = QUEUE_CONTEXT_FACTORY;
connectionFactory = (ConnectionFactory) ctx.lookup(MYCF_LOOKUP_NAME);
connection = ((MQQueueConnectionFactory) connectionFactory)
.createQueueConnection();
I dont know whther it correct or not.. It gives me connectivity issue
n the first program it asks for queue manager name but in the second program it doesn't require Queue Manager name. I need to replace the First program code with the second program.. Can anyone help me on this ..??

You're using JNDI here - JNDI is a store of Java Obects. For JMS this will be ConnectionFactorys and Destinations (Queues or Topics).
So you need to put into JNDI an Connection Factory as the code suggets you already have and also a Queue.
Would suggest if this is not clear why you need to do this I suggest searching for a JNDI tuturial and also a JMS one - to get the very basic background.

Related

ActiveMq not creating queue automaticlly

I create a destination like this:
Destination destination = session.createQueue("queue_name");
In this case if the queue named "queue_name" dont exist, it will be created.
I want to form a destination to a queue and in case it dont exist, i dont want to create it.
Is there a way to connent to a queue only if it exists?
I think you should be able to get a list of the available queues using DestinationSource from your connection. The you could look to see if the queue exists.
Havnt tried it, but think it looks like this:
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
ActiveMQConnection connection = (ActiveMQConnection)connectionFactory.createConnection();
DestinationSource ds = connection.getDestinationSource();
Set<ActiveMQQueue> queues = ds.getQueues();
You have to use the security feature in ActiveMQ to limit the users who are allowed to create destinations. You can then configure a set of destinations in the ActiveMQ config which are always created. See this page on the subject and also this page on configuring security.
You can either do it through security configuration of your client (Consumer/Producer).
Or alternatively you can do it programmatically by getting the list of queues available and only connecting if it is in the list. ActiveMQ provides a class for this, but its not part of JMS (so you'll be restricted to an ActiveMQ specific implementation).
http://activemq.apache.org/maven/5.5.0/activemq-core/apidocs/org/apache/activemq/advisory/DestinationSource.html

Rabbit Mq status check

In my spring, hibernate application RMQ is used. I have a url in this application to check the status the DB connection and the status of the threads. I also want to include the status check of RMQ connection from the application. Please guide me.
It is hard to give a specific answer as this depends on how you manage connections to the RabbitMQ broker in your application...
However in essence all you need to do is look at the IsOpen property on the connection
You probably have this code somewhere in your application:
ConnectionFactory factory = new ConnectionFactory();
factory.set...
Connection connection = factory.newConnection();
all you need to do to check the current state is:
bool connected = connection.IsOpen();

How to initialize activeMQ broker with failover

I need to send JMS messages to the following provider location:
failover:(tcp://amq.vip.ebay.com:61616,tcp://amqstby.vip.ebay.com:61616)?initialReconnectDelay=100&randomize=false&wireFormat.maxInactivityDuration=0
How to correctly initialize ConnectorFactory for it? Should I just do the follwing?
String url = "failover:(tcp://amq.vip.ebay.com:61616,tcp://amqstby.vip.ebay.com:61616)?initialReconnectDelay=100&randomize=false&wireFormat.maxInactivityDuration=0";
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Or the things are more tricky with this kind of provider urls?
That is the correct syntax. Be careful when turning off the inactivity monitor.

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

Simple JMS Clients on OS X

I haven't touched any J2EE stuff in years and I need to whip up a quick JMS client for a demo.
I'm using Eclipse, on OS X and I can't even get started because I can't seem to figure out how to get the required libraries.
This is supposed to be a simple stand alone application (not running in a container) that pulls messages from a topic.
Every JMS implementation has its own set of libraries that specify how you get the initial connection factory. If you have an existing server from which to pull messages, you need to examine the documentation of that server to determine where to find the libraries to put in your classpath and how to create your initial connection factory. If you want to create a server for the purposes of the demonstration, I recommend using an embedded Active MQ broker.
Once you have your connection factory, polling for messages from a topic is pretty straightforward. Here is some example code which can be called to drain a topic of its current messages.
// Implementation specific code
public void drainTopic(TopicConnectionFactory factory, String topicName, String subscriberId)
// set factory properties like the server ID
Connection conn = factory.createConnection();
conn.start();
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic(topicName);
MessageConsumer consumer = session.createDurableSubscriber(topic, subscriberId);
Message message;
while (null != (message = consumer.receive(1000))) {
// do work on the message
}
conn.close();
}
Note the use of a durable subscriber. This means that I don't have to try to maintain a single connection all the time and handle the errors if it times out somehow. But because the subscription is durable, the server knows to retain any messages the topic receives while I'm not connected and provide them on my next connection. This code would be the same regardless of the host OS. The only tricky part is the creation of the provider specific connection factory.

Categories

Resources