Search in sources :

Example 51 with MessagingException

use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.

the class UnicastReceivingChannelAdapter method run.

@Override
public void run() {
    getSocket();
    ApplicationEventPublisher publisher = getApplicationEventPublisher();
    if (publisher != null) {
        publisher.publishEvent(new UdpServerListeningEvent(this, getPort()));
    }
    if (logger.isDebugEnabled()) {
        logger.debug("UDP Receiver running on port:" + this.getPort());
    }
    setListening(true);
    // Just schedule the packet for processing.
    while (this.isActive()) {
        try {
            asyncSendMessage(receive());
        } catch (SocketTimeoutException e) {
        // continue
        } catch (SocketException e) {
            this.stop();
        } catch (Exception e) {
            if (e instanceof MessagingException) {
                throw (MessagingException) e;
            }
            throw new MessagingException("failed to receive DatagramPacket", e);
        }
    }
    this.setListening(false);
}
Also used : SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) MessagingException(org.springframework.messaging.MessagingException) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) MessagingException(org.springframework.messaging.MessagingException) IOException(java.io.IOException) SocketException(java.net.SocketException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) SocketTimeoutException(java.net.SocketTimeoutException)

Example 52 with MessagingException

use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.

the class UnicastReceivingChannelAdapter method getSocket.

public synchronized DatagramSocket getSocket() {
    if (this.socket == null) {
        try {
            DatagramSocket socket = null;
            String localAddress = this.getLocalAddress();
            int port = super.getPort();
            if (localAddress == null) {
                socket = port == 0 ? new DatagramSocket() : new DatagramSocket(port);
            } else {
                InetAddress whichNic = InetAddress.getByName(localAddress);
                socket = new DatagramSocket(new InetSocketAddress(whichNic, port));
            }
            setSocketAttributes(socket);
            this.socket = socket;
        } catch (IOException e) {
            throw new MessagingException("failed to create DatagramSocket", e);
        }
    }
    return this.socket;
}
Also used : DatagramSocket(java.net.DatagramSocket) MessagingException(org.springframework.messaging.MessagingException) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Example 53 with MessagingException

use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.

the class MulticastReceivingChannelAdapter method getSocket.

@Override
public synchronized DatagramSocket getSocket() {
    if (getTheSocket() == null) {
        try {
            int port = getPort();
            MulticastSocket socket = port == 0 ? new MulticastSocket() : new MulticastSocket(port);
            String localAddress = this.getLocalAddress();
            if (localAddress != null) {
                InetAddress whichNic = InetAddress.getByName(localAddress);
                socket.setInterface(whichNic);
            }
            setSocketAttributes(socket);
            socket.joinGroup(InetAddress.getByName(this.group));
            setSocket(socket);
        } catch (IOException e) {
            throw new MessagingException("failed to create DatagramSocket", e);
        }
    }
    return super.getSocket();
}
Also used : MulticastSocket(java.net.MulticastSocket) MessagingException(org.springframework.messaging.MessagingException) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Example 54 with MessagingException

use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.

the class MqttPahoMessageHandler method checkConnection.

private synchronized IMqttAsyncClient checkConnection() throws MqttException {
    if (this.client != null && !this.client.isConnected()) {
        this.client.close();
        this.client = null;
    }
    if (this.client == null) {
        try {
            MqttConnectOptions connectionOptions = this.clientFactory.getConnectionOptions();
            Assert.state(this.getUrl() != null || connectionOptions.getServerURIs() != null, "If no 'url' provided, connectionOptions.getServerURIs() must not be null");
            IMqttAsyncClient client = this.clientFactory.getAsyncClientInstance(this.getUrl(), this.getClientId());
            incrementClientInstance();
            client.setCallback(this);
            client.connect(connectionOptions).waitForCompletion(this.completionTimeout);
            this.client = client;
            if (logger.isDebugEnabled()) {
                logger.debug("Client connected");
            }
        } catch (MqttException e) {
            throw new MessagingException("Failed to connect", e);
        }
    }
    return this.client;
}
Also used : MessagingException(org.springframework.messaging.MessagingException) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) MqttException(org.eclipse.paho.client.mqttv3.MqttException) IMqttAsyncClient(org.eclipse.paho.client.mqttv3.IMqttAsyncClient)

Example 55 with MessagingException

use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.

the class BackToBackAdapterTests method testAddRemoveTopic.

@Test
public void testAddRemoveTopic() {
    MqttPahoMessageHandler adapter = new MqttPahoMessageHandler("tcp://localhost:1883", "si-test-out");
    adapter.setDefaultTopic("mqtt-foo");
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.afterPropertiesSet();
    adapter.start();
    MqttPahoMessageDrivenChannelAdapter inbound = new MqttPahoMessageDrivenChannelAdapter("tcp://localhost:1883", "si-test-in");
    QueueChannel outputChannel = new QueueChannel();
    inbound.setOutputChannel(outputChannel);
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.initialize();
    inbound.setTaskScheduler(taskScheduler);
    inbound.setBeanFactory(mock(BeanFactory.class));
    inbound.afterPropertiesSet();
    inbound.start();
    inbound.addTopic("mqtt-foo");
    adapter.handleMessage(new GenericMessage<String>("foo"));
    Message<?> out = outputChannel.receive(20_000);
    assertNotNull(out);
    assertEquals("foo", out.getPayload());
    assertEquals("mqtt-foo", out.getHeaders().get(MqttHeaders.RECEIVED_TOPIC));
    inbound.addTopic("mqtt-bar");
    adapter.handleMessage(MessageBuilder.withPayload("bar").setHeader(MqttHeaders.TOPIC, "mqtt-bar").build());
    out = outputChannel.receive(20_000);
    assertNotNull(out);
    assertEquals("bar", out.getPayload());
    assertEquals("mqtt-bar", out.getHeaders().get(MqttHeaders.RECEIVED_TOPIC));
    inbound.removeTopic("mqtt-bar");
    adapter.handleMessage(MessageBuilder.withPayload("bar").setHeader(MqttHeaders.TOPIC, "mqtt-bar").build());
    out = outputChannel.receive(1);
    assertNull(out);
    try {
        inbound.addTopic("mqtt-foo");
        fail("Expected exception");
    } catch (MessagingException e) {
        assertEquals("Topic 'mqtt-foo' is already subscribed.", e.getMessage());
    }
    inbound.addTopic("mqqt-bar", "mqqt-baz");
    inbound.removeTopic("mqqt-bar", "mqqt-baz");
    inbound.addTopics(new String[] { "mqqt-bar", "mqqt-baz" }, new int[] { 0, 0 });
    inbound.removeTopic("mqqt-bar", "mqqt-baz");
    adapter.stop();
    inbound.stop();
}
Also used : MqttPahoMessageDrivenChannelAdapter(org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) MqttPahoMessageHandler(org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler) BeanFactory(org.springframework.beans.factory.BeanFactory) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Aggregations

MessagingException (org.springframework.messaging.MessagingException)143 Test (org.junit.Test)60 IOException (java.io.IOException)25 Message (org.springframework.messaging.Message)23 QueueChannel (org.springframework.integration.channel.QueueChannel)22 ErrorMessage (org.springframework.messaging.support.ErrorMessage)21 CountDownLatch (java.util.concurrent.CountDownLatch)17 BeanFactory (org.springframework.beans.factory.BeanFactory)15 MessageChannel (org.springframework.messaging.MessageChannel)15 MessageHandlingException (org.springframework.messaging.MessageHandlingException)15 GenericMessage (org.springframework.messaging.support.GenericMessage)15 MessageHandler (org.springframework.messaging.MessageHandler)14 File (java.io.File)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 Matchers.containsString (org.hamcrest.Matchers.containsString)12 DirectChannel (org.springframework.integration.channel.DirectChannel)12 ArrayList (java.util.ArrayList)11 PollableChannel (org.springframework.messaging.PollableChannel)11 Lock (java.util.concurrent.locks.Lock)8 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)8