Search in sources :

Example 6 with JmsTemplate

use of org.springframework.jms.core.JmsTemplate in project camel by apache.

the class JmsRequestReplyManualReplyTest method createCamelContext.

protected CamelContext createCamelContext() throws Exception {
    CamelContext camelContext = super.createCamelContext();
    ConnectionFactory connectionFactory = CamelJmsTestHelper.createConnectionFactory();
    camelContext.addComponent("activemq", jmsComponentAutoAcknowledge(connectionFactory));
    jms = new JmsTemplate(connectionFactory);
    return camelContext;
}
Also used : CamelContext(org.apache.camel.CamelContext) ConnectionFactory(javax.jms.ConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate)

Example 7 with JmsTemplate

use of org.springframework.jms.core.JmsTemplate in project camel by apache.

the class JmsCustomJMSReplyToIssueTest method testCustomJMSReplyTo.

@Test
public void testCustomJMSReplyTo() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("Bye World");
    // start a inOnly route
    template.sendBody("direct:start", "Hello World");
    // now consume using something that is not Camel
    Thread.sleep(1000);
    JmsTemplate jms = new JmsTemplate(amq.getConfiguration().getConnectionFactory());
    TextMessage msg = (TextMessage) jms.receive("in");
    assertEquals("Hello World", msg.getText());
    // there should be a JMSReplyTo so we know where to send the reply
    Destination replyTo = msg.getJMSReplyTo();
    assertEquals("queue://myReplyQueue", replyTo.toString());
    // send reply
    template.sendBody("activemq:" + replyTo.toString(), "Bye World");
    assertMockEndpointsSatisfied();
}
Also used : Destination(javax.jms.Destination) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) JmsTemplate(org.springframework.jms.core.JmsTemplate) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 8 with JmsTemplate

use of org.springframework.jms.core.JmsTemplate in project spring-framework by spring-projects.

the class JmsTransactionManagerTests method testTransactionCommit.

@Test
public void testTransactionCommit() throws JMSException {
    ConnectionFactory cf = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    final Session session = mock(Session.class);
    given(cf.createConnection()).willReturn(con);
    given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
    JmsTransactionManager tm = new JmsTransactionManager(cf);
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    JmsTemplate jt = new JmsTemplate(cf);
    jt.execute(new SessionCallback<Void>() {

        @Override
        public Void doInJms(Session sess) {
            assertTrue(sess == session);
            return null;
        }
    });
    tm.commit(ts);
    verify(session).commit();
    verify(session).close();
    verify(con).close();
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Connection(javax.jms.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) JmsTemplate(org.springframework.jms.core.JmsTemplate) Session(javax.jms.Session) Test(org.junit.Test)

Example 9 with JmsTemplate

use of org.springframework.jms.core.JmsTemplate in project spring-framework by spring-projects.

the class JmsTransactionManagerTests method testTransactionSuspension.

@Test
public void testTransactionSuspension() throws JMSException {
    final ConnectionFactory cf = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    final Session session = mock(Session.class);
    final Session session2 = mock(Session.class);
    given(cf.createConnection()).willReturn(con);
    given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session, session2);
    JmsTransactionManager tm = new JmsTransactionManager(cf);
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    final JmsTemplate jt = new JmsTemplate(cf);
    jt.execute(new SessionCallback<Void>() {

        @Override
        public Void doInJms(Session sess) {
            assertTrue(sess == session);
            return null;
        }
    });
    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            jt.execute(new SessionCallback<Void>() {

                @Override
                public Void doInJms(Session sess) {
                    assertTrue(sess != session);
                    return null;
                }
            });
        }
    });
    jt.execute(new SessionCallback<Void>() {

        @Override
        public Void doInJms(Session sess) {
            assertTrue(sess == session);
            return null;
        }
    });
    tm.commit(ts);
    verify(session).commit();
    verify(session2).commit();
    verify(session).close();
    verify(session2).close();
    verify(con, times(2)).close();
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Connection(javax.jms.Connection) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) SessionCallback(org.springframework.jms.core.SessionCallback) ConnectionFactory(javax.jms.ConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Session(javax.jms.Session) Test(org.junit.Test)

Example 10 with JmsTemplate

use of org.springframework.jms.core.JmsTemplate in project spring-framework by spring-projects.

the class JmsTransactionManagerTests method testParticipatingTransactionWithCommit.

@Test
public void testParticipatingTransactionWithCommit() throws JMSException {
    ConnectionFactory cf = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    final Session session = mock(Session.class);
    given(cf.createConnection()).willReturn(con);
    given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
    JmsTransactionManager tm = new JmsTransactionManager(cf);
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    final JmsTemplate jt = new JmsTemplate(cf);
    jt.execute(new SessionCallback<Void>() {

        @Override
        public Void doInJms(Session sess) {
            assertTrue(sess == session);
            return null;
        }
    });
    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            jt.execute(new SessionCallback<Void>() {

                @Override
                public Void doInJms(Session sess) {
                    assertTrue(sess == session);
                    return null;
                }
            });
        }
    });
    tm.commit(ts);
    verify(session).commit();
    verify(session).close();
    verify(con).close();
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Connection(javax.jms.Connection) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) SessionCallback(org.springframework.jms.core.SessionCallback) ConnectionFactory(javax.jms.ConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

JmsTemplate (org.springframework.jms.core.JmsTemplate)45 Test (org.junit.Test)30 ConnectionFactory (javax.jms.ConnectionFactory)12 Session (javax.jms.Session)9 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)8 Connection (javax.jms.Connection)7 TransactionStatus (org.springframework.transaction.TransactionStatus)7 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)7 Bean (org.springframework.context.annotation.Bean)5 Destination (javax.jms.Destination)4 TextMessage (javax.jms.TextMessage)4 CamelContext (org.apache.camel.CamelContext)4 SessionCallback (org.springframework.jms.core.SessionCallback)4 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)4 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)4 JMSException (javax.jms.JMSException)3 Message (javax.jms.Message)3 PooledConnectionFactory (org.apache.activemq.pool.PooledConnectionFactory)3 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)3 JmsOperations (org.springframework.jms.core.JmsOperations)3