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();
Related
Can someone please help me how to fetch the Enqueue Count for a particular queue in Active MQ?
Here is my code:
BrokerService broker = new BrokerService()
String queueName= "queue.Test"
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory('ACTIVEMQ URL')
Connection connection = connectionFactory.createConnection()
connection.start()
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)
ActiveMQDestination requestDestination = session.createTopic(queueName)
QueueViewMBean view = (QueueViewMBean)broker.getDestination(requestDestination)
println("Count = "+view.getEnqueueCount())
But this does not seem to be working. How can I fix it?
You appear to be trying to cast ActiveMQ JMS client resources into JMX MBeans which of course would never work. You need to use JMX to access the management capabilities of the broker.
Some examples of using the MBeans can be found in the unit tests of the broker
I want to know the number of queues on my ActiveMQ and I have this Code, which works with my local installation of ActiveMQ:
ActiveMQSslConnectionFactory connectionFactory = new ActiveMQSslConnectionFactory(url);
//connectionFactory.setTrustStore("truststore.ts");
//connectionFactory.setTrustStorePassword("password");
ActiveMQConnection connection = (ActiveMQConnection) connectionFactory.createConnection();
connection.start();
DestinationSource destinationQueues = connection.getDestinationSource();
Set<ActiveMQQueue> queues = destinationQueues.getQueues();
System.out.println(queues.size());
But when I'm using it on our ActiveMQ on the server (with the truststore) it will return zero queues. I know that the connection works because I can consume messages from one exact queue. I already tried tips from other threads like sleep but it still won't work.
Is it maybe because of the SSL Connection, do I have to change something in this case? Thank you in advance.
Found the problem. It's a setting of the ActiveMQ which is found in the activemq.xml. The part advisorySupport="false" caused the problem. After commenting this setting out, the query returns the true amount of queues.
The Destination Source bits make no guarantee of timely return of the full set of Queues etc. It could be you have configured the SSL connector on the broker differently or it could just be that due to the much slower throughput of SSL on JDK implementations the data hasn't arrived.
In our project we are maintaining our own DB connection pool.
For resolving the issue 'java.sql.SQLRecoverableException: Io exception most of people has suggested to use standard connection pool like apache dbcp.
I am wondering what is the logic those standard pooling mechanism will perform during connection reset?
How do DBConnectionPool know that DB connection has timed out? since we know conn.isClosed() won't help here.
Is it each db connection will have one tcp client socket with DB server?
Finally is it advisable; whenever i return the connection to the pool; pool should close the connection; if the connection is existing more than ~10 mins from it is returned?
[~10 mins server side conn timeout variable]
Kindly answer all my questions.
I am answering this question assuming that you made use of Apache DBCP for connection pooling by using org.apache.commons.pool.impl.GenericObjectPool, org.apache.commons.dbcp.DataSourceConnectionFactory, org.apache.commons.dbcp.PoolableConnectionFactory and org.apache.commons.dbcp.PoolingDataSource classes.
I am wondering what is the logic those standard pooling mechanism
will perform during connection reset?
If GenericObjectPool.testOnBorrow and GenericObjectPool.testOnReturn are set true to The Connection will be validated whether it is active or not using a validationQuery set in PoolableConnectionFactory. If the validation is failed the Connection object is dropped and new one is created and added to the pool
How do DBConnectionPool know that DB connection has timed out? since
we know conn.isClosed() won't help here. Same mechanism as above
Is it each db connection will have one tcp client socket with DB
server? Yes
Finally is it advisable; whenever i return the connection to the
pool; pool should close the connection; if the connection is existing
more than ~10 mins from it is created? [~10 mins server side conn
timeout variable] If you think it should will not create unneccessary network traffic and if you have special reason to do that. You can do it. By setting minEvictableIdleTimeMillis in GenericObjectPool along with timeBetweenEvictionRunsMillis if you want to remove based on idle time
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.
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.