@Test public void testRegisterDefaultTwice() throws Exception { MessageCodec codec = new MyPOJOEncoder1(); vertx.eventBus().registerDefaultCodec(MyPOJO.class, codec); assertIllegalStateException(() -> vertx.eventBus().registerDefaultCodec(MyPOJO.class, codec)); }
eventBus.registerDefaultCodec(CustomMessage.class, new CustomMessageCodec());
@Override public void start() throws Exception { EventBus eventBus = getVertx().eventBus(); // Register codec for custom message eventBus.registerDefaultCodec(CustomMessage.class, new CustomMessageCodec()); // Receive message eventBus.consumer("cluster-message-receiver", message -> { CustomMessage customMessage = (CustomMessage) message.body(); System.out.println("Custom message received: "+customMessage.getSummary()); // Replying is same as publishing CustomMessage replyMessage = new CustomMessage(200, "a00000002", "Message sent from cluster receiver!"); message.reply(replyMessage); }); } }
@Test public void testDefaultDecoderSendSymetric() throws Exception { startNodes(2); MessageCodec codec = new MyPOJOEncoder2(); vertices[0].eventBus().registerDefaultCodec(MyPOJO.class, codec); vertices[1].eventBus().registerDefaultCodec(MyPOJO.class, codec); String str = TestUtils.randomAlphaString(100); MyPOJO pojo = new MyPOJO(str); testSend(pojo, pojo, null, null); }
@Test public void testDefaultDecoderSendAsymmetric() throws Exception { startNodes(2); MessageCodec codec = new MyPOJOEncoder1(); vertices[0].eventBus().registerDefaultCodec(MyPOJO.class, codec); vertices[1].eventBus().registerDefaultCodec(MyPOJO.class, codec); String str = TestUtils.randomAlphaString(100); testSend(new MyPOJO(str), str, null, null); }
@Test public void testDefaultDecoderReplyAsymmetric() throws Exception { startNodes(2); MessageCodec codec = new MyPOJOEncoder1(); vertices[0].eventBus().registerDefaultCodec(MyPOJO.class, codec); vertices[1].eventBus().registerDefaultCodec(MyPOJO.class, codec); String str = TestUtils.randomAlphaString(100); testReply(new MyPOJO(str), str, null, null); }
@Test public void testDefaultDecoderReplySymetric() throws Exception { startNodes(2); MessageCodec codec = new MyPOJOEncoder2(); vertices[0].eventBus().registerDefaultCodec(MyPOJO.class, codec); vertices[1].eventBus().registerDefaultCodec(MyPOJO.class, codec); String str = TestUtils.randomAlphaString(100); MyPOJO pojo = new MyPOJO(str); testReply(pojo, pojo, null, null); }
@Test public void testRegisterDefaultSystemDecoder() throws Exception { assertIllegalArgumentException(() -> vertx.eventBus().registerDefaultCodec(MyPOJO.class, new MySystemDecoder())); }
@Test public void testDefaultCodecNullName() throws Exception { assertNullPointerException(() -> vertx.eventBus().registerDefaultCodec(String.class, new NullNameCodec())); }
@Test public void testDefaultDecoderSendAsymmetric() throws Exception { MessageCodec codec = new MyPOJOEncoder1(); vertx.eventBus().registerDefaultCodec(MyPOJO.class, codec); String str = TestUtils.randomAlphaString(100); testSend(new MyPOJO(str), str, null, null); }
@Test public void testDefaultDecoderReplySymetric() throws Exception { MessageCodec codec = new MyPOJOEncoder2(); vertx.eventBus().registerDefaultCodec(MyPOJO.class, codec); String str = TestUtils.randomAlphaString(100); MyPOJO pojo = new MyPOJO(str); testReply(pojo, pojo, null, null); }
@Test public void testDefaultDecoderReplyAsymmetric() throws Exception { MessageCodec codec = new MyPOJOEncoder1(); vertx.eventBus().registerDefaultCodec(MyPOJO.class, codec); String str = TestUtils.randomAlphaString(100); testReply(new MyPOJO(str), str, null, null); }
@Test public void testDefaultDecoderSendSymetric() throws Exception { MessageCodec codec = new MyPOJOEncoder2(); vertx.eventBus().registerDefaultCodec(MyPOJO.class, codec); String str = TestUtils.randomAlphaString(100); MyPOJO pojo = new MyPOJO(str); testSend(pojo, pojo, null, null); }
@Test public void testDefaultCodecReplyExceptionSubclass() throws Exception { startNodes(2); MyReplyException myReplyException = new MyReplyException(23, "my exception"); MyReplyExceptionMessageCodec codec = new MyReplyExceptionMessageCodec(); vertices[0].eventBus().registerDefaultCodec(MyReplyException.class, codec); vertices[1].eventBus().registerDefaultCodec(MyReplyException.class, codec); MessageConsumer<ReplyException> reg = vertices[0].eventBus().<ReplyException>consumer(ADDRESS1, msg -> { assertTrue(msg.body() instanceof MyReplyException); testComplete(); }); reg.completionHandler(ar -> { vertices[1].eventBus().send(ADDRESS1, myReplyException); }); await(); }
@Test public void testUnregisterDefaultDecoder() throws Exception { MessageCodec codec = new MyPOJOEncoder1(); vertx.eventBus().registerDefaultCodec(MyPOJO.class, codec); vertx.eventBus().unregisterDefaultCodec(MyPOJO.class); assertIllegalArgumentException(() -> vertx.eventBus().send(ADDRESS1, new MyPOJO("foo"))); }
@Test public void testDefaultCodecReplyExceptionSubclass() throws Exception { MyReplyException myReplyException = new MyReplyException(23, "my exception"); vertx.eventBus().registerDefaultCodec(MyReplyException.class, new MyReplyExceptionMessageCodec()); eb.<ReplyException>consumer(ADDRESS1, msg -> { assertTrue(msg.body() instanceof MyReplyException); testComplete(); }); vertx.eventBus().send(ADDRESS1, myReplyException); await(); }
@Test public void testArgumentValidation() throws Exception { assertNullPointerException(() -> eb.send(null, "")); assertNullPointerException(() -> eb.send(null, "", handler -> {})); assertNullPointerException(() -> eb.send(null, "", new DeliveryOptions())); assertNullPointerException(() -> eb.send("", "", (DeliveryOptions) null)); assertNullPointerException(() -> eb.send(null, "", new DeliveryOptions(), handler -> {})); assertNullPointerException(() -> eb.send("", "", null, handler -> {})); assertNullPointerException(() -> eb.publish(null, "")); assertNullPointerException(() -> eb.publish(null, "", new DeliveryOptions())); assertNullPointerException(() -> eb.publish("", "", null)); assertNullPointerException(() -> eb.consumer(null)); assertNullPointerException(() -> eb.consumer(null, msg -> {})); assertNullPointerException(() -> eb.consumer(ADDRESS1, null)); assertNullPointerException(() -> eb.localConsumer(null)); assertNullPointerException(() -> eb.localConsumer(null, msg -> {})); assertNullPointerException(() -> eb.localConsumer(ADDRESS1, null)); assertNullPointerException(() -> eb.sender(null)); assertNullPointerException(() -> eb.sender(null, new DeliveryOptions())); assertNullPointerException(() -> eb.publisher("", null)); assertNullPointerException(() -> eb.publisher(null, new DeliveryOptions())); assertNullPointerException(() -> eb.registerCodec(null)); assertNullPointerException(() -> eb.unregisterCodec(null)); assertNullPointerException(() -> eb.registerDefaultCodec(null, new MyPOJOEncoder1())); assertNullPointerException(() -> eb.registerDefaultCodec(Object.class, null)); assertNullPointerException(() -> eb.unregisterDefaultCodec(null)); }
@Test public void testDefaultDecoderReplySymetric() throws Exception { startNodes(2); MessageCodec codec = new MyPOJOEncoder2(); vertices[0].eventBus().registerDefaultCodec(MyPOJO.class, codec); vertices[1].eventBus().registerDefaultCodec(MyPOJO.class, codec); String str = TestUtils.randomAlphaString(100); MyPOJO pojo = new MyPOJO(str); testReply(pojo, pojo, null, null); }
@Test public void testDefaultDecoderReplyAsymmetric() throws Exception { MessageCodec codec = new MyPOJOEncoder1(); vertx.eventBus().registerDefaultCodec(MyPOJO.class, codec); String str = TestUtils.randomAlphaString(100); testReply(new MyPOJO(str), str, null, null); }
@Test public void testDefaultDecoderSendSymetric() throws Exception { MessageCodec codec = new MyPOJOEncoder2(); vertx.eventBus().registerDefaultCodec(MyPOJO.class, codec); String str = TestUtils.randomAlphaString(100); MyPOJO pojo = new MyPOJO(str); testSend(pojo, pojo, null, null); }