I my application i have a remote ActiveMQ server and embedded ActiveMQ Server. When i send text messages it's working. But when i try to send Blob messages it throws a javax.jms.JMSException caused by java.net.MalformedURLException and java.lang.NumberFormatException.
My Broker URL
String broker1 = "tcp://localhost:7005?jms.blobTransferPolicy.defaultUploadUrl=http://localhost:61617";
where tcp://localhost:7005 is an embedded broker, and http://localhost:61617 is a remote broker.
My Producer code
File file=new File("C:/Users/xxx/Downloads/1234.txt");
ActiveMQConnection connection = ActiveMQConnection.makeConnection(broker);
connection.start();
ActiveMQSession session = (ActiveMQSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createQueue("test");
MessageProducer producer = session.createProducer(destination);
BlobMessage message = session.createBlobMessage(file);
System.out.println("upload started");
producer.send(message);
System.out.println("upload finished");
session.close();
connection.close();
URL should be like this embededbroker?jms.blobTransferPolicy.defaultUploadUrl=externalurl
In case of failure in embedded URL then external URL will be used.By default activemq provides a file server in this link http://(activemq server ip):portnumber/fileserver
For example if your activemq server running on localhost url should be http://localhost:8161/fileserver/
It
It supports HTTP or FTP or SCP or some other point-to-point protocol.
Related
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.
I am trying to integrate ActiveMQ within Tomcat. To that end, I used this tutorial (a little outdated, I know, but the best I found). I created a servlet that instantiates and starts a broker:
try {
broker = new BrokerService();
broker.addConnector("tcp://localhost:61616");
broker.start()
} catch (final Exception e) {
e.printStackTrace();
}
I then created a simple Hello World example with a single producer sending a persisted message to a queue and a consumer retrieving the message, which failed. Both, producer and consumer, were able to connect to ActiveMQ and the queue but the consumer did not receive any message. However, after restarting Tomcat (and consequently the broker), the consumer received the message previously sent. Any newly created messages were again stuck.
I tried some non-persisted messages, which all arrived as expected. When sending a combination of persisted and non-persisted messages, the non-persisted ones arrived immediately and the persisted ones required a restart.
This is my (very basic) code for the producer and consumer:
final ConnectionFactory conFactory = (ConnectionFactory) jndi.lookup("connectionFactory");
final Connection connection = conFactory.createConnection();
connection.start();
final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Destination destination = (Destination) jndi.lookup("MyQueue");
final MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
final TextMessage message = session.createTextMessage("Hello World!");
producer.send(message);
connection.close();
final ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
connectionFactory.setTrustAllPackages(true);
final Connection connection = connectionFactory.createConnection();
connection.start();
final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Destination destination = session.createQueue("APP.JMS.QUEUE");
final MessageConsumer consumer = session.createConsumer(destination);
final Message message = consumer.receive(1000);
System.out.println("Received: " + message);
consumer.close();
session.close();
connection.close();
I tried transacted, manual-acknowledge, and auto-acknowledge mode for the consumer. I also tried directly creating ConnectionFactory and Destination (as in the consumer code) as well as JNDI lookup (producer code). All yielded the same result.
I tried versions 5.15.12, 5.15.11, 5.14.5, 5.11.0 and 5.10.0.
Any pointers what I did wrong or where I can find a more recent tutorial? I am aware of the official docs regarding ActiveMQ and Tomcat but they seem to be missing the essential parts.
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.
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");
Where should I set MQMD message context in client WebsphereMQ?
MQQueueConnectionFactory mqConnectionFactory = new MQQueueConnectionFactory();
mqConnectionFactory.setHostName(producerProperties.getProperty("wmq.host"));
mqConnectionFactory.setPort(Integer.valueOf(producerProperties.getProperty("wmq.port")));
mqConnectionFactory.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
mqConnectionFactory.setQueueManager(producerProperties.getProperty("wmq.manager"));
mqConnectionFactory.setChannel("MyChannel");
/**
* Producer Section
*/
// Getting producer connection from the MQ server and starting it
Connection producerConnection = mqConnectionFactory.createConnection();
System.out.println("Going to start the producer connection...");
producerConnection.start();
// JMS messages are sent and received using a Session.
Session producerSession = producerConnection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
// Destination represents here our queue(testQ) on the MQ server.
Destination producerDestination = producerSession.createQueue(producerProperties.getProperty("wmq.queue.name"));
// MessageProducer is used for sending messages
MessageProducer producer = producerSession.createProducer(producerDestination1);
//create text message going to send
TextMessage sendingMessage = producerSession.createTextMessage("Hi Welcome");
sendingMessage.setJMSType(queueName);
System.out.println("Sending the message...");
//sending the message
producer.send(sendingMessage);
See the sample program. If you installed to the default location on Windows, it's at:
"C:\Program Files (x86)\IBM\WebSphere MQ\tools\jms\samples\simple\SimpleMQMDWrite.java"
Or on *NIX platforms at:
/opt/mqm/samp/jms/samples/simple/SimpleMQMDWrite.java
Go to the WMQ Information Center and look up JMSX* and JMS_IBM_* selectors.