Hadoop cumulocity中的java客户端如何侦听事件?
目的是构建一个订阅和收听频道的java客户机。然后将到达的事件从cumulocity服务器处理到hadoop。首先,使用java客户端很难连接(订阅)cumulocity服务器。然而,现在我们有了订户(正如代码注释中解释的那样,我们能够从订户中获得一些价值)。接下来我们需要的是订户收听我们在cumulocity server中定义的频道。但是我们无法在cumulocity java文档中获得任何有助于实现这一步骤的方法或任何有用的东西。 这是代码。我已将凭据和服务器url匿名化Hadoop cumulocity中的java客户端如何侦听事件?,hadoop,cometd,cumulocity,bayeux,Hadoop,Cometd,Cumulocity,Bayeux,目的是构建一个订阅和收听频道的java客户机。然后将到达的事件从cumulocity服务器处理到hadoop。首先,使用java客户端很难连接(订阅)cumulocity服务器。然而,现在我们有了订户(正如代码注释中解释的那样,我们能够从订户中获得一些价值)。接下来我们需要的是订户收听我们在cumulocity server中定义的频道。但是我们无法在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.
}
}
包c8y.example.hello\u代理;
进口c8y.IsDevice;
导入com.cumulocity.model.authentication.cumulocity redentials;
导入com.cumulocity.rest.representation.inventory.ManagedObjectRepresentation;
导入com.cumulocity.sdk.client.Platform;
导入com.cumulocity.sdk.client.PlatformImpl;
导入com.cumulocity.sdk.client.inventory.InventoryApi;
导入com.cumulocity.sdk.client.PlatformParameters;
导入com.cumulocity.sdk.client.SDKException;
导入com.cumulocity.sdk.client.notification.*;
导入com.cumulocity.sdk.client.ClientConfiguration;
进口积云度*;
导入c8y.example.hello_agent.cred;
公共类CepCustomNotificationsSubscriber实现订阅服务器{
公共静态最终字符串CEP_CUSTOM_NOTIFICATIONS_URL=“test/sendtime”;
专用最终用户;
公共CEPSCustomNotificationsSubscriber(平台参数){
subscriber=createSubscriber(参数);
}
专用订户createSubscriber(平台参数){
//@formatter:off
返回SubscriberBuilder.anSubscriber()
.带参数(参数)
.withEndpoint(CEP\u自定义\u通知\u URL)
.withSubscriptionNameResolver(新标识())
.withDataType(Object.class)
.build();
//@formatter:on
}
公共订阅订阅(最终字符串channelID,最终订阅Listener处理程序)抛出SDKEException{
返回subscriber.subscribe(channelID,handler);
}
公共空间断开连接(){
subscriber.disconnect();
}
私有静态最终类标识实现SubscriptionNameResolver{
@凌驾
公共字符串应用(字符串id){
返回id;
}
}
公共静态void main(字符串[]args)
{
cred crede=新cred();
字符串uRl=”https://xxx.cumulocityiox.com";
积云积云率rC=新积云率rC(crede.name,crede.pass);
PlatformParameters=new PlatformParameters(uRl、rC、new ClientConfiguration());
CepCustomNotificationsSubscriber t=新的CepCustomNotificationsSubscriber(参数);
System.out.println(t.toString()+“-”+t.CEP_CUSTOM_NOTIFICATIONS_URL.toString());//它打印与订户t对应的整数。
//现在,如何收听频道上的事件并获得所需的数据。
}
}
因为我们能够得到一些整数值来验证与服务器的连接。但现在下一点是如何收听频道中的事件并获取这些事件。任何帮助都将不胜感激。我想您已经创建了一个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表达式)
及
将打印类似于
{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
希望这有帮助
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}