Search in sources :

Example 1 with ErrorHandler

use of org.springframework.util.ErrorHandler in project camel by apache.

the class JmsConfiguration method configureMessageListenerContainer.

protected void configureMessageListenerContainer(AbstractMessageListenerContainer container, JmsEndpoint endpoint) throws Exception {
    container.setConnectionFactory(getListenerConnectionFactory());
    if (endpoint instanceof DestinationEndpoint) {
        container.setDestinationResolver(createDestinationResolver((DestinationEndpoint) endpoint));
    } else if (destinationResolver != null) {
        container.setDestinationResolver(destinationResolver);
    }
    container.setAutoStartup(autoStartup);
    if (durableSubscriptionName != null) {
        container.setDurableSubscriptionName(durableSubscriptionName);
        container.setSubscriptionDurable(true);
    }
    if (clientId != null) {
        container.setClientId(clientId);
    }
    if (exceptionListener != null) {
        container.setExceptionListener(exceptionListener);
    }
    if (errorHandler != null) {
        container.setErrorHandler(errorHandler);
    } else {
        ErrorHandler handler = new DefaultSpringErrorHandler(endpoint.getCamelContext(), EndpointMessageListener.class, getErrorHandlerLoggingLevel(), isErrorHandlerLogStackTrace());
        container.setErrorHandler(handler);
    }
    container.setAcceptMessagesWhileStopping(acceptMessagesWhileStopping);
    container.setExposeListenerSession(exposeListenerSession);
    container.setSessionTransacted(transacted);
    if (transacted) {
        container.setSessionAcknowledgeMode(Session.SESSION_TRANSACTED);
    } else {
        if (acknowledgementMode >= 0) {
            container.setSessionAcknowledgeMode(acknowledgementMode);
        } else if (acknowledgementModeName != null) {
            container.setSessionAcknowledgeModeName(acknowledgementModeName);
        }
    }
    if (endpoint.getSelector() != null && endpoint.getSelector().length() != 0) {
        container.setMessageSelector(endpoint.getSelector());
    }
    if (container instanceof DefaultMessageListenerContainer) {
        DefaultMessageListenerContainer listenerContainer = (DefaultMessageListenerContainer) container;
        configureDefaultMessageListenerContainer(endpoint, listenerContainer);
    } else if (container instanceof SimpleMessageListenerContainer) {
        SimpleMessageListenerContainer listenerContainer = (SimpleMessageListenerContainer) container;
        configureSimpleMessageListenerContainer(listenerContainer);
    }
}
Also used : DefaultMessageListenerContainer(org.springframework.jms.listener.DefaultMessageListenerContainer) ErrorHandler(org.springframework.util.ErrorHandler) SimpleMessageListenerContainer(org.springframework.jms.listener.SimpleMessageListenerContainer)

Example 2 with ErrorHandler

use of org.springframework.util.ErrorHandler in project spring-framework by spring-projects.

the class JmsNamespaceHandlerTests method testErrorHandlers.

@Test
public void testErrorHandlers() {
    ErrorHandler expected = this.context.getBean("testErrorHandler", ErrorHandler.class);
    ErrorHandler errorHandler1 = getErrorHandler("listener1");
    ErrorHandler errorHandler2 = getErrorHandler("listener2");
    ErrorHandler defaultErrorHandler = getErrorHandler(DefaultMessageListenerContainer.class.getName() + "#0");
    assertSame(expected, errorHandler1);
    assertSame(expected, errorHandler2);
    assertNull(defaultErrorHandler);
}
Also used : ErrorHandler(org.springframework.util.ErrorHandler) Test(org.junit.Test)

Example 3 with ErrorHandler

use of org.springframework.util.ErrorHandler in project Protocol-Adapter-IEC61850 by OSGP.

the class MessagingConfig method iec61850RequestsMessageListenerContainer.

@Bean
public DefaultMessageListenerContainer iec61850RequestsMessageListenerContainer(final JmsConfiguration iec61850RequestJmsConfiguration) {
    final DefaultMessageListenerContainer messageListenerContainer = iec61850RequestJmsConfiguration.getMessageListenerContainer();
    messageListenerContainer.setErrorHandler(new ErrorHandler() {

        @Override
        public void handleError(final Throwable t) {
            // Implementing ErrorHandler to prevent logging at WARN level
            // when JMSException is thrown: Execution of JMS message
            // listener failed, and no ErrorHandler has been set.
            LOGGER.debug("iec61850RequestsMessageListenerContainer.ErrorHandler.handleError()", t);
        }
    });
    return messageListenerContainer;
}
Also used : DefaultMessageListenerContainer(org.springframework.jms.listener.DefaultMessageListenerContainer) ErrorHandler(org.springframework.util.ErrorHandler) Bean(org.springframework.context.annotation.Bean)

Example 4 with ErrorHandler

use of org.springframework.util.ErrorHandler in project camel by apache.

the class JmsEndpointConfigurationTest method testSettingEndpointOptions.

@SuppressWarnings("deprecation")
@Test
public void testSettingEndpointOptions() throws Exception {
    JmsEndpoint endpoint = resolveMandatoryEndpoint("jms:queue:Foo", JmsEndpoint.class);
    endpoint.setAcceptMessagesWhileStopping(true);
    assertTrue(endpoint.isAcceptMessagesWhileStopping());
    endpoint.setAllowReplyManagerQuickStop(true);
    assertTrue(endpoint.isAllowReplyManagerQuickStop());
    endpoint.setAcknowledgementMode(2);
    assertEquals(2, endpoint.getAcknowledgementMode());
    endpoint.setAcknowledgementModeName("CLIENT_ACKNOWLEDGE");
    assertEquals("CLIENT_ACKNOWLEDGE", endpoint.getAcknowledgementModeName());
    endpoint.setAlwaysCopyMessage(true);
    assertTrue(endpoint.isAlwaysCopyMessage());
    endpoint.setCacheLevel(2);
    assertEquals(2, endpoint.getCacheLevel());
    endpoint.setCacheLevelName("foo");
    assertEquals("foo", endpoint.getCacheLevelName());
    endpoint.setClientId("bar");
    assertEquals("bar", endpoint.getClientId());
    endpoint.setConcurrentConsumers(5);
    assertEquals(5, endpoint.getConcurrentConsumers());
    endpoint.setDeliveryPersistent(true);
    assertTrue(endpoint.isDeliveryPersistent());
    endpoint.setDestinationName("cool");
    assertEquals("cool", endpoint.getDestinationName());
    endpoint.setDisableReplyTo(true);
    assertTrue(endpoint.isDisableReplyTo());
    endpoint.setEagerLoadingOfProperties(true);
    assertTrue(endpoint.isEagerLoadingOfProperties());
    endpoint.setExceptionListener(new ExceptionListener() {

        public void onException(JMSException exception) {
        }
    });
    assertNotNull(endpoint.getExceptionListener());
    endpoint.setErrorHandler(new ErrorHandler() {

        public void handleError(Throwable t) {
        }
    });
    assertNotNull(endpoint.getErrorHandler());
    endpoint.setExplicitQosEnabled(true);
    assertTrue(endpoint.isExplicitQosEnabled());
    endpoint.setExposeListenerSession(true);
    assertTrue(endpoint.isExposeListenerSession());
    endpoint.setIdleTaskExecutionLimit(5);
    assertEquals(5, endpoint.getIdleTaskExecutionLimit());
    endpoint.setIdleConsumerLimit(5);
    assertEquals(5, endpoint.getIdleConsumerLimit());
    endpoint.setMaxConcurrentConsumers(4);
    assertEquals(4, endpoint.getMaxConcurrentConsumers());
    endpoint.setMaxMessagesPerTask(9);
    assertEquals(9, endpoint.getMaxMessagesPerTask());
    endpoint.setMessageConverter(new SimpleMessageConverter());
    assertNotNull(endpoint.getMessageConverter());
    endpoint.setMessageIdEnabled(true);
    assertTrue(endpoint.isMessageIdEnabled());
    endpoint.setMessageTimestampEnabled(true);
    assertTrue(endpoint.isMessageTimestampEnabled());
    endpoint.setPreserveMessageQos(true);
    assertTrue(endpoint.isPreserveMessageQos());
    endpoint.setPriority(6);
    assertEquals(6, endpoint.getPriority());
    endpoint.setPubSubNoLocal(true);
    assertTrue(endpoint.isPubSubNoLocal());
    endpoint.setPubSubNoLocal(true);
    assertTrue(endpoint.isPubSubNoLocal());
    assertFalse(endpoint.isPubSubDomain());
    endpoint.setReceiveTimeout(5000);
    assertEquals(5000, endpoint.getReceiveTimeout());
    endpoint.setRecoveryInterval(6000);
    assertEquals(6000, endpoint.getRecoveryInterval());
    endpoint.setReplyTo("bar");
    assertEquals("bar", endpoint.getReplyTo());
    endpoint.setReplyToDeliveryPersistent(true);
    assertTrue(endpoint.isReplyToDeliveryPersistent());
    endpoint.setReplyToDestinationSelectorName("me");
    assertEquals("me", endpoint.getReplyToDestinationSelectorName());
    endpoint.setRequestTimeout(3000);
    assertEquals(3000, endpoint.getRequestTimeout());
    endpoint.setSelector("you");
    assertEquals("you", endpoint.getSelector());
    endpoint.setTimeToLive(4000);
    assertEquals(4000, endpoint.getTimeToLive());
    endpoint.setTransacted(true);
    assertTrue(endpoint.isTransacted());
    endpoint.setTransactedInOut(true);
    assertTrue(endpoint.isTransactedInOut());
    endpoint.setTransferExchange(true);
    assertTrue(endpoint.isTransferExchange());
    endpoint.setTransferException(true);
    assertTrue(endpoint.isTransferException());
    endpoint.setJmsMessageType(JmsMessageType.Text);
    assertEquals(JmsMessageType.Text, endpoint.getJmsMessageType());
}
Also used : ErrorHandler(org.springframework.util.ErrorHandler) ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Test(org.junit.Test)

Example 5 with ErrorHandler

use of org.springframework.util.ErrorHandler in project spring-framework by spring-projects.

the class SimpleMessageListenerContainerTests method testRegisteredErrorHandlerIsInvokedOnException.

@Test
public void testRegisteredErrorHandlerIsInvokedOnException() throws Exception {
    final SimpleMessageConsumer messageConsumer = new SimpleMessageConsumer();
    Session session = mock(Session.class);
    // Queue gets created in order to create MessageConsumer for that Destination...
    given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION);
    // and then the MessageConsumer gets created...
    // no MessageSelector...
    given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer);
    // an exception is thrown, so the rollback logic is being applied here...
    given(session.getTransacted()).willReturn(false);
    Connection connection = mock(Connection.class);
    // session gets created in order to register MessageListener...
    given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session);
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.createConnection()).willReturn(connection);
    final IllegalStateException theException = new IllegalStateException("intentional test failure");
    this.container.setConnectionFactory(connectionFactory);
    this.container.setDestinationName(DESTINATION_NAME);
    this.container.setMessageListener(new SessionAwareMessageListener<Message>() {

        @Override
        public void onMessage(Message message, Session session) throws JMSException {
            throw theException;
        }
    });
    ErrorHandler errorHandler = mock(ErrorHandler.class);
    this.container.setErrorHandler(errorHandler);
    this.container.afterPropertiesSet();
    this.container.start();
    // manually trigger an Exception with the above bad MessageListener...
    Message message = mock(Message.class);
    // a Throwable from a MessageListener MUST simply be swallowed...
    messageConsumer.sendMessage(message);
    verify(connection).setExceptionListener(this.container);
    verify(connection).start();
    verify(errorHandler).handleError(theException);
}
Also used : ErrorHandler(org.springframework.util.ErrorHandler) ConnectionFactory(javax.jms.ConnectionFactory) Message(javax.jms.Message) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

ErrorHandler (org.springframework.util.ErrorHandler)5 Test (org.junit.Test)3 JMSException (javax.jms.JMSException)2 DefaultMessageListenerContainer (org.springframework.jms.listener.DefaultMessageListenerContainer)2 Connection (javax.jms.Connection)1 ConnectionFactory (javax.jms.ConnectionFactory)1 ExceptionListener (javax.jms.ExceptionListener)1 Message (javax.jms.Message)1 Session (javax.jms.Session)1 Bean (org.springframework.context.annotation.Bean)1 SimpleMessageListenerContainer (org.springframework.jms.listener.SimpleMessageListenerContainer)1 SimpleMessageConverter (org.springframework.jms.support.converter.SimpleMessageConverter)1