2017-01-24 1 views
0

목표는 구독하고 채널을 청취하는 Java 클라이언트를 빌드하는 것입니다. 그런 다음 cumulocity 서버에서 hadoop으로 도착 이벤트를 처리합니다. 먼저 자바 클라이언트를 사용하여 cumulocity 서버에 연결 (구독)하기가 어려웠습니다. 그러나 이제 우리는 코드 주석에서 설명한대로 가입자에 대한 가치를 얻을 수 있기 때문에 가입자를 보유하게되었습니다. 다음은 가입자가 cumulocity 서버에서 정의한 채널을 청취하는 것입니다. 그러나 우리는이 단계를 달성하는 데 도움이되는 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.      
     } 

    } 

우리는 서버에 대한 연결을 확인하는 정수 값을 얻을 수 있기 때문에. 하지만 다음은 이벤트 채널을 듣고 이벤트를받는 방법입니다. 어떤 도움을 주시면 감사하겠습니다.

답변

1

난 당신이

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

처럼 만든 CEP 모듈 당신이 채널에 "맞춤 이벤트/cumulocity 시스템-ID가"

에서를 구독을 만들 경우 해당 이벤트를 얻을 수있을 것입니다이되어 자바 코드는 (람다 식을 사용) 아래 라인

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 
     } 


    }); 

01,239 추가 69,891,

은 "/ 맞춤 이벤트/1191201은"당신의 욕망 채널 ID가 있습니다

{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

관련 문제