【问题标题】:How the java client in cumulocity listens to events?cumulocity中的java客户端如何监听事件?
【发布时间】:2017-01-24 14:36:20
【问题描述】:

目的是构建一个订阅和收听频道的java客户端。然后将来自 cumulocity 服务器的到达事件处理到 hadoop。首先,使用 java 客户端很难连接(订阅)cumulocity 服务器。但是,现在我们已经有了订阅者(因为我们能够获得一些价值,如代码 cmets 中所述)。接下来我们想要订阅者收听我们在 cumulocity 服务器中定义的频道。但是我们无法在 cumulocity java 文档中获得任何有助于实现此步骤的方法或任何有用的东西。 这是代码。我已将凭据和服务器 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.                       
        }

    }

因为我们能够获得一些整数值来验证与服务器的连接。但是现在下一点是如何监听事件的通道并获取这些事件。任何帮助将不胜感激。

【问题讨论】:

    标签: hadoop cometd cumulocity bayeux


    【解决方案1】:

    我想你有一个像

    这样创建的 cep 模块
    insert into
      SendNotification
    select
      e.event as payload,
      "customevent/" || e.event.source.value as channelName
    from
      EventCreated e;
    

    如果您订阅频道“customevent/cumulocity-system-id”,您将能够获取这些事件

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

    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。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      • 2019-03-22
      相关资源
      最近更新 更多