Page History
...
The code example below shows the complete class. The code includes creates a default TEXT message consisting of a the body for a JS7 /orders/add
API request.
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
package com.sos.jms.producer; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageProducer; import javax.jms.Session; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.log4j.Logger; public class SOSProducer { private static final Logger LOGGER = Logger.getLogger(SOSProducer.class); private static final String TEXT = "{\"controllerId\":\"testsuite\",\"orders\":[{\"workflowPath\":\"/JS7Demo/01_HelloWorld/jdHelloWorld\",\"scheduledFor\":\"now\"}],\"auditLog\":{}}"; private String uri; public SOSProducer(String uri) { this.uri = uri; } public void write(String text, String queueName) throws Exception { write(text, queueName, 5000L); } public void write(String text, String queueName, long ttl) throws Exception { Connection connection = null; Session session = null; try { ConnectionFactory factory = new ActiveMQConnectionFactory(uri); connection = factory.createConnection(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(queueName); MessageProducer producer = session.createProducer(destination); // 5 sec time to live for the producer for this showcase producer.setTimeToLive(ttl); Message message = null; if(text != null){ message = session.createTextMessage(text); } else{ message = session.createTextMessage(TEXT); } producer.send(message); } catch (Throwable e) { LOGGER.error("JMSException occurred while trying to write Message to Destination: "); throw e; } finally { if(session != null) { try { session.close(); } catch (JMSException e) { LOGGER.warn("JMSException occurred while trying to close the session: ", e); } } if (connection != null) { try { connection.close(); } catch (JMSException e) { LOGGER.warn("JMSException occurred while trying to close the connection: ", e); } } } } } |
...
This section describes how to establish a connection to a MQ server Message Queue Service and receive a message from a queue on this server. Furthermore it shows how to connect to a JOC Cockpit instance via HTTP to send an api API request:
- create a connection,
- create a session,
- create a destination.
- create a consumer,
- receive a message with the consumer,
- close the (MQ) connection,
- login to a JOC Cockpit instance via a HTTP REST API call,
- send a ./orders/add API request to a JOC Cockpit instance,
- close the connection.
...
Overview
Content Tools