Search in sources :

Example 1 with EventMeshException

use of org.apache.eventmesh.common.exception.EventMeshException in project incubator-eventmesh by apache.

the class AbstractHttpClient method setHttpClient.

private CloseableHttpClient setHttpClient() throws EventMeshException {
    if (!eventMeshHttpClientConfig.isUseTls()) {
        return HttpClients.createDefault();
    }
    SSLContext sslContext;
    try {
        // todo: config in properties file?
        String protocol = System.getProperty("ssl.client.protocol", "TLSv1.2");
        TrustManager[] tm = new TrustManager[] { new MyX509TrustManager() };
        sslContext = SSLContext.getInstance(protocol);
        sslContext.init(null, tm, new SecureRandom());
        // todo: custom client pool
        return HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(new DefaultHostnameVerifier()).build();
    } catch (Exception e) {
        log.error("Error in creating HttpClient.", e);
        throw new EventMeshException(e);
    }
}
Also used : DefaultHostnameVerifier(org.apache.http.conn.ssl.DefaultHostnameVerifier) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) EventMeshException(org.apache.eventmesh.common.exception.EventMeshException) IOException(java.io.IOException) EventMeshException(org.apache.eventmesh.common.exception.EventMeshException) TrustManager(javax.net.ssl.TrustManager) MyX509TrustManager(org.apache.eventmesh.client.http.ssl.MyX509TrustManager) MyX509TrustManager(org.apache.eventmesh.client.http.ssl.MyX509TrustManager)

Example 2 with EventMeshException

use of org.apache.eventmesh.common.exception.EventMeshException 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 3 with EventMeshException

use of org.apache.eventmesh.common.exception.EventMeshException 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 4 with EventMeshException

use of org.apache.eventmesh.common.exception.EventMeshException 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 5 with EventMeshException

use of org.apache.eventmesh.common.exception.EventMeshException 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)

Aggregations

EventMeshException (org.apache.eventmesh.common.exception.EventMeshException)37 Package (org.apache.eventmesh.common.protocol.tcp.Package)16 RequestParam (org.apache.eventmesh.client.http.model.RequestParam)12 IOException (java.io.IOException)10 EventMeshRetObj (org.apache.eventmesh.client.http.EventMeshRetObj)10 SubscriptionItem (org.apache.eventmesh.common.protocol.SubscriptionItem)5 CloudEvent (io.cloudevents.CloudEvent)3 Preconditions (com.google.common.base.Preconditions)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 Message (io.openmessaging.api.Message)1 SecureRandom (java.security.SecureRandom)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