ActiveMQ setting transport parameters programmatically - java

Is it possible to set the ActiveMQ transport parameters such as maxReconnectAttempts using a java API at the runtime?
In my case I'm creating the ActiveMQ connection factory initially by providing a basic failover url failover:
ActiveMQSslConnectionFactory connectionFactory = new ActiveMQSslConnectionFactory("(ssl://192.168.1.112:61617,ssl://192.168.1.112:61619)?randomize=false")
However later I would require to set transport parameter to this connection factory such as maxReconnectAttempts. Is it possible?

certainly, simply like this:
ActiveMQSslConnectionFactory connectionFactory = new ActiveMQSslConnectionFactory("failover:(ssl://192.168.1.112:61617,ssl://192.168.1.112:61619)?randomize=false&maxReconnectAttempts=Value")
All failover Transport Options can be set in the url
http://activemq.apache.org/failover-transport-reference.html
If you want to change the url later, you can call connectionFactory.setBrokerURL("newURL"), after that all new connections created will be configured with the new parameters of the url.
if you want to change that after the creation of the ConnectionFactory, keep in mind that a new instance of FailoverTransport is created for each new Connection based on the url parameters and each Connection holds an instance of his FailoverTransport, so to change his state you can access it like this :
((FailoverTransport) ((TransportFilter) ((TransportFilter) ((ActiveMQConnection) connection).getTransport()).getNext()).getNext())
.setMaxReconnectAttempts(10);
or more readable :
org.apache.activemq.transport.TransportFilter responseCorrelator = (TransportFilter) ((ActiveMQConnection) connection).getTransport();
TransportFilter mutexTransport = (TransportFilter) responseCorrelator.getNext();
FailoverTransport failoverTransport = (FailoverTransport) mutexTransport.getNext();
failoverTransport.setMaxReconnectAttempts(10);
to understand why all these casts, you can take a look at source code of this method :
org.apache.activemq.transport.failover.FailoverTransportFactory.doConnect(URI)
here https://github.com/apache/activemq/blob/master/activemq-client/src/main/java/org/apache/activemq/transport/failover/FailoverTransportFactory.java

Related

Restlet Client Internal Connector Error (1002)

I am using a restlet 2.1 client sever based architecture, my client times out within 1 minute after sending the request. and I get the following exception :
Internal Connector Error (1002) - The calling thread timed out while waiting for a response to unblock it.
at org.restlet.resource.ClientResource$1.invoke(ClientResource.java:1663)
at com.sun.proxy.$Proxy17.getTaskList(Unknown Source)....
My code is as below :
import org.restlet.resource.ClientResource;
ClientResource cr = new ClientResource(uri);
MyResource resource= cr.wrap(MyResource .class);
updateStatus = resource.updateData(Parameter);
i also tried this code :
Context context = new Context();
context.getParameters().add("socketTimeout", new String("180000"));
context.getParameters().add("socketConnectTimeoutMs", new String("180000"));
context.getParameters().add("idleTimeout", new String("180000"));
ClientResource cr = new ClientResource(context, url);
TasksResource resource = cr.wrap(TasksResource.class);
how should I configure my client resource to avoid timeout ?
The connection timeout for a Restlet client can be configured at the client connector level. If your call is within a component, you can get the client connector from it (class Client), otherwise you need to instantiate it. Configuring timeout can be done through its parameters (method getContext and then getParameters).
Parameters depend on the underlying connectors (HTTP client, ...).
The following links will help you to fix your issue:
Restlet HTTP Connection Pool
Restlet timeout
Hope it helps you,
Thierry

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

Categories

Resources