Example usage for javax.jms Session AUTO_ACKNOWLEDGE

List of usage examples for javax.jms Session AUTO_ACKNOWLEDGE

Introduction

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

Prototype

int AUTO_ACKNOWLEDGE

To view the source code for javax.jms Session AUTO_ACKNOWLEDGE.

Click Source Link

Document

With this acknowledgment mode, the session automatically acknowledges a client's receipt of a message either when the session has successfully returned from a call to receive or when the message listener the session has called to process the message successfully returns.

Usage

From source file:drepcap.frontend.jms.JmsAdapter.java

/**
 * //from  w  w  w  . j a  va2 s  . c o  m
 * Components are typically pcap-sensors or packet-mergers. The component
 * name is the name without any additional suffix, e.g.,
 * "pcap.single.raw.2".
 * 
 * @param connection
 * @param componentName
 * @throws JMSException
 */
public JmsAdapter(Connection connection, String componentName) throws JMSException {
    this.componentName = componentName;
    this.connection = connection;
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    commandTopic = session.createTopic(componentName + ".command");
    commandConsumer = session.createConsumer(commandTopic);
    commandConsumer.setMessageListener(new MessageListener() {
        @Override
        public void onMessage(Message msg) {
            if (msg instanceof TextMessage) {
                TextMessage textMsg = (TextMessage) msg;
                try {
                    final String txt = textMsg.getText();

                    for (StringReceiver cmdReplyReceiver : commandReplyReceivers) {
                        if (cmdReplyReceiver != null && txt.startsWith("reply")) {
                            cmdReplyReceiver.process(txt.replaceFirst("reply ", ""));
                        }
                    }
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    commandProducer = session.createProducer(commandTopic);

    monitorTopic = session.createTopic(componentName + ".monitor");
    monitorConsumer = session.createConsumer(monitorTopic);
    monitorConsumer.setMessageListener(new MessageListener() {
        @Override
        public void onMessage(Message msg) {
            if (msg instanceof TextMessage) {
                TextMessage textMsg = (TextMessage) msg;
                try {
                    final String txt = textMsg.getText();

                    for (StringReceiver monReceiver : monitorReceivers) {
                        if (monReceiver != null) {
                            monReceiver.process(txt);
                        }
                    }

                    if (statsDataReceivers.size() > 0) {
                        processStatsFromString(txt);
                    }
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }
    });
}

From source file:org.apache.qpid.client.ssl.SSLTest.java

public void testCreateSSLConnectionUsingConnectionURLParams() throws Exception {
    if (shouldPerformTest()) {
        clearSslStoreSystemProperties();

        //Start the broker (NEEDing client certificate authentication)
        configureJavaBrokerIfNecessary(true, true, true, false);
        super.setUp();

        String url = "amqp://guest:guest@test/?brokerlist='tcp://localhost:%s"
                + "?ssl='true'&ssl_verify_hostname='true'" + "&key_store='%s'&key_store_password='%s'"
                + "&trust_store='%s'&trust_store_password='%s'" + "'";

        url = String.format(url, QpidBrokerTestCase.DEFAULT_SSL_PORT, KEYSTORE, KEYSTORE_PASSWORD, TRUSTSTORE,
                TRUSTSTORE_PASSWORD);/*  www .j av  a2 s .  c  o m*/

        Connection con = getConnection(new AMQConnectionURL(url));
        assertNotNull("connection should be successful", con);
        Session ssn = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
        assertNotNull("create session should be successful", ssn);
    }
}

From source file:org.apache.qpid.systest.management.jmx.ConnectionManagementTest.java

public void testNonTransactedSession() throws Exception {
    _connection = getConnection();/*from   w w  w .j  a  v  a  2  s.  c  o  m*/

    boolean transactional = false;
    boolean flowBlocked = false;

    _connection.createSession(transactional, Session.AUTO_ACKNOWLEDGE);

    final ManagedConnection mBean = getConnectionMBean();
    final CompositeDataSupport row = getTheOneChannelRow(mBean);
    assertChannelRowData(row, 0, transactional, flowBlocked);
}

From source file:org.apache.activemq.JmsTopicSendReceiveWithTwoConnectionsTest.java

protected Session createSendSession(Connection sendConnection) throws Exception {
    return sendConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
}

From source file:org.apache.servicemix.wsn.jms.JmsPublisher.java

@Override
public void notify(NotificationMessageHolderType messageHolder, String mes) {
    Session session = null;//from ww w  .j  av a2s  .c  o  m
    try {
        Topic topic = topicConverter.toActiveMQTopic(messageHolder.getTopic());
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(topic);
        Message message = session.createTextMessage(mes);
        producer.send(message);
    } catch (JMSException e) {
        log.warn("Error dispatching message", e);
    } catch (InvalidTopicException e) {
        log.warn("Error dispatching message", e);
    } finally {
        if (session != null) {
            try {
                session.close();
            } catch (JMSException e) {
                log.debug("Error closing session", e);
            }
        }
    }
}

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

protected Session getSession() throws JMSException {
    JmsConnector jmsConn = (JmsConnector) MuleUtil.getSpringBean(getMuleContext(), "soitoolkit-jms-connector");
    Connection c = jmsConn.getConnection();
    Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE);
    return s;/* w w  w . j av a2  s  . com*/
}

From source file:org.exist.messaging.JmsMessageSender.java

@Override
public NodeImpl send(JmsMessagingConfiguration config, MessagingMetadata metadata, Item content)
        throws XPathException {

    // JMS specific checks
    config.validateContent();//from   www  .  j a  v a2  s.  c o m

    // Retrieve relevant values
    String initialContextFactory = config.getInitalContextProperty(Context.INITIAL_CONTEXT_FACTORY);
    String providerURL = config.getInitalContextProperty(Context.PROVIDER_URL);
    String connectionFactory = config.getConnectionFactory();
    String destination = config.getDestination();

    // TODO split up, use more exceptions, add better reporting
    try {
        Properties props = new Properties();
        props.setProperty(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
        props.setProperty(Context.PROVIDER_URL, providerURL);
        javax.naming.Context context = new InitialContext(props);

        // Setup connection
        ConnectionFactory cf = (ConnectionFactory) context.lookup(connectionFactory);
        Connection connection = cf.createConnection();

        // Lookup queue
        Destination dest = (Destination) context.lookup(destination);

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

        // Create message producer
        MessageProducer producer = session.createProducer(dest);

        // Create message
        Message message = createMessage(session, content, metadata, xqcontext);

        // Write properties
        Map<String, String> kvs = metadata.getValueMap();
        for (String key : kvs.keySet()) {
            message.setStringProperty(key, kvs.get(key));
        }

        // Send message
        producer.send(message);

        // Close connection
        // TODO keep connection open for re-use, efficiency
        connection.close();

        return createReport(message, xqcontext);

    } catch (Throwable ex) {
        LOG.error(ex);
        throw new XPathException(ex);
    }
}

From source file:jenkins.plugins.logstash.persistence.ActiveMqDao.java

@Override
public void push(String data) throws IOException {
    TopicConnection connection = null;//from w ww.  j  a v  a2  s  .  com
    Session session = null;
    try {
        // Create a Connection
        connection = connectionFactory.createTopicConnection();

        connection.start();

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

        // Create the destination Queue
        Destination destination = session.createTopic(key);

        // Create the MessageProducer from the Session to the Queue
        MessageProducer producer = session.createProducer(destination);

        producer.setDeliveryMode(DeliveryMode.PERSISTENT);

        // Create the message
        TextMessage message = session.createTextMessage(data);
        message.setJMSType("application/json");
        // Tell the producer to send the message
        producer.send(message);

        //logger.log( Level.FINER, String.format("JMS message sent with ID [%s]", message.getJMSMessageID()));

    } catch (JMSException e) {
        logger.log(Level.SEVERE, null != e.getMessage() ? e.getMessage() : e.getClass().getName());
        throw new IOException(e);
    } finally {
        // Clean up
        try {
            if (null != session)
                session.close();
        } catch (JMSException e) {
            logger.log(Level.WARNING, null != e.getMessage() ? e.getMessage() : e.getClass().getName());
        }
        try {
            if (null != connection)
                connection.close();
        } catch (JMSException e) {
            logger.log(Level.WARNING, null != e.getMessage() ? e.getMessage() : e.getClass().getName());
        }
    }
}

From source file:RequesterTool.java

public void run() {

    Connection connection = null;
    try {// w ww . j  av  a2  s  . c o m

        System.out.println("Connecting to URL: " + url);
        System.out.println("Publishing a Message with size " + messageSize + " to "
                + (topic ? "topic" : "queue") + ": " + subject);
        System.out.println("Using " + (persistent ? "persistent" : "non-persistent") + " messages");
        System.out.println("Sleeping between publish " + sleepTime + " ms");

        // Create the connection
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
        connection = connectionFactory.createConnection();
        if (persistent && clientId != null && clientId.length() > 0 && !"null".equals(clientId)) {
            connection.setClientID(clientId);
        }
        connection.start();

        // Create the Session
        session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);

        // And the Destinations..
        if (topic) {
            destination = session.createTopic(subject);
            if (replySubject == null || replySubject.equals("")) {
                replyDest = session.createTemporaryTopic();
            } else {
                replyDest = session.createTopic(replySubject);
            }
        } else {
            destination = session.createQueue(subject);
            if (replySubject == null || replySubject.equals("")) {
                replyDest = session.createTemporaryQueue();
            } else {
                replyDest = session.createQueue(replySubject);
            }
        }
        System.out.println("Reply Destination: " + replyDest);

        // Create the producer
        producer = session.createProducer(destination);
        if (persistent) {
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        } else {
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        }
        if (timeToLive != 0) {
            System.out.println("Messages time to live " + timeToLive + " ms");
            producer.setTimeToLive(timeToLive);
        }

        // Create the reply consumer
        consumer = session.createConsumer(replyDest);

        // Start sending reqests.
        requestLoop();

        System.out.println("Done.");

        // Use the ActiveMQConnection interface to dump the connection
        // stats.
        ActiveMQConnection c = (ActiveMQConnection) connection;
        c.getConnectionStats().dump(new IndentPrinter());

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

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

private Boolean submitErrorMessage(String messageId) {
    Connection connection;//from ww w.  j  a va2  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;
}