Цель состоит в том, чтобы создать java-клиент, который подписывается и слушает канал. Затем обработайте поступающие события с сервера cumulocity до hadoop. Сначала было сложно подключиться (подписаться) к серверу cumulocity с помощью java-клиента. Однако теперь у нас есть абонент (поскольку мы можем получить некоторое значение против него, как объяснено в комментариях к коду). Затем мы хотим, чтобы абонент прослушал канал, который мы определили на сервере кумулятивности. Но мы не могли получить какой-либо метод или что-нибудь полезное в документах jum cumulocity, которые помогут достичь этого шага. Вот код. Я анонимизировал учетные данные и серверный URL.Как клиент 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.
}
}
Поскольку мы можем получить целочисленное значение, которое подтверждает возможность подключения к серверу. Но теперь следующий вопрос - как прослушать канал для событий и получить эти события. Любая помощь будет высоко оценена.
Нет, дело здесь в том, как слушать или получать события. –
См. Мое редактирование. @irfanaziz – Jorge