How the java client in cumulocity listens to events?

独自空忆成欢 提交于 2019-12-02 08:46:18

I supposed you have a cep module created like

insert into
  SendNotification
select
  e.event as payload,
  "customevent/" || e.event.source.value as channelName
from
  EventCreated e;

you will be able to get those events if you create a subscription to the channel "customevent/cumulocity-system-id"

In your Java code just add the lines below (Using a lambda expression)

t.subscribe("/customevent/1191201", new  SubscriptionListener (){

        @Override
        public void onNotification(Subscription s, Object r) {
            // here come the notification of the desired channel. The Object r is the event desired.
            System.out.println(r);

        }

        @Override
        public void onError(Subscription s, Throwable thrwbl) {
            // errors will come here
        }


    });

and

System.out.println(r);

will print something like

{creationTime=2017-01-26T19:00:15.837+01:00, c8y_Position=Position 
[lat=4, lng=-71.80, alt=67, accuracy=null],    
self=http://yourTenant.cumulocity.com/event/events/1202018, 
time=2017-01-    26T13:00:15.000-05:00, id=1202018, source={name=Lancer 
UBL142,
self=http://yourTenant.cumulocity.com/inventory/managedObjects/1191201, 
id=1191201}, text=Estado,Idle mode (Parking), type=c8y_LocationUpdate}

Please note that "/customevent/1191201" is your desire channelId.

Hope this helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!