You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@plc4x.apache.org by Wolfgang Huse <wo...@nutanix.com> on 2020/05/03 13:02:51 UTC

Java Example for Subscription

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



Re: Java Example for Subscription

Posted by Wolfgang Huse <wo...@nutanix.com>.
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://192.168.178.120:53530/opcua/SimulationServer");
        PlcSubscriptionRequest.Builder builder = plcConnection.subscriptionRequestBuilder();
        builder.addCyclicField("value-1", "ns=3;s=Counter", Duration.ofMillis(1000));
        PlcSubscriptionRequest subscriptionRequest = builder.build();
        PlcSubscriptionResponse subResp = subscriptionRequest.execute().get();
        Consumer<PlcSubscriptionEvent> consumer = plcSubscriptionEvent -> System.out.println("Your Information"); 
        PlcConsumerRegistration registration = subResp.getSubscriptionHandle("value-1").register(consumer);        
        while (true) {
            TimeUnit.MILLISECONDS.sleep(50);
        }
    }
}

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


On 5/4/20, 10:40 AM, "Strljic, Matthias Milan" <ma...@isw.uni-stuttgart.de> wrote:

    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://127.0.0.1:12686/milo?discovery=false"; // 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 = subBuilder.build(); // 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

    Matthias

    ________________________________
    Von: Wolfgang Huse <wo...@nutanix.com>
    Gesendet: Sonntag, 3. Mai 2020 15:02:51
    An: dev@plc4x.apache.org
    Betreff: Java Example for Subscription

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




AW: Java Example for Subscription

Posted by "Strljic, Matthias Milan" <ma...@isw.uni-stuttgart.de>.
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://127.0.0.1:12686/milo?discovery=false"; // 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 = subBuilder.build(); // 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

Matthias

________________________________
Von: Wolfgang Huse <wo...@nutanix.com>
Gesendet: Sonntag, 3. Mai 2020 15:02:51
An: dev@plc4x.apache.org
Betreff: Java Example for Subscription

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