plc4x-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Strljic, Matthias Milan" <>
Subject AW: Java Example for Subscription
Date Mon, 04 May 2020 08:39:52 GMT
Hi Wolfgang,

i added a small example to the ManualPLC4XOpcua file which will be included into the up comming
OPC docu.

It is stripped down to the functions which are required to  create a subscription and does
not include a further embedding into an application.

I hope it helps you!

String fieldAddress = "ns=2;s=HelloWorld/ScalarTypes/String"; // String in opc ua format to
describe target value
String fieldName = "field1"; // local unique key for the requested variable
String connectionString = "opcua:tcp://"; // Address of
the opc ua server

PlcConnection opcuaSubConnection =  new PlcDriverManager().getConnection(connectionString);
// build connection
PlcSubscriptionRequest.Builder subBuilder =  opcuaSubConnection.subscriptionRequestBuilder();
// get builder
subBuilder.addChangeOfStateField(fieldName, fieldAddress); // add the tuple of fieldName and
Address to the request
PlcSubscriptionRequest subReq =; // build the request
PlcSubscriptionResponse subResp = subReq.execute().get(); // execute the build up of a subscription
Consumer<PlcSubscriptionEvent> consumer = plcSubscriptionEvent -> System.out.println("Your
Information"); // create a consumer function for the subscription
PlcConsumerRegistration registration = subResp.getSubscriptionHandle(fieldName).register(consumer);
// add the consumer to the created subscription  of the request and access it over the  SubscriptionHandler
registration.unregister(); // Unsubscribe
// You done :)

Beste regards


Von: Wolfgang Huse <>
Gesendet: Sonntag, 3. Mai 2020 15:02:51
Betreff: Java Example for Subscription

could anybody provide me a small example how to use Subscription?
I am able to subscribe as example to a opcua field but I have difficulties to create the callback-function
which is triggered when new values arrive.

I had a look at different examples but most of them use synchronous communication or async
read but not Subscriptions.

Mit freundlichen Grüßen – With kind regards

Wolfgang Huse

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message