use of org.apache.eventmesh.client.http.EventMeshRetObj in project incubator-eventmesh by apache.
the class EventMeshHttpConsumer method heartBeat.
// todo: remove http heartBeat?
public void heartBeat(List<SubscriptionItem> topicList, String subscribeUrl) {
Preconditions.checkNotNull(topicList, "Subscribe item cannot be null");
Preconditions.checkNotNull(subscribeUrl, "SubscribeUrl cannot be null");
scheduler.scheduleAtFixedRate(() -> {
try {
List<HeartbeatRequestBody.HeartbeatEntity> heartbeatEntities = topicList.stream().map(subscriptionItem -> {
HeartbeatRequestBody.HeartbeatEntity heartbeatEntity = new HeartbeatRequestBody.HeartbeatEntity();
heartbeatEntity.topic = subscriptionItem.getTopic();
heartbeatEntity.url = subscribeUrl;
return heartbeatEntity;
}).collect(Collectors.toList());
RequestParam requestParam = buildCommonRequestParam().addHeader(ProtocolKey.REQUEST_CODE, RequestCode.HEARTBEAT.getRequestCode()).addBody(HeartbeatRequestBody.CLIENTTYPE, ClientType.SUB.name()).addBody(HeartbeatRequestBody.HEARTBEATENTITIES, JsonUtils.serialize(heartbeatEntities));
String target = selectEventMesh();
String res = HttpUtils.post(httpClient, target, requestParam);
EventMeshRetObj ret = JsonUtils.deserialize(res, EventMeshRetObj.class);
if (ret.getRetCode() != EventMeshRetCode.SUCCESS.getRetCode()) {
throw new EventMeshException(ret.getRetCode(), ret.getRetMsg());
}
} catch (Exception e) {
log.error("send heartBeat error", e);
}
}, EventMeshCommon.HEARTBEAT, EventMeshCommon.HEARTBEAT, TimeUnit.MILLISECONDS);
}
use of org.apache.eventmesh.client.http.EventMeshRetObj in project incubator-eventmesh by apache.
the class EventMeshHttpConsumer method subscribe.
/**
* When receive message will callback the url.
*
* @param topicList topic that be subscribed
* @param subscribeUrl url will be trigger
* @throws EventMeshException if subscribe failed
*/
public void subscribe(List<SubscriptionItem> topicList, String subscribeUrl) throws EventMeshException {
Preconditions.checkNotNull(topicList, "Subscribe item cannot be null");
Preconditions.checkNotNull(subscribeUrl, "SubscribeUrl cannot be null");
RequestParam subscribeParam = buildCommonRequestParam().addHeader(ProtocolKey.REQUEST_CODE, RequestCode.SUBSCRIBE.getRequestCode()).addBody(SubscribeRequestBody.TOPIC, JsonUtils.serialize(topicList)).addBody(SubscribeRequestBody.CONSUMERGROUP, eventMeshHttpClientConfig.getConsumerGroup()).addBody(SubscribeRequestBody.URL, subscribeUrl);
String target = selectEventMesh();
try {
String subRes = HttpUtils.post(httpClient, target, subscribeParam);
EventMeshRetObj ret = JsonUtils.deserialize(subRes, EventMeshRetObj.class);
if (ret.getRetCode() != EventMeshRetCode.SUCCESS.getRetCode()) {
throw new EventMeshException(ret.getRetCode(), ret.getRetMsg());
}
SUBSCRIPTIONS.addAll(topicList);
} catch (Exception ex) {
throw new EventMeshException(String.format("Subscribe topic error, target:%s", target), ex);
}
}
use of org.apache.eventmesh.client.http.EventMeshRetObj in project incubator-eventmesh by apache.
the class CloudEventProducer method publish.
@Override
public void publish(CloudEvent cloudEvent) throws EventMeshException {
validateCloudEvent(cloudEvent);
CloudEvent enhanceCloudEvent = enhanceCloudEvent(cloudEvent);
// todo: Can put to abstract class, all protocol use the same send method? This can be a template method
RequestParam requestParam = buildCommonPostParam(enhanceCloudEvent).addHeader(ProtocolKey.REQUEST_CODE, RequestCode.MSG_SEND_ASYNC.getRequestCode());
String target = selectEventMesh();
try {
String response = HttpUtils.post(httpClient, target, requestParam);
EventMeshRetObj ret = JsonUtils.deserialize(response, EventMeshRetObj.class);
if (ret.getRetCode() != EventMeshRetCode.SUCCESS.getRetCode()) {
throw new EventMeshException(ret.getRetCode(), ret.getRetMsg());
}
} catch (Exception exception) {
throw new EventMeshException(String.format("Publish message error, target:%s", target), exception);
}
}
use of org.apache.eventmesh.client.http.EventMeshRetObj in project incubator-eventmesh by apache.
the class CloudEventProducer method request.
@Override
public CloudEvent request(CloudEvent cloudEvent, long timeout) throws EventMeshException {
validateCloudEvent(cloudEvent);
CloudEvent enhanceCloudEvent = enhanceCloudEvent(cloudEvent);
RequestParam requestParam = buildCommonPostParam(enhanceCloudEvent).addHeader(ProtocolKey.REQUEST_CODE, RequestCode.MSG_SEND_SYNC.getRequestCode()).setTimeout(timeout);
String target = selectEventMesh();
try {
String response = HttpUtils.post(httpClient, target, requestParam);
EventMeshRetObj ret = JsonUtils.deserialize(response, EventMeshRetObj.class);
if (ret.getRetCode() == EventMeshRetCode.SUCCESS.getRetCode()) {
return transformMessage(ret);
}
throw new EventMeshException(ret.getRetCode(), ret.getRetMsg());
} catch (Exception e) {
throw new EventMeshException(String.format("Request message error, target:%s", target), e);
}
}
use of org.apache.eventmesh.client.http.EventMeshRetObj in project incubator-eventmesh by apache.
the class EventMeshMessageProducer method publish.
@Override
public void publish(EventMeshMessage message) throws EventMeshException {
validateEventMeshMessage(message);
RequestParam requestParam = buildCommonPostParam(message).addHeader(ProtocolKey.REQUEST_CODE, RequestCode.MSG_SEND_ASYNC.getRequestCode());
String target = selectEventMesh();
try {
String response = HttpUtils.post(httpClient, target, requestParam);
EventMeshRetObj ret = JsonUtils.deserialize(response, EventMeshRetObj.class);
if (ret.getRetCode() != EventMeshRetCode.SUCCESS.getRetCode()) {
throw new EventMeshException(ret.getRetCode(), ret.getRetMsg());
}
} catch (Exception exception) {
throw new EventMeshException(String.format("Publish message error, target:%s", target), exception);
}
}
Aggregations