Dear Chris, I tried to follow your suggestions but I still have some problem.
I wrote a test application to show you the problem and ask suggestions.
Basically my app fire a couple of thread which read a field in a DB
continuously; the problem is that after some seconds one of the two
thread fire an untrappable exception and the software dies.
Here is my code:
|package it.fox; import org.apache.logging.log4j.LogManager; import
org.apache.logging.log4j.Logger; import
org.apache.plc4x.java.api.PlcConnection; import
org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager;
public class App { private static final Logger logger =
LogManager.getLogger(App.class); public static void main(String[] args)
{ try { PlcConnection plcConnection = new
PooledPlcDriverManager().getConnection("s7://192.168.1.192?controller-type=S7_1200");
if (plcConnection.getMetadata().canRead() ) { Poller01 poller01 = new
Poller01(); poller01.start(plcConnection); Poller02 poller02 = new
Poller02(); poller02.start(plcConnection); } else { logger.error("This
connection doesn't support reading."); Thread.sleep(5000); } } catch
(Exception exception) { logger.error("Error connecting to the PLC",
exception); } } } |
|package it.fox; import java.util.concurrent.TimeUnit; import
org.apache.logging.log4j.LogManager; import
org.apache.logging.log4j.Logger; import
org.apache.plc4x.java.api.PlcConnection; import
org.apache.plc4x.java.api.messages.PlcReadRequest; import
org.apache.plc4x.java.api.messages.PlcReadResponse; public class
Poller01 implements Runnable { private static final Logger logger =
LogManager.getLogger(Poller01.class); private Thread thread; private
PlcConnection plcConnection; @Override public void run() {
PlcReadRequest.Builder requestBuilder =
plcConnection.readRequestBuilder(); requestBuilder.addItem("data",
"%DB1:262.0:INT"); PlcReadRequest readRequest = requestBuilder.build();
while(true){ try { PlcReadResponse response =
readRequest.execute().get(5000, TimeUnit.MILLISECONDS); if (response !=
null){ logger.info("poller01 = {}", response.getPlcValue("data") );
Thread.sleep( (int) Math.floor(Math.random() * 100) ); } else {
logger.error("No response from PLC in reading polling variable"); break;
} } catch (Exception e) { logger.error("Poller01 Exception", e); } } }
/** * Start the thread */ public void start(PlcConnection plcConnection)
{ this.plcConnection = plcConnection; logger.info("Starting poller01
thread"); if (thread == null) { thread = new Thread(this, "poller01");
thread.start(); } } } |
|package it.fox; import java.util.concurrent.TimeUnit; import
org.apache.logging.log4j.LogManager; import
org.apache.logging.log4j.Logger; import
org.apache.plc4x.java.api.PlcConnection; import
org.apache.plc4x.java.api.messages.PlcReadRequest; import
org.apache.plc4x.java.api.messages.PlcReadResponse; public class
Poller02 implements Runnable{ private static final Logger logger =
LogManager.getLogger(Poller02.class); private Thread thread; private
PlcConnection plcConnection; @Override public void run() {
PlcReadRequest.Builder requestBuilder =
plcConnection.readRequestBuilder(); requestBuilder.addItem("data",
"%DB1:0.0:DINT"); PlcReadRequest readRequest = requestBuilder.build();
while(true){ try { PlcReadResponse response =
readRequest.execute().get(5000, TimeUnit.MILLISECONDS); if (response !=
null){ logger.info("poller02 = {}", response.getPlcValue("data") );
Thread.sleep( (int) Math.floor(Math.random() * 500) ); } else {
logger.error("No response from PLC in reading polling variable"); break;
} } catch (Exception e) { logger.error("Poller01 Exception", e); } } }
/** * Start the thread */ public void start(PlcConnection plcConnection)
{ this.plcConnection = plcConnection; logger.info("Starting poller02
thread"); if (thread == null) { thread = new Thread(this, "poller02");
thread.start(); } } } |
The exception the code throw is:
|[INFO ] 22:59:19.767 org.apache.plc4x.java.PlcDriverManager.<init>() -
Instantiating new PLC Driver Manager with class loader
jdk.internal.loader.ClassLoaders$AppClassLoader@55054057 [INFO ]
22:59:19.770 org.apache.plc4x.java.PlcDriverManager.<init>() -
Registering available drivers... [INFO ] 22:59:19.774
org.apache.plc4x.java.PlcDriverManager.<init>() - Registering driver for
Protocol s7 (Siemens S7 (Basic)) [INFO ] 22:59:19.908
org.apache.plc4x.java.transport.tcp.TcpChannelFactory.configureBootstrap()
- Configuring Bootstrap with Configuration{local-rack=1, local-slot=1,
remote-rack=0, remot-slot=0, pduSize=1024, maxAmqCaller=8,
maxAmqCallee=8, controllerType='S7_1200'} [INFO ] 22:59:20.158
it.fox.Poller01.start() - Starting poller01 thread [INFO ] 22:59:20.160
it.fox.Poller02.start() - Starting poller02 thread [INFO ] 22:59:20.244
it.fox.Poller01.run() - poller01 = 7 [INFO ] 22:59:20.286
it.fox.Poller02.run() - poller02 = 12139 [INFO ] 22:59:20.347
it.fox.Poller01.run() - poller01 = 7 [INFO ] 22:59:20.400
it.fox.Poller01.run() - poller01 = 7 [INFO ] 22:59:20.530
it.fox.Poller01.run() - poller01 = 7 [INFO ] 22:59:20.661
it.fox.Poller01.run() - poller01 = 7 [INFO ] 22:59:20.726
it.fox.Poller02.run() - poller02 = 12139 [INFO ] 22:59:20.786
it.fox.Poller01.run() - poller01 = 7 [INFO ] 22:59:20.913
it.fox.Poller01.run() - poller01 = 7 [INFO ] 22:59:21.035
it.fox.Poller02.run() - poller02 = 12139 [WARN ] 22:59:21.046
io.netty.channel.DefaultChannelPipeline.onUnhandledInboundException() -
An exceptionCaught() event was fired, and it reached at the tail of the
pipeline. It usually means the last handler in the pipeline did not
handle the exception. java.net.SocketException: Connection reset at
sun.nio.ch.SocketChannelImpl.throwConnectionReset(SocketChannelImpl.java:345)
~[?:?] SocketChannelImpl.java:345 at
sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:376) ~[?:?]
SocketChannelImpl.java:376 at
io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:253)
~[netty-buffer-4.1.47.Final.jar:4.1.47.Final] PooledByteBuf.java:253 at
io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1133)
~[netty-buffer-4.1.47.Final.jar:4.1.47.Final] AbstractByteBuf.java:1133
at
io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:350)
~[netty-transport-4.1.47.Final.jar:4.1.47.Final]
NioSocketChannel.java:350 at
io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:148)
[netty-transport-4.1.47.Final.jar:4.1.47.Final]
AbstractNioByteChannel.java:148 at
io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714)
[netty-transport-4.1.47.Final.jar:4.1.47.Final] NioEventLoop.java:714 |
or in another run:
|[INFO ] 22:58:32.997 org.apache.plc4x.java.PlcDriverManager.<init>() -
Instantiating new PLC Driver Manager with class loader
jdk.internal.loader.ClassLoaders$AppClassLoader@55054057 [INFO ]
22:58:33.000 org.apache.plc4x.java.PlcDriverManager.<init>() -
Registering available drivers... [INFO ] 22:58:33.005
org.apache.plc4x.java.PlcDriverManager.<init>() - Registering driver for
Protocol s7 (Siemens S7 (Basic)) [INFO ] 22:58:33.138
org.apache.plc4x.java.transport.tcp.TcpChannelFactory.configureBootstrap()
- Configuring Bootstrap with Configuration{local-rack=1, local-slot=1,
remote-rack=0, remot-slot=0, pduSize=1024, maxAmqCaller=8,
maxAmqCallee=8, controllerType='S7_1200'} [INFO ] 22:58:33.388
it.fox.Poller01.start() - Starting poller01 thread [INFO ] 22:58:33.389
it.fox.Poller02.start() - Starting poller02 thread [INFO ] 22:58:33.472
it.fox.Poller02.run() - poller02 = 12139 [WARN ] 22:58:33.473
io.netty.channel.DefaultChannelPipeline.onUnhandledInboundException() -
An exceptionCaught() event was fired, and it reached at the tail of the
pipeline. It usually means the last handler in the pipeline did not
handle the exception. java.net.SocketException: Connection reset at
sun.nio.ch.SocketChannelImpl.throwConnectionReset(SocketChannelImpl.java:345)
~[?:?] SocketChannelImpl.java:345 at
sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:376) ~[?:?]
SocketChannelImpl.java:376 at
io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:253)
~[netty-buffer-4.1.47.Final.jar:4.1.47.Final] PooledByteBuf.java:253 at
io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1133)
~[netty-buffer-4.1.47.Final.jar:4.1.47.Final] AbstractByteBuf.java:1133
at
io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:350)
~[netty-transport-4.1.47.Final.jar:4.1.47.Final]
NioSocketChannel.java:350 at
io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:148)
[netty-transport-4.1.47.Final.jar:4.1.47.Final]
AbstractNioByteChannel.java:148 at
io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714)
[netty-transport-4.1.47.Final.jar:4.1.47.Final] NioEventLoop.java:714 at
io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)
[netty-transport-4.1.47.Final.jar:4.1.47.Final] NioEventLoop.java:650 at
io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)
[netty-transport-4.1.47.Final.jar:4.1.47.Final] NioEventLoop.java:576 at
io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
[netty-transport-4.1.47.Final.jar:4.1.47.Final] NioEventLoop.java:493 at
io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
[netty-common-4.1.47.Final.jar:4.1.47.Final]
SingleThreadEventExecutor.java:989 at
io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
[netty-common-4.1.47.Final.jar:4.1.47.Final] ThreadExecutorMap.java:74
at
io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
[netty-common-4.1.47.Final.jar:4.1.47.Final]
FastThreadLocalRunnable.java:30 at java.lang.Thread.run(Thread.java:830)
[?:?] |
Is there some sort of race condition?
Regards,
Stefano Bossi
signature.asc
Description: OpenPGP digital signature
