Search in sources :

Example 41 with Message

use of io.vertx.core.eventbus.Message in project chili-core by codingchili.

the class BusRouterTest method testTimeout.

@Test
public void testTimeout(TestContext test) {
    Async async = test.async();
    mockNode(NODE_1, (message) -> {
    });
    handle(NODE_1, ((response, status) -> {
        test.assertEquals(ResponseStatus.ERROR, status);
        test.assertTrue(response.getString(PROTOCOL_MESSAGE).contains(system().getClusterTimeout() + ""));
        async.complete();
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Message(io.vertx.core.eventbus.Message) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Timeout(io.vertx.ext.unit.junit.Timeout) Test(org.junit.Test) CoreStrings(com.codingchili.core.configuration.CoreStrings) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) UUID(java.util.UUID) ResponseStatus(com.codingchili.core.protocol.ResponseStatus) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) ResponseListener(com.codingchili.core.testing.ResponseListener) RequestMock(com.codingchili.core.testing.RequestMock) After(org.junit.After) JsonObject(io.vertx.core.json.JsonObject) ACCEPTED(com.codingchili.core.protocol.ResponseStatus.ACCEPTED) SystemContext(com.codingchili.core.context.SystemContext) Handler(io.vertx.core.Handler) Configurations.system(com.codingchili.core.files.Configurations.system) CoreContext(com.codingchili.core.context.CoreContext) Before(org.junit.Before) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 42 with Message

use of io.vertx.core.eventbus.Message in project chili-core by codingchili.

the class BusRouterTest method testRouteNodeTimeoutLogged.

@Test
public void testRouteNodeTimeoutLogged(TestContext test) {
    Async async = test.async();
    String node = UUID.randomUUID().toString();
    mockNode(node, message -> {
    });
    handle(node, ((response, status) -> {
        test.assertTrue(nodeTimeout.get());
        async.complete();
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Message(io.vertx.core.eventbus.Message) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Timeout(io.vertx.ext.unit.junit.Timeout) Test(org.junit.Test) CoreStrings(com.codingchili.core.configuration.CoreStrings) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) UUID(java.util.UUID) ResponseStatus(com.codingchili.core.protocol.ResponseStatus) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) ResponseListener(com.codingchili.core.testing.ResponseListener) RequestMock(com.codingchili.core.testing.RequestMock) After(org.junit.After) JsonObject(io.vertx.core.json.JsonObject) ACCEPTED(com.codingchili.core.protocol.ResponseStatus.ACCEPTED) SystemContext(com.codingchili.core.context.SystemContext) Handler(io.vertx.core.Handler) Configurations.system(com.codingchili.core.files.Configurations.system) CoreContext(com.codingchili.core.context.CoreContext) Before(org.junit.Before) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 43 with Message

use of io.vertx.core.eventbus.Message in project VX-API-Gateway by EliMirren.

the class VxApiApplication method addRoute.

/**
 * 添加一个路由
 *
 * @param msg
 */
public void addRoute(Message<JsonObject> msg) {
    JsonObject body = msg.body().getJsonObject("api");
    VxApisDTO dto = VxApisDTO.fromJson(body);
    if (dto != null) {
        VxApis api = new VxApis(dto);
        // 是否代理启动API到当前应用
        boolean otherRouteAdd = msg.body().getBoolean("elseRouteToThis", false);
        if (otherRouteAdd) {
            // 服务器的类型1=http,2=https,3=webSocket
            int type = msg.body().getInteger("serverType", 0);
            if (type == 1) {
                addHttpRouter(api, res -> {
                    if (res.succeeded()) {
                        msg.reply(1);
                    } else {
                        msg.fail(500, res.cause().getMessage());
                    }
                });
            } else if (type == 2) {
                addHttpsRouter(api, res -> {
                    if (res.succeeded()) {
                        msg.reply(1);
                    } else {
                        msg.fail(500, res.cause().getMessage());
                        res.cause().printStackTrace();
                    }
                });
            } else {
                msg.fail(500, "不存在的服务");
            }
        } else {
            // 本应用添加API,既属于自己的网关应用添加API
            if (httpRouter != null && httpsRouter != null) {
                Future<Boolean> httpFuture = Future.future(http -> addHttpRouter(api, http));
                Future<Boolean> httpsFuture = Future.<Boolean>future(https -> addHttpsRouter(api, https));
                CompositeFuture.all(httpFuture, httpsFuture).setHandler(res -> {
                    if (res.succeeded()) {
                        msg.reply(1);
                    } else {
                        msg.fail(500, res.cause().getMessage());
                    }
                });
            } else if (httpRouter != null) {
                addHttpRouter(api, res -> {
                    if (res.succeeded()) {
                        msg.reply(1);
                    } else {
                        msg.fail(500, res.cause().getMessage());
                    }
                });
            } else if (httpsRouter != null) {
                addHttpsRouter(api, res -> {
                    if (res.succeeded()) {
                        msg.reply(1);
                    } else {
                        msg.fail(500, res.cause().getMessage());
                    }
                });
            } else {
                msg.fail(404, "找不到的服务器可以加载API");
            }
        }
    } else {
        msg.fail(1400, "API参数不能为null,请检查APIDTO需要实例化的JSON编写是否正确");
    }
}
Also used : VxApis(com.szmirren.vxApi.core.entity.VxApis) VxApiServerOptions(com.szmirren.vxApi.core.options.VxApiServerOptions) VxApiBeforeHandler(com.szmirren.vxApi.spi.handler.VxApiBeforeHandler) HttpMethodEnum(com.szmirren.vxApi.core.enums.HttpMethodEnum) Router(io.vertx.ext.web.Router) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) VxApiEventBusAddressConstant(com.szmirren.vxApi.core.common.VxApiEventBusAddressConstant) ApiServerTypeEnum(com.szmirren.vxApi.core.enums.ApiServerTypeEnum) VxApiAuthFactory(com.szmirren.vxApi.spi.auth.VxApiAuthFactory) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) Map(java.util.Map) VxApiAfterHandler(com.szmirren.vxApi.spi.handler.VxApiAfterHandler) VxApiAfterHandlerFactory(com.szmirren.vxApi.spi.handler.VxApiAfterHandlerFactory) JsonObject(io.vertx.core.json.JsonObject) CookieHandler(io.vertx.ext.web.handler.CookieHandler) VxApiApplicationOptions(com.szmirren.vxApi.core.options.VxApiApplicationOptions) ClusteredSessionStore(io.vertx.ext.web.sstore.ClusteredSessionStore) VxApiCustomHandler(com.szmirren.vxApi.spi.customHandler.VxApiCustomHandler) Set(java.util.Set) Message(io.vertx.core.eventbus.Message) VxApiTrackInfos(com.szmirren.vxApi.core.entity.VxApiTrackInfos) Future(io.vertx.core.Future) VxApisDTO(com.szmirren.vxApi.core.options.VxApisDTO) VxApis(com.szmirren.vxApi.core.entity.VxApis) List(java.util.List) VxApiGatewayAttribute(com.szmirren.vxApi.core.common.VxApiGatewayAttribute) VxApiAuth(com.szmirren.vxApi.spi.auth.VxApiAuth) Logger(org.apache.logging.log4j.Logger) VxApiAfterHandlerOptions(com.szmirren.vxApi.spi.handler.VxApiAfterHandlerOptions) StrUtil(com.szmirren.vxApi.core.common.StrUtil) SessionStore(io.vertx.ext.web.sstore.SessionStore) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) VxApiApplicationDTO(com.szmirren.vxApi.core.options.VxApiApplicationDTO) VxApiCustomHandlerOptions(com.szmirren.vxApi.spi.customHandler.VxApiCustomHandlerOptions) HttpClient(io.vertx.core.http.HttpClient) VxApiCustomHandlerFactory(com.szmirren.vxApi.spi.customHandler.VxApiCustomHandlerFactory) VxApiRouteHandlerApiLimit(com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerApiLimit) LocalSessionStore(io.vertx.ext.web.sstore.LocalSessionStore) VxApiRouteHandlerRedirectType(com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerRedirectType) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) PfxOptions(io.vertx.core.net.PfxOptions) LinkedHashMap(java.util.LinkedHashMap) CompositeFuture(io.vertx.core.CompositeFuture) VxApiBeforeHandlerOptions(com.szmirren.vxApi.spi.handler.VxApiBeforeHandlerOptions) SessionHandler(io.vertx.ext.web.handler.SessionHandler) VxApiRouteHandlerParamCheck(com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerParamCheck) VxApiCertOptions(com.szmirren.vxApi.core.options.VxApiCertOptions) AsyncResult(io.vertx.core.AsyncResult) LinkedHashSet(java.util.LinkedHashSet) Route(io.vertx.ext.web.Route) VxApiCorsOptions(com.szmirren.vxApi.core.options.VxApiCorsOptions) MalformedURLException(java.net.MalformedURLException) VxApiAuthOptions(com.szmirren.vxApi.spi.auth.VxApiAuthOptions) JsonArray(io.vertx.core.json.JsonArray) VxApiBeforeHandlerFactory(com.szmirren.vxApi.spi.handler.VxApiBeforeHandlerFactory) HttpMethod(io.vertx.core.http.HttpMethod) VxApiRouteHandlerHttpService(com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerHttpService) Handler(io.vertx.core.Handler) LogManager(org.apache.logging.log4j.LogManager) CorsHandler(io.vertx.ext.web.handler.CorsHandler) JsonObject(io.vertx.core.json.JsonObject) VxApisDTO(com.szmirren.vxApi.core.options.VxApisDTO)

Example 44 with Message

use of io.vertx.core.eventbus.Message in project failsafe by jhalterman.

the class VertxExample method main.

/**
 * A Vert.x sender and retryable receiver example.
 */
public static void main(String... args) throws Throwable {
    // Receiver that fails 2 times then succeeds
    AtomicInteger failures = new AtomicInteger();
    vertx.eventBus().consumer("ping-address", message -> {
        if (failures.getAndIncrement() < 2)
            message.fail(1, "Failed");
        else {
            message.reply("pong!");
        }
    });
    // Retryable sender
    Failsafe.with(retryPolicy).with(scheduler).getAsyncExecution(execution -> vertx.eventBus().send("ping-address", "ping!", reply -> {
        if (reply.succeeded())
            execution.recordResult(reply.result());
        else
            execution.recordFailure(reply.cause());
    }));
    Thread.sleep(5000);
    System.exit(0);
}
Also used : RetryPolicy(dev.failsafe.RetryPolicy) DefaultScheduledFuture(dev.failsafe.spi.DefaultScheduledFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Vertx(io.vertx.core.Vertx) Message(io.vertx.core.eventbus.Message) ReplyFailure(io.vertx.core.eventbus.ReplyFailure) Failsafe(dev.failsafe.Failsafe) Scheduler(dev.failsafe.spi.Scheduler) ReplyException(io.vertx.core.eventbus.ReplyException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 45 with Message

use of io.vertx.core.eventbus.Message in project vert.x by eclipse.

the class NetTest method setHandlers.

void setHandlers(NetSocket sock) {
    Handler<Message<Buffer>> resumeHandler = m -> sock.resume();
    MessageConsumer reg = vertx.eventBus().<Buffer>consumer("client_resume").handler(resumeHandler);
    sock.closeHandler(v -> reg.unregister());
}
Also used : HAProxyMessageCompletionHandler(io.vertx.core.net.impl.HAProxyMessageCompletionHandler) CoreMatchers(org.hamcrest.CoreMatchers) NetServerImpl(io.vertx.core.net.impl.NetServerImpl) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Utils(io.vertx.core.impl.Utils) Unpooled(io.netty.buffer.Unpooled) PlatformDependent(io.netty.util.internal.PlatformDependent) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReadStream(io.vertx.core.streams.ReadStream) JsonObject(io.vertx.core.json.JsonObject) CheckingSender(io.vertx.test.core.CheckingSender) Logger(io.vertx.core.impl.logging.Logger) VertxHandler(io.vertx.core.net.impl.VertxHandler) ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) ConnectTimeoutException(io.netty.channel.ConnectTimeoutException) TestLoggerFactory(io.vertx.test.netty.TestLoggerFactory) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) java.util.concurrent(java.util.concurrent) Message(io.vertx.core.eventbus.Message) Trust(io.vertx.test.tls.Trust) ChannelPipeline(io.netty.channel.ChannelPipeline) InetSocketAddress(java.net.InetSocketAddress) io.vertx.core.http(io.vertx.core.http) StandardCharsets(java.nio.charset.StandardCharsets) io.netty.handler.codec.http(io.netty.handler.codec.http) SSLException(javax.net.ssl.SSLException) Certificate(java.security.cert.Certificate) Buffer(io.vertx.core.buffer.Buffer) Cert(io.vertx.test.tls.Cert) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) NetSocketInternal(io.vertx.core.net.impl.NetSocketInternal) HttpVersion(io.netty.handler.codec.http.HttpVersion) java.util(java.util) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) io.vertx.core(io.vertx.core) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VertxTestBase(io.vertx.test.core.VertxTestBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) SSLSession(javax.net.ssl.SSLSession) ByteBuf(io.netty.buffer.ByteBuf) io.vertx.test.proxy(io.vertx.test.proxy) OutputStreamWriter(java.io.OutputStreamWriter) Assume(org.junit.Assume) VertxInternal(io.vertx.core.impl.VertxInternal) ConcurrentHashSet(io.vertx.core.impl.ConcurrentHashSet) BufferedWriter(java.io.BufferedWriter) HttpMethod(io.netty.handler.codec.http.HttpMethod) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) File(java.io.File) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) AtomicLong(java.util.concurrent.atomic.AtomicLong) Rule(org.junit.Rule) TemporaryFolder(org.junit.rules.TemporaryFolder) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) Message(io.vertx.core.eventbus.Message)

Aggregations

Message (io.vertx.core.eventbus.Message)53 JsonObject (io.vertx.core.json.JsonObject)37 Test (org.junit.Test)33 Async (io.vertx.ext.unit.Async)19 Vertx (io.vertx.core.Vertx)18 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)17 Before (org.junit.Before)17 PermittedOptions (io.vertx.ext.bridge.PermittedOptions)16 Handler (io.vertx.core.Handler)15 BridgeOptions (io.vertx.ext.bridge.BridgeOptions)15 TestContext (io.vertx.ext.unit.TestContext)15 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)14 After (org.junit.After)14 RunWith (org.junit.runner.RunWith)14 NetServerOptions (io.vertx.core.net.NetServerOptions)12 BridgeEventType (io.vertx.ext.bridge.BridgeEventType)12 JsonArray (io.vertx.core.json.JsonArray)11 NetClient (io.vertx.core.net.NetClient)11 NetSocket (io.vertx.core.net.NetSocket)11 FrameHelper (io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameHelper)11