Search in sources :

Example 36 with MessageProducer

use of javax.jms.MessageProducer in project tesb-rt-se by Talend.

the class ImportRouteTest method sendMessage.

private void sendMessage(String queue, Object body) throws JMSException, TimeoutException, JAXBException {
    Connection con = tryTo("connect to jms broker", new Callable<Connection>() {

        public Connection call() throws Exception {
            return cf.createConnection();
        }
    }, 20000);
    Session sess = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination destination = sess.createQueue(queue);
    MessageProducer prod = sess.createProducer(destination);
    TextMessage message = sess.createTextMessage();
    message.setText(marshal(body));
    prod.send(message);
    prod.close();
    sess.close();
    con.close();
}
Also used : Destination(javax.jms.Destination) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TimeoutException(java.util.concurrent.TimeoutException) JMSException(javax.jms.JMSException) JAXBException(javax.xml.bind.JAXBException) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 37 with MessageProducer

use of javax.jms.MessageProducer in project oozie by apache.

the class TestHCatMessageHandler method testCacheUpdateByMessage.

/**
 * Test that message is processed to update the dependency map and mark
 * partition as available
 */
@Test
public void testCacheUpdateByMessage() {
    try {
        String actionId1 = "1234465451";
        String actionId2 = "1234465452";
        String actionId3 = "1234465453";
        String actionId4 = "1234465454";
        // add partition as missing
        HCatURI dep1 = new HCatURI("hcat://hcat.server.com:5080/mydb/mytbl/dt=20120101;country=us");
        HCatURI dep2 = new HCatURI("hcat://hcat.server.com:5080/mydb/mytbl/country=us;dt=20120101");
        HCatURI dep3 = new HCatURI("hcat://hcat.server.com:5080/mydb/mytbl/dt=20120102;country=us");
        HCatURI dep4 = new HCatURI("hcat://hcat.server.com:5080/mydb/mytbl/dt=20120102;country=us;state=CA");
        PartitionDependencyManagerService pdms = Services.get().get(PartitionDependencyManagerService.class);
        pdms.addMissingDependency(dep1, actionId1);
        pdms.addMissingDependency(dep2, actionId2);
        pdms.addMissingDependency(dep3, actionId3);
        pdms.addMissingDependency(dep4, actionId4);
        assertTrue(pdms.getWaitingActions(dep1).contains(actionId1));
        assertTrue(pdms.getWaitingActions(dep2).contains(actionId2));
        assertTrue(pdms.getWaitingActions(dep3).contains(actionId3));
        assertTrue(pdms.getWaitingActions(dep4).contains(actionId4));
        // construct message
        List<Map<String, String>> partitionsList = new ArrayList<Map<String, String>>();
        partitionsList.add(getPartitionMap("dt=20120101;country=us;state=CA"));
        partitionsList.add(getPartitionMap("dt=20120101;country=us;state=NY"));
        JSONAddPartitionMessage jsonMsg = new JSONAddPartitionMessage("thrift://" + dep1.getServer(), "", dep1.getDb(), dep1.getTable(), partitionsList, System.currentTimeMillis());
        Message msg = session.createTextMessage(jsonMsg.toString());
        msg.setStringProperty(HCatConstants.HCAT_EVENT, HCatEventMessage.EventType.ADD_PARTITION.toString());
        // test message processing
        HCatMessageHandler hcatHandler = new HCatMessageHandler("hcat.server.com:5080");
        hcatHandler.process(msg);
        assertNull(pdms.getWaitingActions(dep1));
        assertNull(pdms.getWaitingActions(dep2));
        assertTrue(pdms.getWaitingActions(dep3).contains(actionId3));
        assertTrue(pdms.getWaitingActions(dep4).contains(actionId4));
        // test message processing through JMS notification listener
        partitionsList.clear();
        partitionsList.add(getPartitionMap("dt=20120102;country=us;state=CA"));
        partitionsList.add(getPartitionMap("dt=20120102;country=us;state=NY"));
        jsonMsg = new JSONAddPartitionMessage("thrift://" + dep1.getServer(), "", dep1.getDb(), dep1.getTable(), partitionsList, System.currentTimeMillis());
        msg = session.createTextMessage(jsonMsg.toString());
        msg.setStringProperty(HCatConstants.HCAT_EVENT, HCatEventMessage.EventType.ADD_PARTITION.toString());
        HCatAccessorService hcatService = Services.get().get(HCatAccessorService.class);
        hcatService.registerForNotification(dep1, "hcat.topic1", hcatHandler);
        Topic topic = session.createTopic("hcat.topic1");
        MessageProducer producer = session.createProducer(topic);
        producer.send(msg);
        Thread.sleep(500);
        assertNull(pdms.getWaitingActions(dep3));
        assertNull(pdms.getWaitingActions(dep4));
        assertTrue(pdms.getAvailableDependencyURIs(actionId1).contains(dep1.getURI().toString()));
        assertTrue(pdms.getAvailableDependencyURIs(actionId2).contains(dep2.getURI().toString()));
        assertTrue(pdms.getAvailableDependencyURIs(actionId3).contains(dep3.getURI().toString()));
        assertTrue(pdms.getAvailableDependencyURIs(actionId4).contains(dep4.getURI().toString()));
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception: " + e.getMessage());
    }
}
Also used : HCatEventMessage(org.apache.hive.hcatalog.messaging.HCatEventMessage) JSONAddPartitionMessage(org.apache.hive.hcatalog.messaging.json.JSONAddPartitionMessage) Message(javax.jms.Message) HCatMessageHandler(org.apache.oozie.dependency.hcat.HCatMessageHandler) ArrayList(java.util.ArrayList) PartitionDependencyManagerService(org.apache.oozie.service.PartitionDependencyManagerService) JSONAddPartitionMessage(org.apache.hive.hcatalog.messaging.json.JSONAddPartitionMessage) HCatAccessorService(org.apache.oozie.service.HCatAccessorService) MessageProducer(javax.jms.MessageProducer) HCatURI(org.apache.oozie.util.HCatURI) Topic(javax.jms.Topic) Map(java.util.Map) Test(org.junit.Test)

Example 38 with MessageProducer

use of javax.jms.MessageProducer in project oozie by apache.

the class JMSSLAEventListener method sendJMSMessage.

protected void sendJMSMessage(SLAMessage slaMsg, String messageBody, String topicName, String messageFormat) {
    jmsContext = jmsService.createProducerConnectionContext(connInfo);
    if (jmsContext != null) {
        try {
            Session session = jmsContext.createThreadLocalSession(jmsSessionOpts);
            TextMessage textMessage = session.createTextMessage(messageBody);
            textMessage.setStringProperty(JMSHeaderConstants.EVENT_STATUS, slaMsg.getEventStatus().toString());
            textMessage.setStringProperty(JMSHeaderConstants.SLA_STATUS, slaMsg.getSLAStatus().toString());
            textMessage.setStringProperty(JMSHeaderConstants.APP_TYPE, slaMsg.getAppType().toString());
            textMessage.setStringProperty(JMSHeaderConstants.MESSAGE_TYPE, slaMsg.getMessageType().toString());
            textMessage.setStringProperty(JMSHeaderConstants.APP_NAME, slaMsg.getAppName());
            textMessage.setStringProperty(JMSHeaderConstants.USER, slaMsg.getUser());
            textMessage.setStringProperty(JMSHeaderConstants.MESSAGE_FORMAT, messageFormat);
            LOG.trace("Event related JMS text body [{0}]", textMessage.getText());
            LOG.trace("Event related JMS message [{0}]", textMessage.toString());
            MessageProducer producer = jmsContext.createProducer(session, topicName);
            producer.setDeliveryMode(jmsDeliveryMode);
            producer.setTimeToLive(jmsExpirationDate);
            producer.send(textMessage);
            producer.close();
        } catch (JMSException jmse) {
            LOG.error("Exception happened while sending event related jms message :" + messageBody, jmse);
        }
    } else {
        LOG.warn("No connection. Not sending message" + messageBody);
    }
}
Also used : JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 39 with MessageProducer

use of javax.jms.MessageProducer in project hazelcast-simulator by hazelcast.

the class Server method sendCoordinator.

public void sendCoordinator(SimulatorOperation op) {
    try {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("sending [" + op + "]");
        }
        Destination topic = session.createTopic("coordinator");
        MessageProducer producer = session.createProducer(topic);
        producer.setDeliveryMode(NON_PERSISTENT);
        Message message = session.createMessage();
        message.setStringProperty("source", selfAddressString);
        message.setStringProperty("payload", OperationCodec.toJson(op));
        message.setIntProperty("operationType", getOperationType(op).toInt());
        producer.send(message);
    } catch (JMSException e) {
        LOGGER.error(e);
    }
}
Also used : Destination(javax.jms.Destination) Message(javax.jms.Message) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer)

Example 40 with MessageProducer

use of javax.jms.MessageProducer in project OpenOLAT by OpenOLAT.

the class SearchClientProxy method doSearchRequest.

private Message doSearchRequest(Session session, Message message) throws JMSException {
    Destination replyQueue = acquireTempQueue(session);
    if (log.isDebug()) {
        log.debug("doSearchRequest replyQueue=" + replyQueue);
    }
    try {
        MessageConsumer responseConsumer = session.createConsumer(replyQueue);
        message.setJMSReplyTo(replyQueue);
        String correlationId = createRandomString();
        message.setJMSCorrelationID(correlationId);
        MessageProducer producer = session.createProducer(searchQueue_);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        producer.setTimeToLive(timeToLive_);
        if (log.isDebug()) {
            log.debug("Sending search request message with correlationId=" + correlationId);
        }
        producer.send(message);
        producer.close();
        Message returnedMessage = null;
        final long start = System.currentTimeMillis();
        while (true) {
            final long diff = (start + receiveTimeout_) - System.currentTimeMillis();
            if (diff <= 0) {
                // timeout
                log.info("Timeout in search. Remaining time zero or negative.");
                break;
            }
            if (log.isDebug()) {
                log.debug("doSearchRequest: call receive with timeout=" + diff);
            }
            returnedMessage = responseConsumer.receive(diff);
            if (returnedMessage == null) {
                // timeout case, we're stopping now with a reply...
                log.info("Timeout in search. Repy was null.");
                break;
            } else if (!correlationId.equals(returnedMessage.getJMSCorrelationID())) {
                // we got an old reply from a previous search request
                log.info("Got a response with a wrong correlationId. Ignoring and waiting for the next");
                continue;
            } else {
                // we got a valid reply
                break;
            }
        }
        responseConsumer.close();
        if (log.isDebug()) {
            log.debug("doSearchRequest: returnedMessage=" + returnedMessage);
        }
        return returnedMessage;
    } finally {
        releaseTempQueue(replyQueue);
    }
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) MessageProducer(javax.jms.MessageProducer)

Aggregations

MessageProducer (javax.jms.MessageProducer)1512 Session (javax.jms.Session)1302 MessageConsumer (javax.jms.MessageConsumer)987 Connection (javax.jms.Connection)915 TextMessage (javax.jms.TextMessage)888 Test (org.junit.Test)850 Message (javax.jms.Message)646 Queue (javax.jms.Queue)404 JMSException (javax.jms.JMSException)213 Destination (javax.jms.Destination)190 ConnectionFactory (javax.jms.ConnectionFactory)147 BytesMessage (javax.jms.BytesMessage)140 Topic (javax.jms.Topic)140 TopicSession (javax.jms.TopicSession)135 TopicConnection (javax.jms.TopicConnection)115 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)108 ObjectMessage (javax.jms.ObjectMessage)98 CountDownLatch (java.util.concurrent.CountDownLatch)91 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)90 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)88