Search in sources :

Example 31 with MessageObserver

use of org.apache.cxf.transport.MessageObserver in project cxf by apache.

the class SoapBindingSelectionTest method testMultipleSoapBindings.

@Test
public void testMultipleSoapBindings() throws Exception {
    ServerFactoryBean svrBean1 = new ServerFactoryBean();
    svrBean1.setAddress("http://localhost/Hello");
    svrBean1.setServiceClass(HelloService.class);
    svrBean1.setServiceBean(new HelloServiceImpl());
    svrBean1.setBus(getBus());
    svrBean1.getInInterceptors().add(new AbstractPhaseInterceptor<Message>(Phase.USER_LOGICAL) {

        public void handleMessage(Message message) throws Fault {
            service1Invoked = true;
        }
    });
    svrBean1.create();
    ServerFactoryBean svrBean2 = new ServerFactoryBean();
    svrBean2.setAddress("http://localhost/Hello");
    svrBean2.setServiceClass(HelloService.class);
    svrBean2.setServiceBean(new HelloServiceImpl());
    svrBean2.setBus(getBus());
    svrBean2.getInInterceptors().add(new AbstractPhaseInterceptor<Message>(Phase.USER_LOGICAL) {

        public void handleMessage(Message message) throws Fault {
            service2Invoked = true;
        }
    });
    SoapBindingConfiguration config = new SoapBindingConfiguration();
    config.setVersion(Soap12.getInstance());
    svrBean2.setBindingConfig(config);
    ServerImpl server2 = (ServerImpl) svrBean2.create();
    Destination d = server2.getDestination();
    MessageObserver mo = d.getMessageObserver();
    assertTrue(mo instanceof MultipleEndpointObserver);
    MultipleEndpointObserver meo = (MultipleEndpointObserver) mo;
    assertEquals(2, meo.getEndpoints().size());
    Node nd = invoke("http://localhost/Hello", LocalTransportFactory.TRANSPORT_ID, "soap11.xml");
    assertEquals("http://schemas.xmlsoap.org/soap/envelope/", getNs(nd));
    assertTrue(service1Invoked);
    assertFalse(service2Invoked);
    service1Invoked = false;
    nd = invoke("http://localhost/Hello", LocalTransportFactory.TRANSPORT_ID, "soap12.xml");
    assertEquals("http://www.w3.org/2003/05/soap-envelope", getNs(nd));
    assertFalse(service1Invoked);
    assertTrue(service2Invoked);
    server2.stop();
    server2.start();
    nd = invoke("http://localhost/Hello", LocalTransportFactory.TRANSPORT_ID, "soap12.xml");
    assertEquals("http://www.w3.org/2003/05/soap-envelope", getNs(nd));
    assertFalse(service1Invoked);
    assertTrue(service2Invoked);
}
Also used : Destination(org.apache.cxf.transport.Destination) MessageObserver(org.apache.cxf.transport.MessageObserver) Message(org.apache.cxf.message.Message) ServerImpl(org.apache.cxf.endpoint.ServerImpl) SoapBindingConfiguration(org.apache.cxf.binding.soap.SoapBindingConfiguration) MultipleEndpointObserver(org.apache.cxf.transport.MultipleEndpointObserver) Node(org.w3c.dom.Node) ServerFactoryBean(org.apache.cxf.frontend.ServerFactoryBean) HelloServiceImpl(org.apache.cxf.service.factory.HelloServiceImpl) Fault(org.apache.cxf.interceptor.Fault) Test(org.junit.Test) AbstractSimpleFrontendTest(org.apache.cxf.service.factory.AbstractSimpleFrontendTest)

Example 32 with MessageObserver

use of org.apache.cxf.transport.MessageObserver in project cxf by apache.

the class JMSDestinationTest method testMessageObserverExceptionHandling.

@Test
public void testMessageObserverExceptionHandling() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    EndpointInfo ei = setupServiceInfo("HelloWorldPubSubService", "HelloWorldPubSubPort");
    JMSConduit conduit = setupJMSConduitWithObserver(ei);
    JMSDestination destination = setupJMSDestination(ei);
    destination.setMessageObserver(new MessageObserver() {

        @Override
        public void onMessage(Message message) {
            try {
                throw new RuntimeException("Error!");
            } finally {
                latch.countDown();
            }
        }
    });
    final Message outMessage = createMessage();
    Thread.sleep(500L);
    sendOneWayMessage(conduit, outMessage);
    latch.await(5, TimeUnit.SECONDS);
    conduit.close();
    destination.shutdown();
}
Also used : EndpointInfo(org.apache.cxf.service.model.EndpointInfo) MessageObserver(org.apache.cxf.transport.MessageObserver) Message(org.apache.cxf.message.Message) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 33 with MessageObserver

use of org.apache.cxf.transport.MessageObserver in project cxf by apache.

the class JMSDestinationTest method testProperty.

@Test
public void testProperty() throws Exception {
    EndpointInfo ei = setupServiceInfo("HelloWorldService", "HelloWorldPort");
    final String customPropertyName = "THIS_PROPERTY_WILL_NOT_BE_AUTO_COPIED";
    // set up the conduit send to be true
    JMSConduit conduit = setupJMSConduitWithObserver(ei);
    final Message outMessage = createMessage();
    JMSMessageHeadersType headers = (JMSMessageHeadersType) outMessage.get(JMSConstants.JMS_CLIENT_REQUEST_HEADERS);
    headers.putProperty(customPropertyName, customPropertyName);
    final JMSDestination destination = setupJMSDestination(ei);
    // set up MessageObserver for handling the conduit message
    MessageObserver observer = new MessageObserver() {

        public void onMessage(Message m) {
            Exchange exchange = new ExchangeImpl();
            exchange.setInMessage(m);
            m.setExchange(exchange);
            verifyReceivedMessage(m);
            verifyHeaders(m, outMessage);
            // setup the message for
            Conduit backConduit;
            try {
                backConduit = destination.getBackChannel(m);
                // wait for the message to be got from the conduit
                Message replyMessage = new MessageImpl();
                // copy the message encoding
                replyMessage.put(Message.ENCODING, m.get(Message.ENCODING));
                sendOneWayMessage(backConduit, replyMessage);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    destination.setMessageObserver(observer);
    sendMessageSync(conduit, outMessage);
    // wait for the message to be got from the destination,
    // create the thread to handler the Destination incoming message
    Message inMessage = waitForReceiveInMessage();
    verifyReceivedMessage(inMessage);
    verifyRequestResponseHeaders(inMessage, outMessage);
    JMSMessageHeadersType inHeader = (JMSMessageHeadersType) inMessage.get(JMSConstants.JMS_CLIENT_RESPONSE_HEADERS);
    assertNotNull("The inHeader should not be null", inHeader);
    // TODO we need to check the SOAP JMS transport properties here
    // wait for a while for the jms session recycling
    // Thread.sleep(1000L);
    conduit.close();
    destination.shutdown();
}
Also used : MessageObserver(org.apache.cxf.transport.MessageObserver) Message(org.apache.cxf.message.Message) InvalidClientIDException(javax.jms.InvalidClientIDException) JMSException(javax.jms.JMSException) Exchange(org.apache.cxf.message.Exchange) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) Conduit(org.apache.cxf.transport.Conduit) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 34 with MessageObserver

use of org.apache.cxf.transport.MessageObserver in project cxf by apache.

the class RequestResponseTest method sendAndReceiveMessages.

private void sendAndReceiveMessages(EndpointInfo ei, boolean synchronous) throws IOException, InterruptedException {
    // set up the conduit send to be true
    JMSConduit conduit = setupJMSConduitWithObserver(ei);
    final Message outMessage = createMessage();
    final JMSDestination destination = setupJMSDestination(ei);
    MessageObserver observer = new MessageObserver() {

        public void onMessage(Message m) {
            Exchange exchange = new ExchangeImpl();
            exchange.setInMessage(m);
            m.setExchange(exchange);
            verifyReceivedMessage(m);
            verifyHeaders(m, outMessage);
            // setup the message for
            try {
                Conduit backConduit = destination.getBackChannel(m);
                // wait for the message to be got from the conduit
                Message replyMessage = new MessageImpl();
                sendOneWayMessage(backConduit, replyMessage);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    destination.setMessageObserver(observer);
    try {
        sendMessage(conduit, outMessage, synchronous);
        // wait for the message to be got from the destination,
        // create the thread to handler the Destination incoming message
        verifyReceivedMessage(waitForReceiveInMessage());
    } finally {
        conduit.close();
        destination.shutdown();
    }
}
Also used : Exchange(org.apache.cxf.message.Exchange) MessageObserver(org.apache.cxf.transport.MessageObserver) Message(org.apache.cxf.message.Message) Conduit(org.apache.cxf.transport.Conduit) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) IOException(java.io.IOException)

Example 35 with MessageObserver

use of org.apache.cxf.transport.MessageObserver in project cxf by apache.

the class JMSDestinationTest method testTemporaryQueueDeletionUponReset.

@Test
public void testTemporaryQueueDeletionUponReset() throws Exception {
    EndpointInfo ei = setupServiceInfo("HelloWorldService", "HelloWorldPort");
    // set up the conduit send to be true
    JMSConduit conduit = setupJMSConduitWithObserver(ei);
    assertNull(conduit.getJmsConfig().getReplyDestination());
    // Store the connection so we could check temporary queues
    final AtomicReference<DestinationSource> destinationSource = new AtomicReference<>();
    final Message outMessage = createMessage();
    // Capture the DestinationSource instance associated with the connection
    final JMSDestination destination = setupJMSDestination(ei, c -> new ConnectionFactory() {

        @Override
        public Connection createConnection() throws JMSException {
            final Connection connection = c.createConnection();
            destinationSource.set(((EnhancedConnection) connection).getDestinationSource());
            return connection;
        }

        @Override
        public Connection createConnection(String userName, String password) throws JMSException {
            final Connection connection = c.createConnection(userName, password);
            destinationSource.set(((EnhancedConnection) connection).getDestinationSource());
            return connection;
        }
    });
    // set up MessageObserver for handling the conduit message
    final MessageObserver observer = new MessageObserver() {

        public void onMessage(Message m) {
            final Exchange exchange = new ExchangeImpl();
            exchange.setInMessage(m);
            m.setExchange(exchange);
            try {
                final Conduit backConduit = destination.getBackChannel(m);
                sendOneWayMessage(backConduit, new MessageImpl());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    destination.setMessageObserver(observer);
    sendMessageSync(conduit, outMessage);
    // wait for the message to be got from the destination,
    // create the thread to handler the Destination incoming message
    Message inMessage = waitForReceiveInMessage();
    verifyReceivedMessage(inMessage);
    final DestinationSource ds = destinationSource.get();
    assertThat(ds.getTemporaryQueues().size(), equalTo(1));
    // Force manual temporary queue deletion by resetting the reply destination
    conduit.getJmsConfig().resetCachedReplyDestination();
    // The queue deletion events (as well as others) are propagated asynchronously
    await().atMost(1, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ds.getTemporaryQueues().size(), equalTo(0)));
    conduit.close();
    destination.shutdown();
}
Also used : MessageObserver(org.apache.cxf.transport.MessageObserver) Message(org.apache.cxf.message.Message) Connection(javax.jms.Connection) EnhancedConnection(org.apache.activemq.EnhancedConnection) AtomicReference(java.util.concurrent.atomic.AtomicReference) JMSException(javax.jms.JMSException) InvalidClientIDException(javax.jms.InvalidClientIDException) JMSException(javax.jms.JMSException) Exchange(org.apache.cxf.message.Exchange) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) ConnectionFactory(javax.jms.ConnectionFactory) Conduit(org.apache.cxf.transport.Conduit) EnhancedConnection(org.apache.activemq.EnhancedConnection) DestinationSource(org.apache.activemq.advisory.DestinationSource) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Aggregations

MessageObserver (org.apache.cxf.transport.MessageObserver)43 Message (org.apache.cxf.message.Message)34 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)21 Conduit (org.apache.cxf.transport.Conduit)14 IOException (java.io.IOException)13 Exchange (org.apache.cxf.message.Exchange)12 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)11 Test (org.junit.Test)11 MessageImpl (org.apache.cxf.message.MessageImpl)10 EndpointReferenceType (org.apache.cxf.ws.addressing.EndpointReferenceType)10 Bus (org.apache.cxf.Bus)8 QName (javax.xml.namespace.QName)7 ConduitInitiator (org.apache.cxf.transport.ConduitInitiator)6 Fault (org.apache.cxf.interceptor.Fault)5 Destination (org.apache.cxf.transport.Destination)5 URISyntaxException (java.net.URISyntaxException)4 URL (java.net.URL)4 HashMap (java.util.HashMap)4 List (java.util.List)4 ExtensionManagerBus (org.apache.cxf.bus.extension.ExtensionManagerBus)4