This is an automated email from the ASF dual-hosted git repository.
acosentino pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 42a62ac19a6 fix(components/plc4x): fix NPE when Camel can not
establish connection and allow to check if endpoint established connection
(#16160)
42a62ac19a6 is described below
commit 42a62ac19a609b81edc12b39a4fac0099127ee59
Author: jmeierhofer <[email protected]>
AuthorDate: Tue Nov 5 11:57:35 2024 +0100
fix(components/plc4x): fix NPE when Camel can not establish connection and
allow to check if endpoint established connection (#16160)
Co-authored-by: Jochen Meierhofer <[email protected]>
---
.../org/apache/camel/component/plc4x/Plc4XEndpoint.java | 17 ++++++++++++-----
.../org/apache/camel/component/plc4x/Plc4XProducer.java | 9 ++++++---
2 files changed, 18 insertions(+), 8 deletions(-)
diff --git
a/components/camel-plc4x/src/main/java/org/apache/camel/component/plc4x/Plc4XEndpoint.java
b/components/camel-plc4x/src/main/java/org/apache/camel/component/plc4x/Plc4XEndpoint.java
index b4f6008ffcb..35c00c2f0d8 100644
---
a/components/camel-plc4x/src/main/java/org/apache/camel/component/plc4x/Plc4XEndpoint.java
+++
b/components/camel-plc4x/src/main/java/org/apache/camel/component/plc4x/Plc4XEndpoint.java
@@ -123,7 +123,7 @@ public class Plc4XEndpoint extends DefaultEndpoint
implements EndpointServiceLoc
public void setupConnection() throws PlcConnectionException {
try {
connection = plcDriverManager.getConnection(this.uri);
- if (!connection.isConnected()) {
+ if (!isConnected()) {
reconnectIfNeeded();
}
} catch (PlcConnectionException e) {
@@ -147,15 +147,15 @@ public class Plc4XEndpoint extends DefaultEndpoint
implements EndpointServiceLoc
* @throws PlcConnectionException If reconnect failed and auto-reconnect
is turned on
*/
public void reconnectIfNeeded() throws PlcConnectionException {
- if (connection != null && connection.isConnected()) {
+ if (isConnected()) {
LOGGER.trace("No reconnect needed, already connected");
} else if (autoReconnect && connection == null) {
connection = plcDriverManager.getConnection(uri);
LOGGER.debug("Successfully reconnected");
- } else if (autoReconnect && !connection.isConnected()) {
+ } else if (autoReconnect && !isConnected()) {
connection.connect();
// If reconnection fails without Exception, reset connection
- if (!connection.isConnected()) {
+ if (!isConnected()) {
LOGGER.debug("No connection established after connect,
resetting connection");
connection = plcDriverManager.getConnection(uri);
}
@@ -173,6 +173,13 @@ public class Plc4XEndpoint extends DefaultEndpoint
implements EndpointServiceLoc
return connection.getMetadata().isWriteSupported();
}
+ /**
+ * @return true if connection is established and still connected
+ */
+ public boolean isConnected() {
+ return connection != null && connection.isConnected();
+ }
+
@Override
public Producer createProducer() {
return new Plc4XProducer(this);
@@ -251,7 +258,7 @@ public class Plc4XEndpoint extends DefaultEndpoint
implements EndpointServiceLoc
@Override
public void doStop() throws Exception {
//Shutting down the connection when leaving the Context
- if (connection != null && connection.isConnected()) {
+ if (isConnected()) {
connection.close();
connection = null;
}
diff --git
a/components/camel-plc4x/src/main/java/org/apache/camel/component/plc4x/Plc4XProducer.java
b/components/camel-plc4x/src/main/java/org/apache/camel/component/plc4x/Plc4XProducer.java
index 2f513f70136..ac4afc54e40 100644
---
a/components/camel-plc4x/src/main/java/org/apache/camel/component/plc4x/Plc4XProducer.java
+++
b/components/camel-plc4x/src/main/java/org/apache/camel/component/plc4x/Plc4XProducer.java
@@ -49,6 +49,9 @@ public class Plc4XProducer extends DefaultAsyncProducer {
super.doStart();
try {
plc4XEndpoint.setupConnection();
+ if (plc4XEndpoint.isConnected() && !plc4XEndpoint.canWrite()) {
+ throw new PlcException("This connection (" +
plc4XEndpoint.getUri() + ") doesn't support writing.");
+ }
} catch (PlcConnectionException e) {
if (log.isTraceEnabled()) {
log.error("Connection setup failed, stopping producer", e);
@@ -57,15 +60,15 @@ public class Plc4XProducer extends DefaultAsyncProducer {
}
doStop();
}
- if (!plc4XEndpoint.canWrite()) {
- throw new PlcException("This connection (" +
plc4XEndpoint.getUri() + ") doesn't support writing.");
- }
}
@Override
public void process(Exchange exchange) throws Exception {
try {
plc4XEndpoint.reconnectIfNeeded();
+ if (plc4XEndpoint.isConnected() && !plc4XEndpoint.canWrite()) {
+ throw new PlcException("This connection (" +
plc4XEndpoint.getUri() + ") doesn't support writing.");
+ }
} catch (PlcConnectionException e) {
if (log.isTraceEnabled()) {
log.warn("Unable to reconnect, skipping request", e);