NamingException getting when tried to push the message to Activemq - java

i tried to publish message to activemq.but i am getting one naming exception in my java application. here is the code follows
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
props.setProperty(Context.PROVIDER_URL,"tcp://localhost:61616");
InitialContext ctx = new InitialContext(props);
// get the initial context
// InitialContext ctx = new InitialContext();
QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
// create a queue connection
QueueConnection queueConn = connFactory.createQueueConnection();
// lookup the queue object
Queue queue = (Queue) ctx.lookup("nixon");
i am getting three warning plus below one error like this
Exception in thread "main" javax.naming.NameNotFoundException: nixon
at org.apache.activemq.jndi.ReadOnlyContext.lookup(ReadOnlyContext.java:235)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.youtube.rest2.status.ProducerNewClient.main(ProducerNewClient.java:38)
can anyone tell why i am getting this error..?

You are most likely getting this error because you haven't configured the destinations in you JNDI configuration file. You can either add these definitions to your file or use the dynamic destination features of ActiveMQ.
For the easiest possible configuration with JNDI based programs, there are 2 dynamic contexts as follows
dynamicQueues
dynamicTopics
which allows you to lookup queues and topics using JNDI without any configuration.
e.g. if you use the following name to lookup into JNDI
dynamicQueues/FOO.BAR
you will get back an ActiveMQQueue of the name "FOO.BAR".
Read the JNDI Documentation at the ActiveMQ site for more.

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");

ActiveMQ setting transport parameters programmatically

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

Queue connection

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.

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