Java Code Examples for org.springframework.messaging.MessageHandlingException

The following examples show how to use org.springframework.messaging.MessageHandlingException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-cloud-gcp   Source File: PubSubMessageSourceTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void doReceive_autoModeNacksAutomatically() {

	PubSubMessageSource pubSubMessageSource = new PubSubMessageSource(
			this.mockPubSubSubscriberOperations, "sub1");
	pubSubMessageSource.setMaxFetchSize(1);
	pubSubMessageSource.setPayloadType(String.class);
	pubSubMessageSource.setAckMode(AckMode.AUTO);
	MessageSourcePollingTemplate poller = new MessageSourcePollingTemplate(pubSubMessageSource);
	assertThatThrownBy(() -> {
		poller.poll((message) -> {
			throw new RuntimeException("Nope.");
		});
	}).isInstanceOf(MessageHandlingException.class).hasMessageContaining("Nope.");

	verify(this.msg1).nack();
}
 
Example 2
Source Project: spring-cloud-gcp   Source File: PubSubMessageSourceTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void doReceive_autoAckModeDoesNotNackAutomatically() {

	PubSubMessageSource pubSubMessageSource = new PubSubMessageSource(
			this.mockPubSubSubscriberOperations, "sub1");
	pubSubMessageSource.setMaxFetchSize(1);
	pubSubMessageSource.setPayloadType(String.class);
	pubSubMessageSource.setAckMode(AckMode.AUTO_ACK);
	MessageSourcePollingTemplate poller = new MessageSourcePollingTemplate(pubSubMessageSource);
	assertThatThrownBy(() -> {
		poller.poll((message) -> {
			throw new RuntimeException("Nope.");
		});
	}).isInstanceOf(MessageHandlingException.class).hasMessageContaining("Nope.");

	verify(this.msg1, times(0)).nack();
}
 
Example 3
@Override
protected void doWrite(Message<?> message) throws Exception {
	Object payload = message.getPayload();
	if (payload instanceof String) {
		String data = (String)payload;
		if (delimiter != null) {
			processor.onNext(Buffer.wrap(data+delimiter));
		} else {
			processor.onNext(Buffer.wrap(data));
		}
		if (meter != null) {
			if ((meterCount++ % rateInterval) == 0) {
				meter.mark(rateInterval);
				log.info("METER: 1 minute rate = " + meter.getOneMinuteRate() + " mean rate = " + meter.getMeanRate());
			}
		}
	} else {
		throw new MessageHandlingException(message, "message not a String");
	}
}
 
Example 4
Source Project: spring-integration-zmq   Source File: ZmqOutboundGateway.java    License: Apache License 2.0 6 votes vote down vote up
protected Object handleRequestMessage(final Message<?> requestMessage) {
	
	if (!running) {
		return null;
	}
	
	Future<Object> response = executorService.submit(new Callable<Object>() {
		public Object call() throws Exception {
			byte[] requestData = requestConverter.convert(requestMessage.getPayload());
			socket.send(requestData);
			byte[] replyData = socket.recv();
			if (replyData == null) {
				socket.close();
				ZmqOutboundGateway.this.connect();
				throw ZmqEndpointUtil.buildMessageHandlingException(requestMessage, socket.base().errno());
			}
			return replyConverter.convert(replyData);
		}
	});
			
	try {
		return response.get();
	} catch (Throwable t) {
		throw new MessageHandlingException(requestMessage, t);
	}
}
 
Example 5
@Test(expected = MessageHandlingException.class)
public void testEvaluationIncorrectTupleInput() {
	Tuple incompleteInputTuple = TupleBuilder.tuple()
			//	missing data type
			.put(TF_SHAPE, new long[0])
			.put(TF_VALUE, new byte[0])
			.build();
	testEvaluation(incompleteInputTuple);
}
 
Example 6
@Override
protected final void handleMessageInternal(Message<?> message) throws Exception {
	try {
		doWrite(message);
	}
	catch (Exception e) {
		throw new MessageHandlingException(message,
				"failed to write Message payload to GPDB/HAWQ", e);
	}
}
 
Example 7
private void requeueOrNack(Message<?> message, AcknowledgmentCallback ackCallback, MessagingException e) {
	if (!ackCallback.isAcknowledged() && shouldRequeue(e)) {
		AckUtils.requeue(ackCallback);
	}
	else {
		AckUtils.autoNack(ackCallback);
		if (e.getFailedMessage().equals(message)) {
			throw e;
		}
		throw new MessageHandlingException(message, e);
	}
}
 
Example 8
private void doHandleMessage(MessageHandler handler, Message<?> message) {
	try {
		handler.handleMessage(message);
	}
	catch (Throwable t) { // NOSONAR
		throw new MessageHandlingException(message, t);
	}
}
 
Example 9
protected Object handleRequestMessage(final Message<?> requestMessage) {
	if (!running) {
		return null;
	}
	
	Future<Object> response = executorService.submit(new Callable<Object>() {
		public Object call() throws Exception {
			byte[] requestData = requestConverter.convert(requestMessage.getPayload());
			int retriesLeft = retryCount;
			while (!Thread.currentThread().isInterrupted()) {
				socket.send(requestData);
				PollItem items[] = { new PollItem(socket, Poller.POLLIN) };
				int rc = ZMQ.poll(items, socketReceiveTimeout);
				if (rc == -1) {
					break;
				}
				if (items[0].isReadable()) {
					byte[] reply = socket.recv();
					return replyConverter.convert(reply);
				} else if (--retriesLeft == 0) {
					break;
				} else {
					ZmqLazyPirateGateway.this.connect();
				}
			}
			ZmqLazyPirateGateway.this.connect();
			return null;
		}
	});
			
	try {
		return response.get();
	} catch (Throwable t) {
		throw new MessageHandlingException(requestMessage, t);
	}
}
 
Example 10
Source Project: spring-integration-zmq   Source File: ZmqEndpointUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static MessageHandlingException buildMessageHandlingException(Message<?> failedMessage, int errno)  {
	if (errno == ZError.ETERM) {
		return new MessageHandlingException(failedMessage, "ZMQ Context has been terminated");
	} else if (errno == ZError.EAGAIN) {
		return new MessageHandlingException(failedMessage, "ZMQ socket timeout out while attempting to send or receive data over zmq socket");
	} else {
		return new MessageHandlingException(failedMessage, "Unknown error while attempting to send or receive data over zmq socket");
	}
}
 
Example 11
@Override
protected void handleMissingValue(String name, MethodParameter parameter, Message<?> message) {
	throw new MessageHandlingException(message, "Missing path template variable '" + name + "' " +
			"for method parameter type [" + parameter.getParameterType() + "]");
}
 
Example 12
@Override
protected void handleMissingValue(String headerName, MethodParameter parameter, Message<?> message) {
	throw new MessageHandlingException(message, "Missing header '" + headerName +
			"' for method parameter type [" + parameter.getParameterType() + "]");
}
 
Example 13
@Override
protected void handleMissingValue(String name, MethodParameter parameter, Message<?> message) {
	throw new MessageHandlingException(message, "Missing path template variable '" + name + "' " +
			"for method parameter type [" + parameter.getParameterType() + "]");
}
 
Example 14
@Override
protected void handleMissingValue(String headerName, MethodParameter parameter, Message<?> message) {
	throw new MessageHandlingException(message, "Missing header '" + headerName +
			"' for method parameter type [" + parameter.getParameterType() + "]");
}
 
Example 15
@Test(expected = MessageHandlingException.class)
public void resolveArgumentNotFound() throws Exception {
	Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).build();
	this.resolver.resolveArgument(this.resolvable.annot(headerPlain()).arg(), message);
}
 
Example 16
@Test(expected = MessageHandlingException.class)
public void resolveArgumentNotFound() throws Exception {
	Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).build();
	this.resolver.resolveArgument(this.resolvable.annot(destinationVar().noValue()).arg(), message);
}
 
Example 17
@Test(expected = MessageHandlingException.class)
public void resolveArgumentNotFound() {
	Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).build();
	resolveArgument(this.resolvable.annot(headerPlain()).arg(), message);
}
 
Example 18
@Test(expected = MessageHandlingException.class)
public void resolveArgumentNotFound() {
	Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).build();
	resolveArgument(this.resolvable.annot(destinationVar().noValue()).arg(), message);
}
 
Example 19
@Override
protected void handleMissingValue(String name, MethodParameter parameter, Message<?> message) {
	throw new MessageHandlingException(message, "Missing path template variable '" + name +
			"' for method parameter type [" + parameter.getParameterType() + "]");
}
 
Example 20
@Override
protected void handleMissingValue(String headerName, MethodParameter parameter, Message<?> message) {
	throw new MessageHandlingException(message, "Missing header '" + headerName +
			"' for method parameter type [" + parameter.getParameterType() + "]");
}
 
Example 21
@Test(expected = MessageHandlingException.class)
public void resolveArgumentNotFound() throws Exception {
	Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).build();
	this.resolver.resolveArgument(this.paramRequired, message);
}
 
Example 22
@Test(expected = MessageHandlingException.class)
public void resolveArgumentNotFound() throws Exception {
	Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).build();
	this.resolver.resolveArgument(this.paramAnnotated, message);
}
 
Example 23
Source Project: spring-cloud-gcp   Source File: PubSubMessageHandler.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected void handleMessageInternal(Message<?> message) {
	Object payload = message.getPayload();
	String topic =
			message.getHeaders().containsKey(GcpPubSubHeaders.TOPIC)
					? message.getHeaders().get(GcpPubSubHeaders.TOPIC, String.class)
					: this.topicExpression.getValue(this.evaluationContext, message, String.class);

	ListenableFuture<String> pubsubFuture;

	Map<String, String> headers = new HashMap<>();
	this.headerMapper.fromHeaders(message.getHeaders(), headers);

	pubsubFuture = this.pubSubPublisherOperations.publish(topic, payload, headers);

	if (this.publishCallback != null) {
		pubsubFuture.addCallback(this.publishCallback);
	}

	if (this.sync) {
		Long timeout = this.publishTimeoutExpression.getValue(this.evaluationContext, message, Long.class);
		try {
			if (timeout == null || timeout < 0) {
				pubsubFuture.get();
			}
			else {
				pubsubFuture.get(timeout, TimeUnit.MILLISECONDS);
			}
		}
		catch (InterruptedException ie) {
			Thread.currentThread().interrupt();
			throw new MessageHandlingException(message, ie);
		}
		catch (ExecutionException ee) {
			throw new MessageHandlingException(message, ee.getCause());
		}
		catch (TimeoutException te) {
			throw new MessageTimeoutException(message, "Timeout waiting for response from Pub/Sub publisher", te);
		}
	}
}
 
Example 24
@Override
protected void handleMissingValue(String name, MethodParameter parameter, Message<?> message) {
	throw new MessageHandlingException(message, "Missing path template variable '" + name +
			"' for method parameter type [" + parameter.getParameterType() + "]");
}
 
Example 25
@Override
protected void handleMissingValue(String headerName, MethodParameter parameter, Message<?> message) {
	throw new MessageHandlingException(message, "Missing header '" + headerName +
			"' for method parameter type [" + parameter.getParameterType() + "]");
}
 
Example 26
@Test(expected = MessageHandlingException.class)
public void resolveArgumentNotFound() throws Exception {
	Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).build();
	this.resolver.resolveArgument(this.paramRequired, message);
}
 
Example 27
@Test(expected = MessageHandlingException.class)
public void resolveArgumentNotFound() throws Exception {
	Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).build();
	this.resolver.resolveArgument(this.paramAnnotated, message);
}
 
Example 28
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testPolledConsumerWithDlq() throws Exception {
	KafkaTestBinder binder = getBinder();
	PollableSource<MessageHandler> inboundBindTarget = new DefaultPollableMessageSource(
			this.messageConverter);
	ExtendedConsumerProperties<KafkaConsumerProperties> properties = createConsumerProperties();
	properties.getExtension().setPollTimeout(1);
	properties.setMaxAttempts(2);
	properties.setBackOffInitialInterval(0);
	properties.getExtension().setEnableDlq(true);
	Map<String, Object> producerProps = KafkaTestUtils
			.producerProps(embeddedKafka.getEmbeddedKafka());
	Binding<PollableSource<MessageHandler>> binding = binder.bindPollableConsumer(
			"pollableDlq", "group-pcWithDlq", inboundBindTarget, properties);
	KafkaTemplate template = new KafkaTemplate(
			new DefaultKafkaProducerFactory<>(producerProps));
	template.send("pollableDlq", "testPollableDLQ");
	try {
		int n = 0;
		while (n++ < 100) {
			inboundBindTarget.poll(m -> {
				throw new RuntimeException("test DLQ");
			});
			Thread.sleep(100);
		}
	}
	catch (MessageHandlingException e) {
		assertThat(e.getCause().getMessage()).isEqualTo("test DLQ");
	}
	Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("dlq", "false",
			embeddedKafka.getEmbeddedKafka());
	consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
	ConsumerFactory cf = new DefaultKafkaConsumerFactory<>(consumerProps);
	Consumer consumer = cf.createConsumer();
	embeddedKafka.getEmbeddedKafka().consumeFromAnEmbeddedTopic(consumer,
			"error.pollableDlq.group-pcWithDlq");
	ConsumerRecord deadLetter = KafkaTestUtils.getSingleRecord(consumer,
			"error.pollableDlq.group-pcWithDlq");
	assertThat(deadLetter).isNotNull();
	assertThat(deadLetter.value()).isEqualTo("testPollableDLQ");
	binding.unbind();
	consumer.close();
}
 
Example 29
@Test(expected = MessageHandlingException.class)
public void sendBadRequest() throws IOException {
	TaskLaunchRequest request = new TaskLaunchRequest("maven://foo", null, null, null);
	sink.input().send(new GenericMessage<>(request));
}
 
Example 30
private static boolean isMessageException(final Throwable cause) {
    return cause instanceof InvalidTargetAddressException || cause instanceof MessageConversionException
            || cause instanceof MessageHandlingException;
}