Java客户端如何累积事件? [英] How the java client in cumulocity listens to events?

查看:97
本文介绍了Java客户端如何累积事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是构建一个订阅并收听频道的Java客户端。然后处理从累积服务器到Hadoop的到达事件。首先,很难使用Java客户端连接(订阅)到累积服务器。但是,现在我们有了订阅者(如代码注释中所述,因为我们能够获得一些价值)。接下来我们想要的是订户收听我们在累积服务器中定义的频道。但是,在累积性Java文档中,我们无法获得任何方法或任何有用的方法来帮助完成此步骤。
这是代码。我已经匿名化了凭据和服务器URL。

Aim is to build a java client that subscribe and listen to a channel. Then process the arriving events from the cumulocity server to hadoop. First it was difficult to connect(subscribe) to the cumulocity server using the java client. However, now we have subscriber in place( as we are able to get some value against it as explained in the code comments). Next we want is the subscriber listen to the channel that we have defined in cumulocity server. But we could not get any method or anything useful in the cumulocity java docs that will help to achieve this step. Here is the code. I have anonymized the credentials and the server url.

        package c8y.example.hello_agent;

    import c8y.IsDevice;
    import com.cumulocity.model.authentication.CumulocityCredentials;
    import com.cumulocity.rest.representation.inventory.ManagedObjectRepresentation;
    import com.cumulocity.sdk.client.Platform;
    import com.cumulocity.sdk.client.PlatformImpl;
    import com.cumulocity.sdk.client.inventory.InventoryApi;

    import com.cumulocity.sdk.client.PlatformParameters;
    import com.cumulocity.sdk.client.SDKException;
    import com.cumulocity.sdk.client.notification.*;
    import com.cumulocity.sdk.client.ClientConfiguration;
    import com.cumulocity.*;

    import c8y.example.hello_agent.cred;

    public class CepCustomNotificationsSubscriber implements Subscriber<String, Object> {

        public static final String CEP_CUSTOM_NOTIFICATIONS_URL = "test/sendTemperature";

        private final Subscriber<String, Object> subscriber;

        public CepCustomNotificationsSubscriber(PlatformParameters parameters) {
            subscriber = createSubscriber(parameters);
        }

        private Subscriber<String, Object> createSubscriber(PlatformParameters parameters) {
            // @formatter:off
            return SubscriberBuilder.<String, Object>anSubscriber()
                        .withParameters(parameters)
                        .withEndpoint(CEP_CUSTOM_NOTIFICATIONS_URL)
                        .withSubscriptionNameResolver(new Identity())
                        .withDataType(Object.class)
                        .build();
            // @formatter:on
        }



        public Subscription<String> subscribe(final String channelID, final SubscriptionListener<String, Object> handler) throws SDKException {
            return subscriber.subscribe(channelID, handler);
        }

        public void disconnect() {
            subscriber.disconnect();
        }

        private static final class Identity implements SubscriptionNameResolver<String> {
            @Override
            public String apply(String id) {
                return id;
            }
        }

        public static void main( String[] args )
        {
           cred crede = new cred(); 
           String uRl = "https://xxx.cumulocityiox.com"; 
           CumulocityCredentials rC = new CumulocityCredentials(crede.name,crede.pass);
           PlatformParameters parameters = new PlatformParameters(uRl,rC, new ClientConfiguration());
           CepCustomNotificationsSubscriber t = new CepCustomNotificationsSubscriber(parameters);


           System.out.println(t.toString() + " - " + t.CEP_CUSTOM_NOTIFICATIONS_URL.toString()); // It prints an integer number corresponding to the subscriber t.
// Now how to listen to the events on the channel and get the desired data.                       
        }

    }

因为我们有能力获得一些整数值,用于验证与服务器的连接。但是现在,下一点是如何监听频道中的事件并获取这些事件。

Since we are able to get some integer value which validate the connectivity to the server. But Now the next point is how to listen to the channel for events and get those events. Any help will be highly appreciated.

推荐答案

我认为您已经创建了一个

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;

如果您创建频道 customevent / cumulocity- system-id

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

在您的Java代码中,只需添加以下行(使用lambda表达式)

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
        }


    });

System.out.println(r);

将打印类似

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

请注意, / customevent / 1191201是您想要的channelId。

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

希望这会有所帮助!

这篇关于Java客户端如何累积事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆