Setting the URL for a JMS connection - java

The JMS Hello World example shows producers and consumers sending and consuming messages; this is how they seem to create the connection:
ConnectionFactory cf = new com.sun.messaging.ConnectionFactory();
Connection connection = cf.createConnection();
How specify the address and parameters of the remote JMS server?
I want to create an encrypted and compressed connection to an OpenMQ server that is running on another machine. Ideally I would like to use both client and server authentication.

This post seems to provide an example using JMS server address.
This is how it creates a connection. They provide the address and also username and password (admin).
String addressList = "http://127.0.0.1:8080/imqhttp/tunnel";
com.sun.messaging.TopicConnectionFactory topicConnectionFactory = new com.sun.messaging.TopicConnectionFactory();
topicConnectionFactory.setProperty(com.sun.messaging.ConnectionConfiguration.imqAddressList, addressList);
javax.jms.Topic top;
javax.jms.Connection con = topicConnectionFactory.createTopicConnection("admin", "admin");

Related

Java WSS Connection Could not create transport exception

I'm so stuck about this.
I want to subscribe an ActiveMQ topic. ActiveMQ works on Centos machine, NOT LOCALHOST. I can consume messages with tcp, http protocols. Code;
public static void main(String[] args) throws JMSException {
PropertyUtils.loadPropertyFile();
Properties receiverProperties = PropertyUtils.getreceiverProperties();
// URL of the JMS server
String url = (String) receiverProperties.get("receiver.connection.url");
// Name of the queue we will receive messages from
String subject = (String) receiverProperties.get("receiver.topic.name");
// Getting JMS connection from the server
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();
// Creating session for getting messages
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Getting the topic
Destination destination = session.createTopic(subject);
// MessageConsumer is used for receiving (consuming) messages
MessageConsumer consumer = session.createConsumer(destination);
Message message = null;
// Here we receive the message.
while (true) {
message = consumer.receive();
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Received message '" + textMessage.getText() + "'");
}
}
// We will be using TestMessage in our example. MessageProducer sent us a
// TextMessage
// so we must cast to it to get access to its .getText() method.
// connection.close();
}
I want to use wss protocol. This is a must for me. When I changed url with wss://host:port getting;
Could not create Transport. Reason: java.io.IOException: createTransport() method not implemented!
So I checked the alternatives. People figure this out with Stomp over WS. My first achievement is wss connection.
Any recommendation will be appreciated!
The exception you're seeing is expected because the OpenWire JMS client you're using doesn't support WebSocket connections, and it doesn't really need to. WebSocket connections are really only relevant for clients running in a limited environment like a web browser. Web browsers don't support running Java clients.
If you really want to use STOMP over WebSockets then you'll have to use a STOMP client implementation that supports WebSockets (most do).
Keep in mind that ActiveMQ is a broker. It does not supply clients for all the protocols it supports. It only provides a JMS client because it is required in order to implement JMS. STOMP, for example, is a standardized protocol and anyone can implement a client which will work with any broker implementing STOMP. There are lots of STOMP client implementations available written in many different languages for many different platforms. Any good search engine should help you find one to fit your needs.

How to specify host, port and channel in ConnectionFactory without using JNDI

Am trying to create a standalone java application to send message to different application using JMS. My reference code uses queueConnectionFactory and all connection parameters are configured in WAS server and retrieved using JNDI. But I cannot use server in my standalone application. In this case, how can I get or connect to the same host, port and queue without using JNDI.
Below is the code I have tried so far
try {
//Set connection factory
ActiveMQConnectionFacotry connectionFactory = new ActiveMQConnectionFactory("");
//create connection with connection factory
Connection connection = connectionFactory.createConnection();
connection.start();
//create session from connection
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//create queue from session
Destination destination = session.createQueue("sample.Queuename");
//create messageProducer
MessageProducer producer = session.createProducer(destination);
My reference code
//#Resource
private static Queue queue;
private QueueConnection con;
private QueueSession session;
private QueueSender sender;
private static InitialContext ctx;
private static JAXBContext jaxb;
private static IBwLogContext logContext = BwLogContextFactory.create();
static{
try {
ctx = new InitialContext();
queue = (Queue) ctx.lookup("jms/sampleJNDI1");
qcf = (QueueConnectionFactory) ctx.lookup("jms/sampleJNDI2");
JMS configuration is server for jms/sampleJNDI2
Now how do I connect to same xxx,yyy,zzz without server. Can we put these details in properties file and use them. If so, how to do it or is there any other way. I have hard coded queue name copying from the server. But stuck with ConnectionFactory.
Thanks in advance
You seem to be using the class ActiveMQConnectionFactory. This classname is used by both ActiveMQ 5.x and ActiveMQ Artemis, but with different package names. For better or worse, though, the basic configuration is the same for both:
ActiveMQConnectionFactory qcf = new ActiveMQConnectionFactory
("tcp://" + host + ":" + port);
QueueConnection qc = qcf.createQueueConnection ("user", "password");
There are other properties you can set on the connection factory, but host, port, user, and password are probably the most used.
Both ActiveMQ 5.x and ActiveMQ Artemis support other protocols than their proprietary ones (OpenWire and Artemis Core, respectively). For example, you can use the Qpid JMS runtime library to connect using the AMQP protocol. This has a different means of configuration, as do all the other client runtime libraries you might use.
Here is one way to set up a connection factory for IBM MQ, using IBM's "Class for Java" runtime.
import com.ibm.msg.client.wmq.*;
import com.ibm.mq.jms.*;
MQQueueConnectionFactory qcf = new MQQueueConnectionFactory();
qcf.setHostName ("my_mq.acme.com");
qcf.setPort (1414);
qcf.setQueueManager ("QMA");
qcf.setChannel ("SYSTEM.DEF.SVRCONN");
qcf.setTransportType (WMQConstants.WMQ_CM_CLIENT);
QueueConnection qc = qcf.createQueueConnection ("user", "password");
And here is an example for Qpid-JMS, that provides AMQP wire protocol support for many message brokers:
QueueConnectionFactory qcf = new
org.apache.qpid.jms.JmsConnectionFactory
("amqp://" + host + ":" + port);
QueueConnection qc = qcf.createQueueConnection ("user", "password");
I'm not sure why, but using a JMS client runtime without JNDI is often not well-documented. It's always possible, in my experience, but sometimes a bit of digging is required, to find the relevant settings.

how to use connection pooling for creating publisher in JMs using activeMQ

I am using following code for creating publisher every time when i need to add message in topic i will run this , but some time it gives error
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://" + ip + ":" + port );
connection = connectionFactory.createConnection();
connection.setClientID(publisherName);
PooledConnectionFactory pf = new PooledConnectionFactory(connectionFactory);
pf.setMaxConnections(1000);
Session session = connection.createSession(false,session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic(TopicName);
MessageProducer messageProducer = session.createProducer(topic);
Error:
javax.jms.InvalidClientIDException: Broker: localhost - Client: AthenaPublisher already connected from tcp://127.0.0.1:44448
Using a pool when setting the client ID will inevitably lead to such errors as you can have only one connection with a given client ID connected to the broker at any given time. In your case you are configuring the pool for 1000 pooled connections so it will happen pretty often that a new connection will be created and the error you've given will be produced. You need to either not use pooling or use a pool with only 1 pooled connection that shares session level resources with you code.

is it possible to establish rabbitMQ connection with javax.jms (not using rabbitMQ jms client/ java client )? if yes how?

is it possible to establish rabbitMQ connection with javax.jms (not using rabbitMQ jms client/ java client )? if yes how (not with springboot).
You either need to use the driver or write your own driver. You should not expect anyone here to write it for you. What have you tried?
Yes it's possible. RabbitMQ provides a class implementing JMS Connection factory com.rabbitmq.client.ConnectionFactory:
ConnectionFactory connectionFactory = new Connection().newConnection();
// create a Connection
connection = connectionFactory.createConnection();
connection.setClientID(clientId);
// create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// create the Topic from which messages will be received
Topic topic = session.createTopic(topicName);
// create a MessageConsumer for receiving messages
messageConsumer = session.createConsumer(topic);
// start the connection in order to receive messages
connection.start();
Except for the first line (Factory creation), this is pure JMS code.

RabbitMQ Consumer connection to rabbitMQ in https domain

I need to receive messages of a queue, but this queue is inside in another machine(AWS instance) with https(https://www.mymachine.com/rabbitmq) but when I want to establish a connection to the queue I get a NullPointerException.
This is a part of code:
factory.setHost(https://www.mymachine.com/rabbitmq);
Connection connection = factory.newConnection();
channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, EXCHANGE_NAME, "");
RabbitMQ, by default, does not use HTTP protocol, it uses AMQP protocol.
you have to change the factory.setHost with the ip or hostname.
factory.setHost(yourmachine)
if you need an SSL connection please read:
https://www.rabbitmq.com/ssl.html it is very clear tutorial.

Categories

Resources