plc4x-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Wolfgang Huse <>
Subject Re: Java Example for Subscription
Date Tue, 05 May 2020 06:52:24 GMT
Sorry... Still not getting this thing running...

I don’t get this subscription event raised ☹
Prosys OPC Simulator log shows that there is a new subscription (cyclic) and I also see regular
Read Requests but nothing happens in my code ?

public class HelloPlc4x {

    private static final Logger logger = LoggerFactory.getLogger(HelloPlc4x.class);
    public static void main(String[] args) throws Exception {
        PlcConnection plcConnection = new PlcDriverManager().getConnection("opcua:tcp://");
        PlcSubscriptionRequest.Builder builder = plcConnection.subscriptionRequestBuilder();
        builder.addCyclicField("value-1", "ns=3;s=Counter", Duration.ofMillis(1000));
        PlcSubscriptionRequest subscriptionRequest =;
        PlcSubscriptionResponse subResp = subscriptionRequest.execute().get();
        Consumer<PlcSubscriptionEvent> consumer = plcSubscriptionEvent -> System.out.println("Your
        PlcConsumerRegistration registration = subResp.getSubscriptionHandle("value-1").register(consumer);
        while (true) {

Mit freundlichen Grüßen – With kind regards
Wolfgang Huse

On 5/4/20, 10:40 AM, "Strljic, Matthias Milan" <>

    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
    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

View raw message