List of usage examples for javax.jms Session createProducer
MessageProducer createProducer(Destination destination) throws JMSException;
From source file:org.jbpm.bpel.tutorial.invoice.ComputePricePT_Impl.java
protected void sendInvoiceMessage(float shippingPrice) throws JMSException { ServletContext servletContext = endpointContext.getServletContext(); Integer orderId = (Integer) servletContext.getAttribute(ORDER_ID_ATTR); Float linePrice = (Float) servletContext.getAttribute(LINE_PRICE_ATTR); float amount = linePrice.floatValue() + shippingPrice; // create a session Session jmsSession = jmsConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE); try {/*from ww w. j av a2 s . c o m*/ // create the message MapMessage invoiceMessage = jmsSession.createMapMessage(); invoiceMessage.setInt("orderId", orderId.intValue()); invoiceMessage.setFloat("amount", amount); // send it! MessageProducer producer = jmsSession.createProducer(invoiceDestination); producer.send(invoiceMessage); log.debug("Sent invoice message: orderId=" + orderId + ", amount=" + amount); } finally { jmsSession.close(); } }
From source file:org.apache.activemq.usecases.ConcurrentProducerDurableConsumerTest.java
public void x_testSendWithInactiveAndActiveConsumers() throws Exception { Destination destination = createDestination(); ConnectionFactory factory = createConnectionFactory(); startInactiveConsumers(factory, destination); Connection connection = factory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(destination); producer.setDeliveryMode(DeliveryMode.PERSISTENT); final int toSend = 100; final int numIterations = 5; double[] noConsumerStats = produceMessages(destination, toSend, numIterations, session, producer, null); startConsumers(factory, destination); LOG.info("Activated consumer"); double[] withConsumerStats = produceMessages(destination, toSend, numIterations, session, producer, null); LOG.info("With consumer: " + withConsumerStats[1] + " , with noConsumer: " + noConsumerStats[1] + ", multiplier: " + (withConsumerStats[1] / noConsumerStats[1])); final int reasonableMultiplier = 15; // not so reasonable but improving assertTrue(/*from www. j a v a 2 s . c o m*/ "max X times as slow with consumer: " + withConsumerStats[1] + ", with no Consumer: " + noConsumerStats[1] + ", multiplier: " + (withConsumerStats[1] / noConsumerStats[1]), withConsumerStats[1] < noConsumerStats[1] * reasonableMultiplier); final int toReceive = toSend * numIterations * consumerCount * 2; Wait.waitFor(new Wait.Condition() { public boolean isSatisified() throws Exception { LOG.info("count: " + allMessagesList.getMessageCount()); return toReceive == allMessagesList.getMessageCount(); } }, 60 * 1000); assertEquals("got all messages", toReceive, allMessagesList.getMessageCount()); }
From source file:de.klemp.middleware.controller.Controller.java
/** * This method creates a connection to the message broker Active MQ and * sends the message to the given topic. The method is used by all methods * of the second component.// www . ja v a 2 s . c o m * * @param message * send to the output device * @param topic * of the message broker */ public static void sendMessage(String message, String topic) { String url = ActiveMQConnection.DEFAULT_BROKER_URL; ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); // Create a Connection Connection connection; try { connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Create the destination (Topic or Queue) Destination destination = session.createTopic(topic); // 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 message1 = session.createTextMessage(message); // Tell the producer to send the message producer.send(message1); session.close(); connection.close(); } catch (JMSException e) { logger.error("Message could not be sended to activemq", e); } }
From source file:de.klemp.middleware.controller.Controller.java
public static void sendMessageFast(String message, String topic) { String url = ActiveMQConnection.DEFAULT_BROKER_URL; ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); // Create a Connection Connection connection;// ww w.j a v a2 s. c om try { connectionFactory.setOptimizeAcknowledge(true); connectionFactory.setUseAsyncSend(true); connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Create the destination (Topic or Queue) Destination destination = session.createTopic(topic); // 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 message1 = session.createTextMessage(message); // Tell the producer to send the message producer.send(message1); session.close(); connection.close(); } catch (JMSException e) { logger.error("Message could not be sended to activemq", e); } }
From source file:ProducerTool.java
public void run() { Connection connection = null; try {//w w w. j ava 2 s.c om // 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) { } } }
From source file:org.apache.activemq.leveldb.test.ReplicatedLevelDBBrokerTest.java
private void sendMessage(BrokerService brokerService, String body) throws Exception { TransportConnector connector = brokerService.getTransportConnectors().get(0); ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(connector.getConnectUri()); Connection connection = factory.createConnection(); try {/* w w w. ja v a2s. com*/ connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(session.createQueue("FOO")); producer.send(session.createTextMessage(body)); } finally { connection.close(); } }
From source file:fr.xebia.springframework.jms.ManagedCachingConnectionFactoryTest.java
@Test public void testMessageProducer() throws Exception { ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory( "vm://localhost?broker.persistent=false&broker.useJmx=true"); ManagedConnectionFactory connectionFactory = new ManagedConnectionFactory(activeMQConnectionFactory); Connection connection = null; Session session = null; MessageProducer messageProducer = null; try {//from w w w . ja v a 2 s. c o m connection = connectionFactory.createConnection(); assertEquals(1, connectionFactory.getActiveConnectionCount()); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); assertEquals(1, connectionFactory.getActiveSessionCount()); Destination myQueue = session.createQueue("test-queue"); messageProducer = session.createProducer(myQueue); assertEquals(1, connectionFactory.getActiveMessageProducerCount()); messageProducer.send(myQueue, session.createTextMessage("test")); assertEquals(0, connectionFactory.getActiveMessageConsumerCount()); } finally { JmsUtils.closeMessageProducer(messageProducer); assertEquals(0, connectionFactory.getActiveMessageProducerCount()); JmsUtils.closeSession(session); assertEquals(0, connectionFactory.getActiveSessionCount()); JmsUtils.closeConnection(connection); assertEquals(0, connectionFactory.getActiveConnectionCount()); } }
From source file:org.apache.activemq.apollo.JmsQueueBrowserTest.java
public void testBrowseReceive() throws Exception { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); ActiveMQQueue destination = new ActiveMQQueue("TEST"); connection.start();//from w ww.j av a 2 s. c om Message[] outbound = new Message[] { session.createTextMessage("First Message"), session.createTextMessage("Second Message"), session.createTextMessage("Third Message") }; MessageProducer producer = session.createProducer(destination); producer.send(outbound[0]); // create browser first QueueBrowser browser = session.createBrowser((Queue) destination); Enumeration enumeration = browser.getEnumeration(); // create consumer MessageConsumer consumer = session.createConsumer(destination); // browse the first message assertTrue("should have received the first message", enumeration.hasMoreElements()); assertEquals(outbound[0], (Message) enumeration.nextElement()); // Receive the first message. assertEquals(outbound[0], consumer.receive(1000)); consumer.close(); browser.close(); producer.close(); }
From source file:org.apache.servicemix.jms.jca.JcaConsumerProcessor.java
public void process(MessageExchange exchange) throws Exception { Context context = (Context) pendingMessages.remove(exchange.getExchangeId()); Message message = (Message) context.getProperty(Message.class.getName()); Message response = null;/*from w w w.j a v a 2 s .c om*/ Connection connection = null; try { if (exchange.getStatus() == ExchangeStatus.DONE) { return; } else if (exchange.getStatus() == ExchangeStatus.ERROR) { if (endpoint.isRollbackOnError()) { TransactionManager tm = (TransactionManager) endpoint.getServiceUnit().getComponent() .getComponentContext().getTransactionManager(); tm.setRollbackOnly(); return; } else if (exchange instanceof InOnly) { LOG.info("Exchange in error: " + exchange, exchange.getError()); return; } else { connection = connectionFactory.createConnection(); Session session = connection.createSession(true, Session.SESSION_TRANSACTED); Exception error = exchange.getError(); if (error == null) { error = new Exception("Exchange in error"); } response = session.createObjectMessage(error); MessageProducer producer = session.createProducer(message.getJMSReplyTo()); if (endpoint.isUseMsgIdInResponse()) { response.setJMSCorrelationID(message.getJMSMessageID()); } else { response.setJMSCorrelationID(message.getJMSCorrelationID()); } producer.send(response); } } else { connection = connectionFactory.createConnection(); Session session = connection.createSession(true, Session.SESSION_TRANSACTED); response = fromNMSResponse(exchange, context, session); if (response != null) { MessageProducer producer = session.createProducer(message.getJMSReplyTo()); if (endpoint.isUseMsgIdInResponse()) { response.setJMSCorrelationID(message.getJMSMessageID()); } else { response.setJMSCorrelationID(message.getJMSCorrelationID()); } producer.send(response); } } } finally { if (connection != null) { connection.close(); } if (exchange.getStatus() == ExchangeStatus.ACTIVE) { exchange.setStatus(ExchangeStatus.DONE); channel.send(exchange); } } }
From source file:org.apache.activemq.apollo.JmsQueueBrowserTest.java
public void testBrowseClose() throws Exception { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); ActiveMQQueue destination = new ActiveMQQueue("TEST"); connection.start();/*from w w w .j ava 2s . c o m*/ TextMessage[] outbound = new TextMessage[] { session.createTextMessage("First Message"), session.createTextMessage("Second Message"), session.createTextMessage("Third Message") }; MessageProducer producer = session.createProducer(destination); producer.send(outbound[0]); producer.send(outbound[1]); producer.send(outbound[2]); // create browser first QueueBrowser browser = session.createBrowser((Queue) destination); Enumeration enumeration = browser.getEnumeration(); // browse some messages assertEquals(outbound[0], (Message) enumeration.nextElement()); assertEquals(outbound[1], (Message) enumeration.nextElement()); //assertEquals(outbound[2], (Message) enumeration.nextElement()); browser.close(); // create consumer MessageConsumer consumer = session.createConsumer(destination); // Receive the first message. TextMessage msg = (TextMessage) consumer.receive(1000); assertEquals("Expected " + outbound[0].getText() + " but received " + msg.getText(), outbound[0], msg); msg = (TextMessage) consumer.receive(1000); assertEquals("Expected " + outbound[1].getText() + " but received " + msg.getText(), outbound[1], msg); msg = (TextMessage) consumer.receive(1000); assertEquals("Expected " + outbound[2].getText() + " but received " + msg.getText(), outbound[2], msg); consumer.close(); producer.close(); }