This is an automated email from the ASF dual-hosted git repository. jamesnetherton pushed a commit to branch camel-3.4.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.4.x by this push: new 88e4009 CAMEL-15834: Fix potential NullPointerException on NATS consumer stop 88e4009 is described below commit 88e400977d7de050cb984c38e098762a1c9a9b99 Author: James Netherton <jamesnether...@gmail.com> AuthorDate: Mon Nov 9 10:05:37 2020 +0000 CAMEL-15834: Fix potential NullPointerException on NATS consumer stop --- .../apache/camel/component/nats/NatsConsumer.java | 18 +++++++----- .../camel/component/nats/NatsConsumerStopTest.java | 34 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConsumer.java b/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConsumer.java index c661e55..1cada1e 100644 --- a/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConsumer.java +++ b/components/camel-nats/src/main/java/org/apache/camel/component/nats/NatsConsumer.java @@ -66,15 +66,19 @@ public class NatsConsumer extends DefaultConsumer { @Override protected void doStop() throws Exception { - if (getEndpoint().getConfiguration().isFlushConnection()) { + NatsConfiguration configuration = getEndpoint().getConfiguration(); + + if (configuration.isFlushConnection() && ObjectHelper.isNotEmpty(connection)) { LOG.debug("Flushing Messages before stopping"); - connection.flush(Duration.ofMillis(getEndpoint().getConfiguration().getFlushTimeout())); + connection.flush(Duration.ofMillis(configuration.getFlushTimeout())); } - try { - dispatcher.unsubscribe(getEndpoint().getConfiguration().getTopic()); - } catch (Exception e) { - getExceptionHandler().handleException("Error during unsubscribing", e); + if (ObjectHelper.isNotEmpty(dispatcher)) { + try { + dispatcher.unsubscribe(configuration.getTopic()); + } catch (Exception e) { + getExceptionHandler().handleException("Error during unsubscribing", e); + } } LOG.debug("Stopping Nats Consumer"); @@ -87,7 +91,7 @@ public class NatsConsumer extends DefaultConsumer { } executor = null; - if (ObjectHelper.isEmpty(getEndpoint().getConfiguration().getConnection())) { + if (ObjectHelper.isEmpty(configuration.getConnection()) && ObjectHelper.isNotEmpty(connection)) { LOG.debug("Closing Nats Connection"); if (!connection.getStatus().equals(Status.CLOSED)) { connection.close(); diff --git a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsConsumerStopTest.java b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsConsumerStopTest.java new file mode 100644 index 0000000..611ff86 --- /dev/null +++ b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsConsumerStopTest.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.nats; + +import org.apache.camel.Consumer; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +/** + * Test NATS consumer stop happens cleanly. See https://issues.apache.org/jira/browse/CAMEL-15834. + */ +public class NatsConsumerStopTest extends CamelTestSupport { + + @Test + public void testConsumerStop() throws Exception { + NatsEndpoint endpoint = context.getEndpoint("nats:test?flushConnection=true", NatsEndpoint.class); + Consumer consumer = endpoint.createConsumer(null); + consumer.stop(); + } +}