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);
}
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;
}
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();
}
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;
}
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();
}
Aggregations