Search in sources :

Example 1 with EventMeshRetObj

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);
}
Also used : RequestParam(org.apache.eventmesh.client.http.model.RequestParam) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ClientType(org.apache.eventmesh.common.protocol.http.common.ClientType) AbstractHttpClient(org.apache.eventmesh.client.http.AbstractHttpClient) EventMeshRetCode(org.apache.eventmesh.common.protocol.http.common.EventMeshRetCode) Constants(org.apache.eventmesh.common.Constants) ArrayList(java.util.ArrayList) HttpUtils(org.apache.eventmesh.client.http.util.HttpUtils) EventMeshCommon(org.apache.eventmesh.client.tcp.common.EventMeshCommon) EventMeshException(org.apache.eventmesh.common.exception.EventMeshException) EventMeshRetObj(org.apache.eventmesh.client.http.EventMeshRetObj) UnSubscribeRequestBody(org.apache.eventmesh.common.protocol.http.body.client.UnSubscribeRequestBody) HeartbeatRequestBody(org.apache.eventmesh.common.protocol.http.body.client.HeartbeatRequestBody) ProtocolKey(org.apache.eventmesh.common.protocol.http.common.ProtocolKey) HttpMethod(io.netty.handler.codec.http.HttpMethod) SubscriptionItem(org.apache.eventmesh.common.protocol.SubscriptionItem) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Collectors(java.util.stream.Collectors) ThreadPoolFactory(org.apache.eventmesh.common.ThreadPoolFactory) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) EventMeshHttpClientConfig(org.apache.eventmesh.client.http.conf.EventMeshHttpClientConfig) RequestCode(org.apache.eventmesh.common.protocol.http.common.RequestCode) SubscribeRequestBody(org.apache.eventmesh.common.protocol.http.body.client.SubscribeRequestBody) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) ProtocolVersion(org.apache.eventmesh.common.protocol.http.common.ProtocolVersion) JsonUtils(org.apache.eventmesh.common.utils.JsonUtils) Collections(java.util.Collections) RequestParam(org.apache.eventmesh.client.http.model.RequestParam) EventMeshRetObj(org.apache.eventmesh.client.http.EventMeshRetObj) HeartbeatRequestBody(org.apache.eventmesh.common.protocol.http.body.client.HeartbeatRequestBody) EventMeshException(org.apache.eventmesh.common.exception.EventMeshException) EventMeshException(org.apache.eventmesh.common.exception.EventMeshException)

Example 2 with EventMeshRetObj

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);
    }
}
Also used : RequestParam(org.apache.eventmesh.client.http.model.RequestParam) EventMeshRetObj(org.apache.eventmesh.client.http.EventMeshRetObj) EventMeshException(org.apache.eventmesh.common.exception.EventMeshException) EventMeshException(org.apache.eventmesh.common.exception.EventMeshException)

Example 3 with EventMeshRetObj

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);
    }
}
Also used : RequestParam(org.apache.eventmesh.client.http.model.RequestParam) EventMeshRetObj(org.apache.eventmesh.client.http.EventMeshRetObj) EventMeshException(org.apache.eventmesh.common.exception.EventMeshException) CloudEvent(io.cloudevents.CloudEvent) IOException(java.io.IOException) EventMeshException(org.apache.eventmesh.common.exception.EventMeshException)

Example 4 with EventMeshRetObj

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);
    }
}
Also used : RequestParam(org.apache.eventmesh.client.http.model.RequestParam) EventMeshRetObj(org.apache.eventmesh.client.http.EventMeshRetObj) EventMeshException(org.apache.eventmesh.common.exception.EventMeshException) CloudEvent(io.cloudevents.CloudEvent) IOException(java.io.IOException) EventMeshException(org.apache.eventmesh.common.exception.EventMeshException)

Example 5 with EventMeshRetObj

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);
    }
}
Also used : RequestParam(org.apache.eventmesh.client.http.model.RequestParam) EventMeshRetObj(org.apache.eventmesh.client.http.EventMeshRetObj) EventMeshException(org.apache.eventmesh.common.exception.EventMeshException) IOException(java.io.IOException) EventMeshException(org.apache.eventmesh.common.exception.EventMeshException)

Aggregations

EventMeshRetObj (org.apache.eventmesh.client.http.EventMeshRetObj)10 EventMeshException (org.apache.eventmesh.common.exception.EventMeshException)10 RequestParam (org.apache.eventmesh.client.http.model.RequestParam)9 IOException (java.io.IOException)6 CloudEvent (io.cloudevents.CloudEvent)2 Preconditions (com.google.common.base.Preconditions)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 Slf4j (lombok.extern.slf4j.Slf4j)1 AbstractHttpClient (org.apache.eventmesh.client.http.AbstractHttpClient)1 EventMeshHttpClientConfig (org.apache.eventmesh.client.http.conf.EventMeshHttpClientConfig)1 HttpUtils (org.apache.eventmesh.client.http.util.HttpUtils)1