use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class ChannelPublishingJmsMessageListener method onMessage.
@Override
public void onMessage(javax.jms.Message jmsMessage, Session session) throws JMSException {
Object result = jmsMessage;
Message<?> requestMessage = null;
boolean errors = false;
try {
if (this.extractRequestPayload) {
result = this.messageConverter.fromMessage(jmsMessage);
if (this.logger.isDebugEnabled()) {
this.logger.debug("converted JMS Message [" + jmsMessage + "] to integration Message payload [" + result + "]");
}
}
Map<String, Object> headers = this.headerMapper.toHeaders(jmsMessage);
requestMessage = (result instanceof Message<?>) ? this.messageBuilderFactory.fromMessage((Message<?>) result).copyHeaders(headers).build() : this.messageBuilderFactory.withPayload(result).copyHeaders(headers).build();
} catch (RuntimeException e) {
MessageChannel errorChannel = this.gatewayDelegate.getErrorChannel();
if (errorChannel == null) {
throw e;
}
this.gatewayDelegate.getMessagingTemplate().send(errorChannel, this.gatewayDelegate.buildErrorMessage(new MessagingException("Inbound conversion failed for: " + jmsMessage, e)));
errors = true;
}
if (!errors) {
if (!this.expectReply) {
this.gatewayDelegate.send(requestMessage);
} else {
Message<?> replyMessage = this.gatewayDelegate.sendAndReceiveMessage(requestMessage);
if (replyMessage != null) {
Destination destination = this.getReplyDestination(jmsMessage, session);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Reply destination: " + destination);
}
if (destination != null) {
// convert SI Message to JMS Message
Object replyResult = replyMessage;
if (this.extractReplyPayload) {
replyResult = replyMessage.getPayload();
}
try {
javax.jms.Message jmsReply = this.messageConverter.toMessage(replyResult, session);
// map SI Message Headers to JMS Message Properties/Headers
this.headerMapper.fromHeaders(replyMessage.getHeaders(), jmsReply);
this.copyCorrelationIdFromRequestToReply(jmsMessage, jmsReply);
this.sendReply(jmsReply, destination, session);
} catch (RuntimeException e) {
this.logger.error("Failed to generate JMS Reply Message from: " + replyResult, e);
throw e;
}
}
} else if (this.logger.isDebugEnabled()) {
this.logger.debug("expected a reply but none was received");
}
}
}
}
use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class AttributePollingMessageSource method doReceive.
/**
* Retrieves the JMX attribute value.
*/
@Override
protected Object doReceive() {
Assert.notNull(this.server, "MBeanServer is required");
Assert.notNull(this.objectName, "object name is required");
Assert.notNull(this.attributeName, "attribute name is required");
try {
return this.server.getAttribute(this.objectName, this.attributeName);
} catch (Exception e) {
throw new MessagingException("failed to retrieve JMX attribute '" + this.attributeName + "' on MBean [" + this.objectName + "]", e);
}
}
use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class OperationInvokingMessageHandler method handleRequestMessage.
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
ObjectName objectName = this.resolveObjectName(requestMessage);
String operationName = this.resolveOperationName(requestMessage);
Map<String, Object> paramsFromMessage = this.resolveParameters(requestMessage);
try {
MBeanInfo mbeanInfo = this.server.getMBeanInfo(objectName);
MBeanOperationInfo[] opInfoArray = mbeanInfo.getOperations();
boolean hasNoArgOption = false;
for (MBeanOperationInfo opInfo : opInfoArray) {
if (operationName.equals(opInfo.getName())) {
MBeanParameterInfo[] paramInfoArray = opInfo.getSignature();
if (paramInfoArray.length == 0) {
hasNoArgOption = true;
}
if (paramInfoArray.length == paramsFromMessage.size()) {
int index = 0;
Object[] values = new Object[paramInfoArray.length];
String[] signature = new String[paramInfoArray.length];
for (MBeanParameterInfo paramInfo : paramInfoArray) {
Object value = paramsFromMessage.get(paramInfo.getName());
if (value == null) {
/*
* With Spring 3.2.3 and greater, the parameter names are
* registered instead of the JVM's default p1, p2 etc.
* Fall back to that naming style if not found.
*/
value = paramsFromMessage.get("p" + (index + 1));
}
if (value != null && valueTypeMatchesParameterType(value, paramInfo)) {
values[index] = value;
signature[index] = paramInfo.getType();
index++;
}
}
if (index == paramInfoArray.length) {
return this.server.invoke(objectName, operationName, values, signature);
}
}
}
}
if (hasNoArgOption) {
return this.server.invoke(objectName, operationName, null, null);
}
throw new MessagingException(requestMessage, "failed to find JMX operation '" + operationName + "' on MBean [" + objectName + "] of type [" + mbeanInfo.getClassName() + "] with " + paramsFromMessage.size() + " parameters: " + paramsFromMessage);
} catch (JMException e) {
throw new MessageHandlingException(requestMessage, "failed to invoke JMX operation '" + operationName + "' on MBean [" + objectName + "]" + " with " + paramsFromMessage.size() + " parameters: " + paramsFromMessage, e);
} catch (IOException e) {
throw new MessageHandlingException(requestMessage, "IOException on MBeanServerConnection", e);
}
}
use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class PresenceListeningEndpointTests method testWithErrorChannel.
@Test
public void testWithErrorChannel() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
XMPPConnection connection = mock(XMPPConnection.class);
bf.registerSingleton(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, connection);
PresenceListeningEndpoint endpoint = new PresenceListeningEndpoint();
DirectChannel outChannel = new DirectChannel();
outChannel.subscribe(message -> {
throw new RuntimeException("ooops");
});
PollableChannel errorChannel = new QueueChannel();
endpoint.setBeanFactory(bf);
endpoint.setOutputChannel(outChannel);
endpoint.setErrorChannel(errorChannel);
endpoint.afterPropertiesSet();
RosterListener listener = (RosterListener) TestUtils.getPropertyValue(endpoint, "rosterListener");
Presence presence = new Presence(Type.available);
listener.presenceChanged(presence);
ErrorMessage msg = (ErrorMessage) errorChannel.receive();
assertSame(presence, ((MessagingException) msg.getPayload()).getFailedMessage().getPayload());
}
use of org.springframework.messaging.MessagingException in project spring-cloud-stream by spring-cloud.
the class StreamListenerHandlerMethodTests method testMethodWithMultipleOutputParameters.
@Test
public void testMethodWithMultipleOutputParameters() throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(TestMethodWithMultipleOutputParameters.class, "--server.port=0", "--spring.jmx.enabled=false");
Processor processor = context.getBean(Processor.class);
StreamListenerTestUtils.FooOutboundChannel1 source2 = context.getBean(StreamListenerTestUtils.FooOutboundChannel1.class);
final CountDownLatch latch = new CountDownLatch(2);
((SubscribableChannel) processor.output()).subscribe(new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
Assert.isTrue(message.getPayload().equals("testing"), "Assert failed");
Assert.isTrue(message.getHeaders().get("output").equals("output2"), "Assert failed");
latch.countDown();
}
});
((SubscribableChannel) source2.output()).subscribe(new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
Assert.isTrue(message.getPayload().equals("TESTING"), "Assert failed");
Assert.isTrue(message.getHeaders().get("output").equals("output1"), "Assert failed");
latch.countDown();
}
});
processor.input().send(MessageBuilder.withPayload("testING").setHeader("output", "output1").build());
processor.input().send(MessageBuilder.withPayload("TESTing").setHeader("output", "output2").build());
assertThat(latch.await(1, TimeUnit.SECONDS));
context.close();
}
Aggregations