Example usage for javax.jms Session createQueue

List of usage examples for javax.jms Session createQueue

Introduction

In this page you can find the example usage for javax.jms Session createQueue.

Prototype

Queue createQueue(String queueName) throws JMSException;

Source Link

Document

Creates a Queue object which encapsulates a specified provider-specific queue name.

Usage

From source file:com.datatorrent.lib.io.jms.JMSTestBase.java

public void produceMsg(String text) throws Exception {
    // Create a ConnectionFactory
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");

    // Create a Connection
    Connection connection = connectionFactory.createConnection();
    connection.start();/* w  w w  .  ja  v  a 2  s .  co  m*/

    // Create a Session
    Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);

    // Create the destination (Topic or Queue)
    Destination destination = session.createQueue("TEST.FOO");

    // Create a MessageProducer from the Session to the Topic or Queue
    MessageProducer producer = session.createProducer(destination);
    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

    // Create a messages
    TextMessage message = session.createTextMessage(text);
    producer.send(message);

    // Clean up
    session.close();
    connection.close();
}

From source file:org.soitoolkit.commons.studio.components.logger.impl.DefaultLogEventSender.java

protected void sendOneTextMessage(Session session, String queueName, String message) {

    MessageProducer publisher = null;/* w w w .  j  a  va  2 s . c  o  m*/

    try {
        publisher = session.createProducer(session.createQueue(queueName));
        TextMessage textMessage = session.createTextMessage(message);
        publisher.send(textMessage);

    } catch (JMSException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (publisher != null)
                publisher.close();
        } catch (JMSException e) {
        }
    }
}

From source file:org.openbaton.common.vnfm_sdk.amqp.VnfmSpringHelper.java

/**
 * This method should be used for receiving text message from EMS
 *
 * resp = { 'output': out, // the output of the command 'err': err, // the error outputs of the
 * commands 'status': status // the exit status of the command }
 *
 * @param queueName/*from   w w  w. j a  va2 s . c om*/
 * @return
 * @throws JMSException
 */
public String receiveTextFromQueue(String queueName)
        throws JMSException, ExecutionException, InterruptedException, VnfmSdkException {
    String res;

    Connection connection = connectionFactory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(session.createQueue(queueName));
    connection.start();
    String scriptMaxTime = properties.getProperty("script-max-time");
    if (scriptMaxTime != null) {
        TextMessage textMessage = (TextMessage) consumer.receive(Long.parseLong(scriptMaxTime));
        if (textMessage != null)
            res = textMessage.getText();
        else
            throw new VnfmSdkException("No message got from queue " + queueName + " after " + scriptMaxTime);
    } else
        res = ((TextMessage) consumer.receive()).getText();
    log.debug("Received Text from " + queueName + ": " + res);
    consumer.close();
    session.close();
    connection.close();
    return res;
}

From source file:com.datatorrent.lib.io.jms.JMSStringInputOperatorTest.java

private void produceMsg(int numMessages) throws Exception {
    // Create a ConnectionFactory
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");

    // Create a Connection
    Connection connection = connectionFactory.createConnection();
    connection.start();/*from  ww  w .ja va  2 s.c o m*/

    // Create a Session
    Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);

    // Create the destination (Topic or Queue)
    Destination destination = session.createQueue("TEST.FOO");

    // Create a MessageProducer from the Session to the Topic or Queue
    MessageProducer producer = session.createProducer(destination);
    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

    // Create a messages
    String text = "Hello world! From tester producer";
    TextMessage message = session.createTextMessage(text);
    for (int i = 0; i < numMessages; i++) {
        producer.send(message);
    }

    // Clean up
    session.close();
    connection.close();

}

From source file:Supplier.java

public void run() {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
    Session session = null;
    Destination orderQueue;// w ww .  java  2 s  .  c  o m
    try {
        Connection connection = connectionFactory.createConnection();

        session = connection.createSession(true, Session.SESSION_TRANSACTED);
        orderQueue = session.createQueue(QUEUE);
        MessageConsumer consumer = session.createConsumer(orderQueue);

        connection.start();

        while (true) {
            Message message = consumer.receive();
            MessageProducer producer = session.createProducer(message.getJMSReplyTo());
            MapMessage orderMessage;
            if (message instanceof MapMessage) {
                orderMessage = (MapMessage) message;
            } else {
                // End of Stream
                producer.send(session.createMessage());
                session.commit();
                producer.close();
                break;
            }

            int quantity = orderMessage.getInt("Quantity");
            System.out.println(
                    ITEM + " Supplier: Vendor ordered " + quantity + " " + orderMessage.getString("Item"));

            MapMessage outMessage = session.createMapMessage();
            outMessage.setInt("VendorOrderNumber", orderMessage.getInt("VendorOrderNumber"));
            outMessage.setString("Item", ITEM);

            quantity = Math.min(orderMessage.getInt("Quantity"),
                    new Random().nextInt(orderMessage.getInt("Quantity") * 10));
            outMessage.setInt("Quantity", quantity);

            producer.send(outMessage);
            System.out.println(ITEM + " Supplier: Sent " + quantity + " " + ITEM + "(s)");
            session.commit();
            System.out.println(ITEM + " Supplier: committed transaction");
            producer.close();
        }
        connection.close();
    } catch (JMSException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.servicemix.jms.JMSComponentTest.java

public void testConsumerInOut() throws Exception {
    // JMS Component
    JmsComponent component = new JmsComponent();
    container.activateComponent(component, "JMSComponent");

    // Add an echo component
    EchoComponent echo = new EchoComponent();
    ActivationSpec asEcho = new ActivationSpec("receiver", echo);
    asEcho.setService(new QName("http://jms.servicemix.org/Test", "Echo"));
    container.activateComponent(asEcho);

    // Deploy Consumer SU
    URL url = getClass().getClassLoader().getResource("consumer/jms.wsdl");
    File path = new File(new URI(url.toString()));
    path = path.getParentFile();/*w ww .j  av  a 2s .  c  o  m*/
    component.getServiceUnitManager().deploy("consumer", path.getAbsolutePath());
    component.getServiceUnitManager().init("consumer", path.getAbsolutePath());
    component.getServiceUnitManager().start("consumer");

    // Send test message
    jmsTemplate.setDefaultDestinationName("queue/A");
    jmsTemplate.afterPropertiesSet();
    jmsTemplate.send(new MessageCreator() {
        public Message createMessage(Session session) throws JMSException {
            Message m = session.createTextMessage("<hello>world</hello>");
            m.setJMSReplyTo(session.createQueue("queue/B"));
            return m;
        }
    });

    // Receive echo message
    TextMessage reply = (TextMessage) jmsTemplate.receive("queue/B");
    assertNotNull(reply);
    logger.info(reply.getText());
}

From source file:org.logicblaze.lingo.jms.JmsRemotingTest.java

protected void subscribeToQueue(JmsServiceExporter exporter, String queueName) throws JMSException {
    Session serverSession = createSession();
    Queue queue = serverSession.createQueue(queueName);
    MessageConsumer consumer = serverSession.createConsumer(queue);
    consumer.setMessageListener(exporter);
}

From source file:org.sample.TimeController.java

@RequestMapping(method = RequestMethod.GET, value = { "/tick" })
public ModelAndView get() throws Exception {
    Connection connection = jmsFactory.createConnection();
    Session jmsSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    SecureRandom srandom = new SecureRandom();
    String rand = new BigInteger(176, srandom).toString(32);
    String q = Long.toString(System.currentTimeMillis()) + "-" + rand;
    Destination destination = jmsSession.createQueue(q);

    Timer timer = new Timer();
    long delay = 1 * 1000;
    timer.schedule(new TickTask(jmsSession, destination), 0, delay);

    ModelAndView mav = new ModelAndView("client");
    mav.addObject("queue", destination.toString());
    return mav;/*  w w  w . ja va  2s.  com*/
}

From source file:org.ahp.core.messaging.AhpJmsProducer.java

public void sendTextMessage(String pTextMessage, AhpJmsDestinationTypes pAhpJmsDestinationTypes,
        AhpJmsDestinationNames pAhpJmsDestinationNames) {
    Connection lConnection = null;
    Session lSession = null;
    try {//w  w  w.j a va2s  . co  m
        lConnection = this.mJmsConnectionFactory.createConnection();
        lSession = lConnection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        Destination lDestination = null;
        if (pAhpJmsDestinationTypes.equals(AhpJmsDestinationTypes.Queue))
            lDestination = lSession.createQueue(pAhpJmsDestinationNames.toString());
        else
            lDestination = lSession.createTopic(pAhpJmsDestinationNames.toString());
        MessageProducer lMessageProducer = lSession.createProducer(lDestination);
        lMessageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
        //lMessageProducer.setTimeToLive( timeToLive );
        Message lTextMessage = lSession.createTextMessage(pTextMessage);
        LOGGER.debug("AhpJmsProducer sending message to Destination " + pAhpJmsDestinationNames.toString()
                + " TextMessage :: \n" + pTextMessage);
        lMessageProducer.send(lTextMessage);
        lSession.commit();
    } catch (JMSException exJms) {
        LOGGER.error("", exJms);
    } finally {
        try {
            lConnection.close();
        } catch (JMSException exJms) {
            LOGGER.error("", exJms);
        }
    }
}

From source file:ProducerTool.java

public void run() {
    Connection connection = null;
    try {/*from  w  w w.  j a v a  2  s.co  m*/
        // Create the connection.
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
        connection = connectionFactory.createConnection();
        connection.start();

        // Create the session
        Session session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
        if (topic) {
            destination = session.createTopic(subject);
        } else {
            destination = session.createQueue(subject);
        }

        // Create the producer.
        MessageProducer producer = session.createProducer(destination);
        if (persistent) {
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        } else {
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        }
        if (timeToLive != 0) {
            producer.setTimeToLive(timeToLive);
        }

        // Start sending messages
        sendLoop(session, producer);

        System.out.println("[" + this.getName() + "] Done.");

        synchronized (lockResults) {
            ActiveMQConnection c = (ActiveMQConnection) connection;
            System.out.println("[" + this.getName() + "] Results:\n");
            c.getConnectionStats().dump(new IndentPrinter());
        }

    } catch (Exception e) {
        System.out.println("[" + this.getName() + "] Caught: " + e);
        e.printStackTrace();
    } finally {
        try {
            connection.close();
        } catch (Throwable ignore) {
        }
    }
}