Example usage for javax.jms MessageProducer send

List of usage examples for javax.jms MessageProducer send

Introduction

In this page you can find the example usage for javax.jms MessageProducer send.

Prototype


void send(Message message) throws JMSException;

Source Link

Document

Sends a message using the MessageProducer 's default delivery mode, priority, and time to live.

Usage

From source file:org.codehaus.stomp.StompTest.java

public void sendBytesMessage(byte[] msg) throws Exception {
    MessageProducer producer = session.createProducer(queue);
    BytesMessage message = session.createBytesMessage();
    message.writeBytes(msg);//from w w  w . j av  a2 s  . c om
    producer.send(message);
}

From source file:org.apache.activemq.bugs.AMQ7067Test.java

@Test
public void testXArollback() throws Exception {

    setupXAConnection();/*from   w ww.ja v  a2 s  .c  om*/

    Queue holdKahaDb = xaSession.createQueue("holdKahaDb");
    createDanglingTransaction(xaRes, xaSession, holdKahaDb);

    MessageProducer holdKahaDbProducer = xaSession.createProducer(holdKahaDb);

    XATransactionId txid = createXATransaction();
    System.out.println("****** create new txid = " + txid);
    xaRes.start(txid, TMNOFLAGS);

    TextMessage helloMessage = xaSession.createTextMessage(StringUtils.repeat("a", 10));
    holdKahaDbProducer.send(helloMessage);
    xaRes.end(txid, TMSUCCESS);
    xaRes.prepare(txid);

    Queue queue = xaSession.createQueue("test");

    produce(xaRes, xaSession, queue, 100, 512 * 1024);
    xaRes.rollback(txid);
    produce(xaRes, xaSession, queue, 100, 512 * 1024);

    ((org.apache.activemq.broker.region.Queue) broker.getRegionBroker().getDestinationMap().get(queue)).purge();

    Xid[] xids = xaRes.recover(TMSTARTRSCAN);

    //Should be 1 since we have only 1 prepared
    assertEquals(1, xids.length);
    connection.close();

    broker.stop();
    broker.waitUntilStopped();
    createBroker();

    setupXAConnection();
    xids = xaRes.recover(TMSTARTRSCAN);

    // THIS SHOULD NOT FAIL AS THERE SHOULD BE ONLY 1 TRANSACTION!
    assertEquals(1, xids.length);

}

From source file:org.mot.common.mq.ActiveMQFactory.java

/**
 * //from  w  w  w. ja v  a2 s  .com
 * @param tick
 * @param ttl
 * @param persistent
 */
public void publishTicks(Tick[] tick, MessageProducer mp) {

    BytesMessage tickMessage;
    try {
        Session session = this.createSession();

        for (int i = 0; i < tick.length; i++) {
            // Create a new ByteMessage
            tickMessage = session.createBytesMessage();

            // Serialize the tick content into the message
            tickMessage.writeBytes(tick[i].serialize());
            tickMessage.setStringProperty("Symbol", tick[i].getSymbol());
            tickMessage.setStringProperty("Currency", tick[i].getCurrency());

            mp.send(tickMessage);
        }
        this.closeSession(session);
    } catch (JMSException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:eu.domibus.submission.jms.BackendJMSImpl.java

private Boolean submitErrorMessage(String messageId) {
    Connection connection;/*from www.jav a 2 s. c  o m*/
    MessageProducer producer;
    List<ErrorLogEntry> errors = this.errorLogDao.getUnnotifiedErrorsForMessage(messageId);

    try {
        connection = this.cf.createConnection();
        final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producer = session.createProducer(this.receivingQueue);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        final MapMessage resMessage = session.createMapMessage();
        for (int i = 0; i < errors.size(); ++i) {
            resMessage.setString(String.valueOf(i), errors.get(i).toString());
            errors.get(i).setNotified(new Date());
            this.errorLogDao.update(errors.get(i));
        }

        producer.send(resMessage);
        producer.close();
        session.close();

        connection.close();
    } catch (JMSException e) {
        BackendJMSImpl.LOG.error("", e);
        return false;
    }
    return true;
}

From source file:org.apache.activemq.bugs.AMQ7067Test.java

@Test
public void testXAcommit() throws Exception {

    setupXAConnection();/*from w  w w  . j a  v a 2 s  . c  o  m*/

    Queue holdKahaDb = xaSession.createQueue("holdKahaDb");
    createDanglingTransaction(xaRes, xaSession, holdKahaDb);

    MessageProducer holdKahaDbProducer = xaSession.createProducer(holdKahaDb);

    XATransactionId txid = createXATransaction();
    System.out.println("****** create new txid = " + txid);
    xaRes.start(txid, TMNOFLAGS);

    TextMessage helloMessage = xaSession.createTextMessage(StringUtils.repeat("a", 10));
    holdKahaDbProducer.send(helloMessage);
    xaRes.end(txid, TMSUCCESS);
    xaRes.prepare(txid);

    Queue queue = xaSession.createQueue("test");

    produce(xaRes, xaSession, queue, 100, 512 * 1024);
    xaRes.commit(txid, false);
    produce(xaRes, xaSession, queue, 100, 512 * 1024);

    ((org.apache.activemq.broker.region.Queue) broker.getRegionBroker().getDestinationMap().get(queue)).purge();

    Wait.waitFor(new Wait.Condition() {
        @Override
        public boolean isSatisified() throws Exception {
            return 0 == getQueueSize(queue.getQueueName());
        }
    });

    // force gc
    broker.getPersistenceAdapter().checkpoint(true);

    Xid[] xids = xaRes.recover(TMSTARTRSCAN);

    //Should be 1 since we have only 1 prepared
    assertEquals(1, xids.length);
    connection.close();

    broker.stop();
    broker.waitUntilStopped();
    createBroker();

    setupXAConnection();
    xids = xaRes.recover(TMSTARTRSCAN);

    // THIS SHOULD NOT FAIL AS THERE SHOULD DBE ONLY 1 TRANSACTION!
    assertEquals(1, xids.length);

}

From source file:org.apache.activemq.bugs.AMQ7067Test.java

@Test
public void testXAPrepare() throws Exception {

    setupXAConnection();/*from   w ww.ja v a2  s  .c  o  m*/

    Queue holdKahaDb = xaSession.createQueue("holdKahaDb");

    MessageProducer holdKahaDbProducer = xaSession.createProducer(holdKahaDb);

    XATransactionId txid = createXATransaction();
    System.out.println("****** create new txid = " + txid);
    xaRes.start(txid, TMNOFLAGS);

    TextMessage helloMessage = xaSession.createTextMessage(StringUtils.repeat("a", 10));
    holdKahaDbProducer.send(helloMessage);
    xaRes.end(txid, TMSUCCESS);

    Queue queue = xaSession.createQueue("test");

    produce(xaRes, xaSession, queue, 100, 512 * 1024);

    xaRes.prepare(txid);

    produce(xaRes, xaSession, queue, 100, 512 * 1024);

    ((org.apache.activemq.broker.region.Queue) broker.getRegionBroker().getDestinationMap().get(queue)).purge();

    Wait.waitFor(new Wait.Condition() {
        @Override
        public boolean isSatisified() throws Exception {
            return 0 == getQueueSize(queue.getQueueName());
        }
    });

    // force gc
    broker.getPersistenceAdapter().checkpoint(true);

    Xid[] xids = xaRes.recover(TMSTARTRSCAN);

    //Should be 1 since we have only 1 prepared
    assertEquals(1, xids.length);
    connection.close();

    broker.stop();
    broker.waitUntilStopped();
    createBroker();

    setupXAConnection();
    xids = xaRes.recover(TMSTARTRSCAN);

    System.out.println("****** recovered = " + xids);

    // THIS SHOULD NOT FAIL AS THERE SHOULD DBE ONLY 1 TRANSACTION!
    assertEquals(1, xids.length);
}

From source file:org.apache.activemq.bugs.AMQ7067Test.java

@Test
public void testXAPrepareWithAckCompactionDoesNotLooseInflight() throws Exception {

    // investigate liner gc issue - store usage not getting released
    org.apache.log4j.Logger.getLogger(MessageDatabase.class).setLevel(Level.TRACE);

    setupXAConnection();/*  ww  w  . ja va 2  s .c o  m*/

    Queue holdKahaDb = xaSession.createQueue("holdKahaDb");

    MessageProducer holdKahaDbProducer = xaSession.createProducer(holdKahaDb);

    XATransactionId txid = createXATransaction();
    System.out.println("****** create new txid = " + txid);
    xaRes.start(txid, TMNOFLAGS);

    TextMessage helloMessage = xaSession.createTextMessage(StringUtils.repeat("a", 10));
    holdKahaDbProducer.send(helloMessage);
    xaRes.end(txid, TMSUCCESS);

    Queue queue = xaSession.createQueue("test");

    produce(xaRes, xaSession, queue, 100, 512 * 1024);

    xaRes.prepare(txid);

    produce(xaRes, xaSession, queue, 100, 512 * 1024);

    ((org.apache.activemq.broker.region.Queue) broker.getRegionBroker().getDestinationMap().get(queue)).purge();

    Wait.waitFor(new Wait.Condition() {
        @Override
        public boolean isSatisified() throws Exception {
            return 0 == getQueueSize(queue.getQueueName());
        }
    });

    // force gc, two data files requires two cycles
    int limit = ((KahaDBPersistenceAdapter) broker.getPersistenceAdapter()).getCompactAcksAfterNoGC() + 1;
    for (int i = 0; i < limit * 2; i++) {
        broker.getPersistenceAdapter().checkpoint(true);
    }

    // ack compaction task operates in the background
    TimeUnit.SECONDS.sleep(5);

    Xid[] xids = xaRes.recover(TMSTARTRSCAN);

    //Should be 1 since we have only 1 prepared
    assertEquals(1, xids.length);
    connection.close();

    broker.stop();
    broker.waitUntilStopped();
    createBroker();

    setupXAConnection();
    xids = xaRes.recover(TMSTARTRSCAN);

    System.out.println("****** recovered = " + xids);

    // THIS SHOULD NOT FAIL AS THERE SHOULD DBE ONLY 1 TRANSACTION!
    assertEquals(1, xids.length);
}

From source file:com.mdmserver.managers.ControllerFacade.java

public void lockPhone(int accountId) throws IOException {
    Account account = databaseManager.getAccountByAccountId(accountId);
    Context ctx;/*from   ww w. ja v  a2  s . c o  m*/
    try {
        ctx = new InitialContext();
        ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup("jms/mdmConnectionFactory");
        Queue queue = (Queue) ctx.lookup("jms/mdmQueue");
        MessageProducer messageProducer;
        System.out.println("Naming success");
        try {

            javax.jms.Connection connection = connectionFactory.createConnection();
            javax.jms.Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            messageProducer = session.createProducer(queue);
            TextMessage message = session.createTextMessage();
            message.setStringProperty(Account.FIELD_NAME_CLOUD_ID, account.getCloudId());
            ControlClient cClient = new ControlClient();
            cClient.setCommandType(ControlClient.LOCK_PHONE_CONTROL);
            Gson gson = new Gson();
            message.setStringProperty(ControlClient.CONTROL_CLIENT_KEY, gson.toJson(cClient));
            System.out.println("It come from Servlet:" + message.getText());
            messageProducer.send(message);
            System.out.println("JMS success");
        } catch (JMSException ex) {
            //                  Logger.getLogger(Control.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("JMS exception");
        }

    } catch (NamingException ex) {
        //                Logger.getLogger(Control.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println("Naming exception");
    }
}

From source file:org.codehaus.stomp.StompTest.java

public void sendMessage(String msg, String propertyName, String propertyValue) throws JMSException {
    MessageProducer producer = session.createProducer(queue);
    TextMessage message = session.createTextMessage(msg);
    message.setStringProperty(propertyName, propertyValue);
    producer.send(message);
}

From source file:org.apache.falcon.messaging.JMSMessageConsumerTest.java

public void sendMessages(String topic, WorkflowExecutionContext.Type type, boolean isFalconWF)
        throws JMSException, FalconException, IOException {
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(BROKER_URL);
    Connection connection = connectionFactory.createConnection();
    connection.start();//from w  ww  .j  a  v  a2  s.c o  m

    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic destination = session.createTopic(topic);
    javax.jms.MessageProducer producer = session.createProducer(destination);
    producer.setDeliveryMode(DeliveryMode.PERSISTENT);

    for (int i = 0; i < 5; i++) {
        Message message = null;

        switch (type) {
        case POST_PROCESSING:
            message = getMockFalconMessage(i, session);
            break;
        case WORKFLOW_JOB:
            message = getMockOozieMessage(i, session, isFalconWF);
            break;
        case COORDINATOR_ACTION:
            message = getMockOozieCoordMessage(i, session, isFalconWF);
        default:
            break;
        }
        Log.debug("Sending:" + message);
        producer.send(message);
    }
}