Search in sources :

Example 1 with EventMeshRecommendStrategy

use of org.apache.eventmesh.runtime.core.protocol.tcp.client.recommend.EventMeshRecommendStrategy in project incubator-eventmesh by apache.

the class RecommendTask method run.

@Override
public void run() {
    long taskExecuteTime = System.currentTimeMillis();
    Package res = new Package();
    try {
        if (!eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshServerRegistryEnable) {
            throw new Exception("registry enable config is false, not support");
        }
        UserAgent user = (UserAgent) pkg.getBody();
        validateUserAgent(user);
        String group = getGroupOfClient(user);
        EventMeshRecommendStrategy eventMeshRecommendStrategy = new EventMeshRecommendImpl(eventMeshTCPServer);
        String eventMeshRecommendResult = eventMeshRecommendStrategy.calculateRecommendEventMesh(group, user.getPurpose());
        res.setHeader(new Header(RECOMMEND_RESPONSE, OPStatus.SUCCESS.getCode(), OPStatus.SUCCESS.getDesc(), pkg.getHeader().getSeq()));
        res.setBody(eventMeshRecommendResult);
    } catch (Exception e) {
        messageLogger.error("RecommendTask failed|address={}|errMsg={}", ctx.channel().remoteAddress(), e);
        res.setHeader(new Header(RECOMMEND_RESPONSE, OPStatus.FAIL.getCode(), e.toString(), pkg.getHeader().getSeq()));
    } finally {
        writeAndFlush(res, startTime, taskExecuteTime, session.getContext(), session);
    // session.write2Client(res);
    }
}
Also used : EventMeshRecommendStrategy(org.apache.eventmesh.runtime.core.protocol.tcp.client.recommend.EventMeshRecommendStrategy) Header(org.apache.eventmesh.common.protocol.tcp.Header) UserAgent(org.apache.eventmesh.common.protocol.tcp.UserAgent) Package(org.apache.eventmesh.common.protocol.tcp.Package) EventMeshRecommendImpl(org.apache.eventmesh.runtime.core.protocol.tcp.client.recommend.EventMeshRecommendImpl)

Example 2 with EventMeshRecommendStrategy

use of org.apache.eventmesh.runtime.core.protocol.tcp.client.recommend.EventMeshRecommendStrategy in project incubator-eventmesh by apache.

the class QueryRecommendEventMeshHandler method handle.

@Override
public void handle(HttpExchange httpExchange) throws IOException {
    String result = "";
    OutputStream out = httpExchange.getResponseBody();
    try {
        if (!eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshServerRegistryEnable) {
            throw new Exception("registry enable config is false, not support");
        }
        String queryString = httpExchange.getRequestURI().getQuery();
        Map<String, String> queryStringInfo = NetUtils.formData2Dic(queryString);
        String group = queryStringInfo.get(EventMeshConstants.MANAGE_GROUP);
        String purpose = queryStringInfo.get(EventMeshConstants.MANAGE_PURPOSE);
        if (StringUtils.isBlank(group) || StringUtils.isBlank(purpose)) {
            httpExchange.sendResponseHeaders(200, 0);
            result = "params illegal!";
            out.write(result.getBytes());
            return;
        }
        EventMeshRecommendStrategy eventMeshRecommendStrategy = new EventMeshRecommendImpl(eventMeshTCPServer);
        String recommendEventMeshResult = eventMeshRecommendStrategy.calculateRecommendEventMesh(group, purpose);
        result = (recommendEventMeshResult == null) ? "null" : recommendEventMeshResult;
        logger.info("recommend eventmesh:{},group:{},purpose:{}", result, group, purpose);
        httpExchange.sendResponseHeaders(200, 0);
        out.write(result.getBytes());
    } catch (Exception e) {
        logger.error("QueryRecommendEventMeshHandler fail...", e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                logger.warn("out close failed...", e);
            }
        }
    }
}
Also used : EventMeshRecommendStrategy(org.apache.eventmesh.runtime.core.protocol.tcp.client.recommend.EventMeshRecommendStrategy) OutputStream(java.io.OutputStream) EventMeshRecommendImpl(org.apache.eventmesh.runtime.core.protocol.tcp.client.recommend.EventMeshRecommendImpl) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

EventMeshRecommendImpl (org.apache.eventmesh.runtime.core.protocol.tcp.client.recommend.EventMeshRecommendImpl)2 EventMeshRecommendStrategy (org.apache.eventmesh.runtime.core.protocol.tcp.client.recommend.EventMeshRecommendStrategy)2 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Header (org.apache.eventmesh.common.protocol.tcp.Header)1 Package (org.apache.eventmesh.common.protocol.tcp.Package)1 UserAgent (org.apache.eventmesh.common.protocol.tcp.UserAgent)1