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:pl.baczkowicz.mqspy.connectivity.jms.JmsConnection.java

public void configure(final DaemonJmsConnectionDetails connectionSettings) {
    try {//from w  w  w .j a va2s  .  c o  m
        if (connectionSettings.getConnectionFactory().getContextFile() != null) {
            final JmsContextFile contextFile = connectionSettings.getConnectionFactory().getContextFile();

            final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                    contextFile.getContextFileLocation());

            connectionFactory = context.getBean(contextFile.getConnectionFactoryBean(),
                    ConnectionFactory.class);

            if (contextFile.getJmsTemplateBean() != null) {
                jmsTemplate = context.getBean(contextFile.getJmsTemplateBean(), JmsTemplate.class);
            } else {
                jmsTemplate = new JmsTemplate(connectionFactory);
            }

            context.close();

            connection = connectionFactory.createConnection();
            connection.start();
            started = true;

            // TODO: optionally create all topics in spring?
        } else if (connectionSettings.getConnectionFactory().getScriptFile() != null) {
            // TODO: create a connection factory

            // TODO: create connection (optional)
            connection = connectionFactory.createConnection();
            connection.start();
            started = true;

            // TODO: create jms template (optional)

            // TODO: create a session (optional)?
        }

        // TODO: parametrise that
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        for (final ScriptedSubscriptionDetails subscription : connectionSettings.getSubscription()) {
            subscribe(subscription.getTopic());
        }
    } catch (JMSException e) {
        logger.error("JMS error", e);
    }

    // TODO: username and password
}

From source file:org.apache.qpid.disttest.jms.ClientJmsDelegate.java

public ClientJmsDelegate(final Context context) {
    try {//  w  w  w.  j  ava 2s.  co  m
        _context = context;
        final ConnectionFactory connectionFactory = (ConnectionFactory) _context.lookup("connectionfactory");
        _controllerConnection = connectionFactory.createConnection();
        _controllerConnection.start();
        _controllerQueue = (Destination) context.lookup(DistributedTestConstants.CONTROLLER_QUEUE_JNDI_NAME);
        _controllerSession = _controllerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        _controlQueueProducer = _controllerSession.createProducer(_controllerQueue);
        _clientName = UUID.randomUUID().toString();
        _testConnections = new HashMap<String, Connection>();
        _testSessions = new HashMap<String, Session>();
        _testProducers = new HashMap<String, MessageProducer>();
        _testConsumers = new HashMap<String, MessageConsumer>();
        _testSubscriptions = new HashMap<String, Session>();
        _testMessageProviders = new HashMap<String, MessageProvider>();
        _defaultMessageProvider = new MessageProvider(null);
    } catch (final NamingException ne) {
        throw new DistributedTestException("Unable to create client jms delegate", ne);
    } catch (final JMSException jmse) {
        throw new DistributedTestException("Unable to create client jms delegate", jmse);
    }
}

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   ww w . j ava 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:com.jmstoolkit.pipeline.AbstractPlugin.java

/**
 * Configures and starts the JMS MessageConsumer.
 *//*from   w  w w .  jav  a2  s  . co  m*/
@Override
public void init() {
    try {
        setConnection(getJmsTemplate().getConnectionFactory().createConnection());
        setSession(getConnection().createSession(false, Session.AUTO_ACKNOWLEDGE));
        setConsumer(getSession().createConsumer(getInput()));
        getConsumer().setMessageListener(this);
        getConnection().start();

        System.setProperty(P_APP_NAME, APP_NAME);
        try {
            System.setProperty(P_HOSTNAME, InetAddress.getLocalHost().getHostName());
        } catch (UnknownHostException e) {
            LOGGER.log(Level.WARNING, "Couldn't determine hostname", e);
            System.setProperty(P_HOSTNAME, D_HOSTNAME);
        }
    } catch (JMSException ex) {
        LOGGER.log(Level.SEVERE, "Failed to start MessageConsumer", ex);
        setStatus(STATUS_FAILED);
    }
}

From source file:org.jboss.processFlow.knowledgeService.AsyncBAMProducerPool.java

public Object makeObject() throws JMSException {
    log.info("makeObject() ");
    Session pSession = connectObj.createSession(true, Session.AUTO_ACKNOWLEDGE);
    return new BAMProducerWrapper(pSession, dQueue);
}

From source file:eu.learnpad.simulator.mon.manager.GlimpseManager.java

public void setupConnection(TopicConnectionFactory connectionFact, InitialContext initConn) {
    try {//from w  ww .  j a v a 2s .  co m
        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(),
                "Creating connection object ");
        connection = connectionFact.createTopicConnection();
        DebugMessages.ok();

        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(),
                "Creating public session object ");
        publishSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        DebugMessages.ok();

        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(),
                "Creating subscribe object");
        subscribeSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        DebugMessages.ok();

        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(),
                "Setting up destination topic ");
        connectionTopic = (Topic) initConn.lookup(serviceTopic);
        tPub = publishSession.createPublisher(connectionTopic);
        DebugMessages.ok();

        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(),
                "Setting up reading topic ");
        tSub = subscribeSession.createSubscriber(connectionTopic, "DESTINATION = 'monitor'", true);
        tSub.setMessageListener(this);
        DebugMessages.ok();

        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(),
                "Creating response dispatcher ");
        responder = new ResponseDispatcher(initConn, connectionFact, requestMap, learnerAssessmentManager);
        if (responder != null)
            DebugMessages.ok();

    } catch (JMSException e) {
        e.printStackTrace();
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

From source file:eu.learnpad.simulator.mon.impl.ComplexEventProcessorImpl.java

public void init(TopicConnectionFactory connectionFact, InitialContext initConn) {
    try {//w  ww  .ja v a  2 s.c  o m
        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(),
                "Creating connection object ");
        connection = connectionFact.createTopicConnection();
        DebugMessages.ok();

        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(),
                "Creating public session object ");
        publishSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        DebugMessages.ok();

        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(),
                "Creating subscribe object ");
        subscribeSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        DebugMessages.ok();

        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(),
                "Setting up reading topic ");
        connectionTopic = (Topic) initConn.lookup(topic);
        tSub = subscribeSession.createSubscriber(connectionTopic, null, true);
        DebugMessages.ok();

        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(),
                "Setting up destination topic ");
        connectionTopic = publishSession.createTopic(this.topic);
        tPub = publishSession.createPublisher(connectionTopic);
        DebugMessages.ok();

        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(),
                "Reading knowledge base ");

        knowledgeBase = createKnowledgeBase();
        ksession = knowledgeBase.newStatefulKnowledgeSession();
        ksession.setGlobal("EVENTS EntryPoint", eventStream);
        eventStream = ksession.getWorkingMemoryEntryPoint("DEFAULT");
        cepRuleManager = new DroolsRulesManager(knowledgeBuilder, knowledgeBase, ksession);
        DebugMessages.ok();

    } catch (JMSException e) {
        DebugMessages.println(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(), e.getMessage());
    } catch (NamingException e) {
        DebugMessages.println(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(), e.getMessage());
    }
}

From source file:org.wso2.extension.siddhi.io.jms.source.client.JMSClient.java

public void sendJMSEvents(String filePath, String topicName, String queueName, String format, String broker,
        String providerURL) {/* w  ww .j  a  v  a  2s .  c o m*/
    if (format == null || "map".equals(format)) {
        format = "csv";
    }
    if ("".equalsIgnoreCase(broker)) {
        broker = "activemq";
    }
    Session session = null;
    Properties properties = new Properties();
    if (!"activemq".equalsIgnoreCase(broker) && !"mb".equalsIgnoreCase(broker)
            && !"qpid".equalsIgnoreCase(broker)) {
        log.error("Please enter a valid JMS message broker. (ex: activemq, mb, qpid");
        return;
    }
    try {
        if (topicName != null && !"".equalsIgnoreCase(topicName)) {
            TopicConnection topicConnection;
            TopicConnectionFactory connFactory = null;
            if ("activemq".equalsIgnoreCase(broker)) {
                properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("activemq.properties"));
                // to provide custom provider urls
                if (providerURL != null) {
                    properties.put(Context.PROVIDER_URL, providerURL);
                }
                Context context = new InitialContext(properties);
                connFactory = (TopicConnectionFactory) context.lookup("ConnectionFactory");
            } else if ("mb".equalsIgnoreCase(broker)) {
                properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("mb.properties"));
                Context context = new InitialContext(properties);
                connFactory = (TopicConnectionFactory) context.lookup("qpidConnectionFactory");
            } else if ("qpid".equalsIgnoreCase(broker)) {
                properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("qpid.properties"));
                Context context = new InitialContext(properties);
                connFactory = (TopicConnectionFactory) context.lookup("qpidConnectionFactory");
            }
            if (connFactory != null) {
                topicConnection = connFactory.createTopicConnection();
                topicConnection.start();
                session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
                if (session != null) {
                    Topic topic = session.createTopic(topicName);
                    MessageProducer producer = session.createProducer(topic);
                    List<String> messagesList = JMSClientUtil.readFile(filePath);
                    try {
                        if ("csv".equalsIgnoreCase(format)) {
                            log.info("Sending Map messages on '" + topicName + "' topic");
                            JMSClientUtil.publishMapMessage(producer, session, messagesList);

                        } else {
                            log.info("Sending  " + format + " messages on '" + topicName + "' topic");
                            JMSClientUtil.publishTextMessage(producer, session, messagesList);
                        }
                        log.info("All Order Messages sent");
                    } catch (JMSException e) {
                        log.error("Cannot subscribe." + e.getMessage(), e);
                    } catch (IOException e) {
                        log.error("Error when reading the data file." + e.getMessage(), e);
                    } finally {
                        producer.close();
                        session.close();
                        topicConnection.stop();
                    }
                }
            } else {
                log.error("Error when creating connection factory. Please check necessary jar files");
            }
        } else if (queueName != null && !queueName.equalsIgnoreCase("")) {
            QueueConnection queueConnection;
            QueueConnectionFactory connFactory = null;
            if ("activemq".equalsIgnoreCase(broker)) {
                properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("activemq.properties"));
                Context context = new InitialContext(properties);
                connFactory = (QueueConnectionFactory) context.lookup("ConnectionFactory");
            } else if ("mb".equalsIgnoreCase(broker)) {
                properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("mb.properties"));
                Context context = new InitialContext(properties);
                connFactory = (QueueConnectionFactory) context.lookup("qpidConnectionFactory");
            } else if ("qpid".equalsIgnoreCase(broker)) {
                properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("qpid.properties"));
                Context context = new InitialContext(properties);
                connFactory = (QueueConnectionFactory) context.lookup("qpidConnectionFactory");
            }
            if (connFactory != null) {
                queueConnection = connFactory.createQueueConnection();
                queueConnection.start();
                session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
                if (session != null) {
                    Queue queue = session.createQueue(queueName);
                    MessageProducer producer = session.createProducer(queue);
                    List<String> messagesList = JMSClientUtil.readFile(filePath);
                    try {
                        if ("csv".equalsIgnoreCase(format)) {
                            log.info("Sending Map messages on '" + queueName + "' queue");
                            JMSClientUtil.publishMapMessage(producer, session, messagesList);

                        } else {
                            log.info("Sending  " + format + " messages on '" + queueName + "' queue");
                            JMSClientUtil.publishTextMessage(producer, session, messagesList);
                        }
                    } catch (JMSException e) {
                        log.error("Cannot subscribe." + e.getMessage(), e);
                    } catch (IOException e) {
                        log.error("Error when reading the data file." + e.getMessage(), e);
                    } finally {
                        producer.close();
                        session.close();
                        queueConnection.stop();
                    }
                }
            } else {
                log.error("Error when creating connection factory. Please check necessary jar files");
            }
        } else {
            log.error("Enter queue name or topic name to be published!");
        }
    } catch (Exception e) {
        log.error("Error when publishing" + e.getMessage(), e);
    }
}

From source file:org.ct.amq.jdbc.security.JdbcAuthenticationPluginTest.java

public void testTempDestinations() throws Exception {
    Connection conn = factory.createConnection("guest", "password");
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    String name = "org.apache.activemq:BrokerName=localhost,Type=TempTopic";
    try {//  w w w. jav a2 s  .  c o  m
        conn.start();
        TemporaryTopic temp = sess.createTemporaryTopic();
        name += ",Destination=" + temp.getTopicName().replaceAll(":", "_");
        fail("Should have failed creating a temp topic");
    } catch (Exception ignore) {
    }

    ObjectName objName = new ObjectName(name);
    TopicViewMBean mbean = (TopicViewMBean) broker.getManagementContext().newProxyInstance(objName,
            TopicViewMBean.class, true);
    try {
        System.out.println(mbean.getName());
        fail("Shouldn't have created a temp topic");
    } catch (Exception ignore) {
    }
}

From source file:it.cnr.isti.labsedc.glimpse.impl.ComplexEventProcessorImpl.java

public void init(TopicConnectionFactory connectionFact, InitialContext initConn) {
    try {//from   w  w  w.j  a v  a 2 s.co m
        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(),
                "Creating connection object ");
        connection = connectionFact.createTopicConnection();
        DebugMessages.ok();

        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(),
                "Creating public session object ");
        publishSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        DebugMessages.ok();

        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(),
                "Creating subscribe object ");
        subscribeSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        DebugMessages.ok();

        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(),
                "Setting up reading topic ");
        connectionTopic = (Topic) initConn.lookup(topic);
        tSub = subscribeSession.createSubscriber(connectionTopic, null, true);
        DebugMessages.ok();

        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(),
                "Setting up destination topic ");
        connectionTopic = publishSession.createTopic(this.topic);
        tPub = publishSession.createPublisher(connectionTopic);
        DebugMessages.ok();

        DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(),
                "Reading knowledge base ");
        knowledgeBase = createKnowledgeBase();
        ksession = knowledgeBase.newStatefulKnowledgeSession();
        ksession.setGlobal("EVENTS EntryPoint", eventStream);
        eventStream = ksession.getWorkingMemoryEntryPoint("DEFAULT");
        cepRuleManager = new DroolsRulesManager(knowledgeBuilder, knowledgeBase, ksession);
        DebugMessages.ok();

    } catch (JMSException e) {
        DebugMessages.println(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(), e.getMessage());
    } catch (NamingException e) {
        DebugMessages.println(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(), e.getMessage());
    }
}