2017-01-24 6 views
0

目当ては、チャネルを購読して聴くJavaクライアントを構築することです。その後、累積サーバからhadoopに到着するイベントを処理します。まず、Javaクライアントを使用してcumulocityサーバーに接続(購読)するのは困難でした。しかし、今では加入者を確保しています(コードコメントで説明されているように、値を得ることができるため)。次に、購読者は、cumulocity serverで定義したチャンネルを聞きます。しかし、私たちはこのステップを達成するのに役立つcumulocity javaドキュメントで有用な方法や何かを得ることができませんでした。 ここにコードがあります。私は資格情報とサーバーのURLを匿名化しました。cumulocityのJavaクライアントはどのようにイベントをリッスンしますか?

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

    } 

サーバへの接続を検証する整数値を取得できるため、しかし今、次のポイントは、イベントのチャンネルを聴いてそのイベントを取得する方法です。どんな助力も高く評価されます。

私はあなたがチャンネル「customevent/cumulocity-システムID」へのサブスクリプションを作成する場合は、これらのイベントを取得することができます

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

のように作成したCEPモジュールを持っている

を想定し

答えて

1

Javaコードは、ちょうど(ラムダ式を使用して)以下の行

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 
     } 


    }); 

を追加します

は "/ customevent/1191201は、" あなたの欲求channelIdであることに注意してください

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

のようなものを出力します。

希望すると便利です。

+0

いいえ、ここでのポイントは、イベントを聴いたり取得したりする方法です。 –

+0

私の編集をご覧ください。 @irfanaziz – Jorge

関連する問題