복붙노트

[HADOOP] cumulocity의 java 클라이언트가 이벤트를 수신하는 방법은 무엇입니까?

HADOOP

cumulocity의 java 클라이언트가 이벤트를 수신하는 방법은 무엇입니까?

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

    }

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

해결법

  1. ==============================

    1.네가 cep 모듈을 만든 것 같아.

    네가 cep 모듈을 만든 것 같아.

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

    "customevent / cumulocity-system-id"채널 구독을 생성하면 해당 이벤트를 얻을 수 있습니다.

    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
            }
    
    
        });
    

    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"은 귀하가 원하는 채널입니다.

    희망이 도움이!

  2. from https://stackoverflow.com/questions/41830883/how-the-java-client-in-cumulocity-listens-to-events by cc-by-sa and MIT license