Search in sources :

Example 1 with EventMeshHttpClientConfig

use of org.apache.eventmesh.client.http.conf.EventMeshHttpClientConfig in project incubator-eventmesh by apache.

the class AsyncPublishInstance method main.

public static void main(String[] args) throws Exception {
    Properties properties = Utils.readPropertiesFile(ExampleConstants.CONFIG_FILE_NAME);
    final String eventMeshIp = properties.getProperty(ExampleConstants.EVENTMESH_IP);
    final String eventMeshHttpPort = properties.getProperty(ExampleConstants.EVENTMESH_HTTP_PORT);
    // if has multi value, can config as: 127.0.0.1:10105;127.0.0.2:10105
    String eventMeshIPPort = ExampleConstants.DEFAULT_EVENTMESH_IP_PORT;
    if (StringUtils.isNotBlank(eventMeshIp) || StringUtils.isNotBlank(eventMeshHttpPort)) {
        eventMeshIPPort = eventMeshIp + ":" + eventMeshHttpPort;
    }
    EventMeshHttpClientConfig eventMeshClientConfig = EventMeshHttpClientConfig.builder().liteEventMeshAddr(eventMeshIPPort).producerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_PRODUCER_GROUP).env("env").idc("idc").ip(IPUtils.getLocalAddress()).sys("1234").pid(String.valueOf(ThreadUtils.getPID())).userName("eventmesh").password("pass").build();
    try (EventMeshHttpProducer eventMeshHttpProducer = new EventMeshHttpProducer(eventMeshClientConfig)) {
        for (int i = 0; i < MESSAGE_SIZE; i++) {
            Map<String, String> content = new HashMap<>();
            content.put("content", "testAsyncMessage");
            CloudEvent event = CloudEventBuilder.v1().withId(UUID.randomUUID().toString()).withSubject(ExampleConstants.EVENTMESH_HTTP_ASYNC_TEST_TOPIC).withSource(URI.create("/")).withDataContentType(ExampleConstants.CLOUDEVENT_CONTENT_TYPE).withType(EventMeshCommon.CLOUD_EVENTS_PROTOCOL_NAME).withData(JsonUtils.serialize(content).getBytes(StandardCharsets.UTF_8)).withExtension(Constants.EVENTMESH_MESSAGE_CONST_TTL, String.valueOf(4 * 1000)).build();
            eventMeshHttpProducer.publish(event);
            log.info("publish event success content: {}", content);
        }
        Thread.sleep(30000);
    }
}
Also used : EventMeshHttpProducer(org.apache.eventmesh.client.http.producer.EventMeshHttpProducer) HashMap(java.util.HashMap) EventMeshHttpClientConfig(org.apache.eventmesh.client.http.conf.EventMeshHttpClientConfig) Properties(java.util.Properties) CloudEvent(io.cloudevents.CloudEvent)

Example 2 with EventMeshHttpClientConfig

use of org.apache.eventmesh.client.http.conf.EventMeshHttpClientConfig in project incubator-eventmesh by apache.

the class SubService method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    final String eventMeshIPPort = eventMeshIp + ":" + eventMeshHttpPort;
    EventMeshHttpClientConfig eventMeshClientConfig = EventMeshHttpClientConfig.builder().liteEventMeshAddr(eventMeshIPPort).consumerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_CONSUMER_GROUP).env(env).idc(idc).ip(IPUtils.getLocalAddress()).sys(subsys).pid(String.valueOf(ThreadUtils.getPID())).build();
    eventMeshHttpConsumer = new EventMeshHttpConsumer(eventMeshClientConfig);
    eventMeshHttpConsumer.heartBeat(topicList, url);
    eventMeshHttpConsumer.subscribe(topicList, url);
    // Wait for all messaged to be consumed
    Thread stopThread = new Thread(() -> {
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("stopThread start....");
        System.exit(0);
    });
    stopThread.start();
}
Also used : EventMeshHttpClientConfig(org.apache.eventmesh.client.http.conf.EventMeshHttpClientConfig) EventMeshHttpConsumer(org.apache.eventmesh.client.http.consumer.EventMeshHttpConsumer)

Example 3 with EventMeshHttpClientConfig

use of org.apache.eventmesh.client.http.conf.EventMeshHttpClientConfig in project incubator-eventmesh by apache.

the class AsyncSyncRequestInstance method main.

public static void main(String[] args) throws Exception {
    EventMeshHttpProducer eventMeshHttpProducer = null;
    try {
        // String eventMeshIPPort = args[0];
        String eventMeshIPPort = "";
        // final String topic = args[1];
        final String topic = "TEST-TOPIC-HTTP-ASYNC";
        if (StringUtils.isBlank(eventMeshIPPort)) {
            // if has multi value, can config as: 127.0.0.1:10105;127.0.0.2:10105
            eventMeshIPPort = "127.0.0.1:10105";
        }
        EventMeshHttpClientConfig eventMeshClientConfig = EventMeshHttpClientConfig.builder().liteEventMeshAddr(eventMeshIPPort).producerGroup("EventMeshTest-producerGroup").env("env").idc("idc").ip(IPUtils.getLocalAddress()).sys("1234").pid(String.valueOf(ThreadUtils.getPID())).build();
        eventMeshHttpProducer = new EventMeshHttpProducer(eventMeshClientConfig);
        final long startTime = System.currentTimeMillis();
        final EventMeshMessage eventMeshMessage = EventMeshMessage.builder().bizSeqNo(RandomStringUtils.generateNum(30)).content("testAsyncMessage").topic(topic).uniqueId(RandomStringUtils.generateNum(30)).build();
        eventMeshHttpProducer.request(eventMeshMessage, new RRCallback<EventMeshMessage>() {

            @Override
            public void onSuccess(EventMeshMessage o) {
                logger.debug("sendmsg : {}, return : {}, cost:{}ms", eventMeshMessage.getContent(), o.getContent(), System.currentTimeMillis() - startTime);
            }

            @Override
            public void onException(Throwable e) {
                logger.debug("sendmsg failed", e);
            }
        }, 3000);
        Thread.sleep(2000);
    } catch (Exception e) {
        logger.warn("async send msg failed", e);
    }
    Thread.sleep(30000);
    try (final EventMeshHttpProducer ignore = eventMeshHttpProducer) {
    // close producer
    } catch (Exception e1) {
        logger.warn("producer shutdown exception", e1);
    }
}
Also used : EventMeshHttpProducer(org.apache.eventmesh.client.http.producer.EventMeshHttpProducer) EventMeshHttpClientConfig(org.apache.eventmesh.client.http.conf.EventMeshHttpClientConfig) EventMeshMessage(org.apache.eventmesh.common.EventMeshMessage)

Example 4 with EventMeshHttpClientConfig

use of org.apache.eventmesh.client.http.conf.EventMeshHttpClientConfig in project incubator-eventmesh by apache.

the class SyncRequestInstance method main.

public static void main(String[] args) throws Exception {
    EventMeshHttpProducer eventMeshHttpProducer = null;
    try {
        String eventMeshIPPort = args[0];
        final String topic = args[1];
        if (StringUtils.isBlank(eventMeshIPPort)) {
            // if has multi value, can config as: 127.0.0.1:10105;127.0.0.2:10105
            eventMeshIPPort = "127.0.0.1:10105";
        }
        EventMeshHttpClientConfig eventMeshClientConfig = EventMeshHttpClientConfig.builder().liteEventMeshAddr(eventMeshIPPort).producerGroup("EventMeshTest-producerGroup").env("env").idc("idc").ip(IPUtils.getLocalAddress()).sys("1234").pid(String.valueOf(ThreadUtils.getPID())).build();
        eventMeshHttpProducer = new EventMeshHttpProducer(eventMeshClientConfig);
        long startTime = System.currentTimeMillis();
        EventMeshMessage eventMeshMessage = EventMeshMessage.builder().bizSeqNo(RandomStringUtils.generateNum(30)).content("contentStr with special protocal").topic(topic).uniqueId(RandomStringUtils.generateNum(30)).build();
        EventMeshMessage rsp = eventMeshHttpProducer.request(eventMeshMessage, 10000);
        if (logger.isDebugEnabled()) {
            logger.debug("sendmsg : {}, return : {}, cost:{}ms", eventMeshMessage.getContent(), rsp.getContent(), System.currentTimeMillis() - startTime);
        }
    } catch (Exception e) {
        logger.warn("send msg failed", e);
    }
    Thread.sleep(30000);
    try (final EventMeshHttpProducer closed = eventMeshHttpProducer) {
    // close producer
    } catch (Exception e1) {
        logger.warn("producer shutdown exception", e1);
    }
}
Also used : EventMeshHttpProducer(org.apache.eventmesh.client.http.producer.EventMeshHttpProducer) EventMeshHttpClientConfig(org.apache.eventmesh.client.http.conf.EventMeshHttpClientConfig) EventMeshMessage(org.apache.eventmesh.common.EventMeshMessage)

Example 5 with EventMeshHttpClientConfig

use of org.apache.eventmesh.client.http.conf.EventMeshHttpClientConfig in project incubator-eventmesh by apache.

the class HttpLoadBalanceUtilsTest method testCreateWeightRandomSelector.

@Test
public void testCreateWeightRandomSelector() throws EventMeshException {
    EventMeshHttpClientConfig eventMeshHttpClientConfig = EventMeshHttpClientConfig.builder().liteEventMeshAddr("127.0.0.1:1001:1;127.0.0.2:1001:2").loadBalanceType(LoadBalanceType.WEIGHT_RANDOM).build();
    LoadBalanceSelector<String> weightRoundRobinSelector = HttpLoadBalanceUtils.createEventMeshServerLoadBalanceSelector(eventMeshHttpClientConfig);
    Assert.assertEquals(LoadBalanceType.WEIGHT_RANDOM, weightRoundRobinSelector.getType());
}
Also used : EventMeshHttpClientConfig(org.apache.eventmesh.client.http.conf.EventMeshHttpClientConfig) Test(org.junit.Test)

Aggregations

EventMeshHttpClientConfig (org.apache.eventmesh.client.http.conf.EventMeshHttpClientConfig)11 EventMeshHttpProducer (org.apache.eventmesh.client.http.producer.EventMeshHttpProducer)7 EventMeshMessage (org.apache.eventmesh.common.EventMeshMessage)6 Properties (java.util.Properties)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 CloudEvent (io.cloudevents.CloudEvent)1 EventMeshHttpConsumer (org.apache.eventmesh.client.http.consumer.EventMeshHttpConsumer)1