Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #1927]add workflow demo #1935

Merged
merged 2 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eventmesh-examples/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies {
implementation "io.cloudevents:cloudevents-core"
implementation "io.cloudevents:cloudevents-json-jackson"
implementation "io.openmessaging:openmessaging-api"
implementation 'com.alibaba.nacos:nacos-client:2.1.0'

implementation "io.grpc:grpc-protobuf:${grpcVersion}"
implementation "io.grpc:grpc-stub:${grpcVersion}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@ public class ExampleConstants {
public static final String DEFAULT_EVENTMESH_TEST_PRODUCER_GROUP = "EventMeshTest-producerGroup";
public static final String DEFAULT_EVENTMESH_TEST_CONSUMER_GROUP = "EventMeshTest-consumerGroup";

public static final String SERVER_NAME = "server.name";

public static final String EVENTMESH_CATALOG_NAME = "eventmesh.catalog.name";

public static final String EVENTMESH_WORKFLOW_NAME = "eventmesh.workflow.name";

public static final String EVENTMESH_SELECTOR_NACOS_ADDRESS = "eventmesh.selector.nacos.address";

public static final String EVENTMESH_SELECTOR_TYPE = "eventmesh.selector.type";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.eventmesh.grpc.pub.eventmeshmessage;

import org.apache.eventmesh.client.grpc.config.EventMeshGrpcClientConfig;
import org.apache.eventmesh.client.grpc.producer.EventMeshGrpcProducer;
import org.apache.eventmesh.client.selector.SelectorFactory;
import org.apache.eventmesh.client.workflow.EventMeshWorkflowClient;
import org.apache.eventmesh.client.workflow.config.EventMeshWorkflowClientConfig;
import org.apache.eventmesh.common.ExampleConstants;
import org.apache.eventmesh.common.protocol.workflow.protos.ExecuteRequest;
import org.apache.eventmesh.common.protocol.workflow.protos.ExecuteResponse;
import org.apache.eventmesh.selector.NacosSelector;
import org.apache.eventmesh.util.Utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.nacos.shaded.com.google.gson.Gson;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class WorkflowAsyncPublishInstance {

private static final Logger logger = LoggerFactory.getLogger(WorkflowAsyncPublishInstance.class);

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 eventMeshGrpcPort = properties.getProperty(ExampleConstants.EVENTMESH_GRPC_PORT);
final String workflowServerName = properties.getProperty(ExampleConstants.EVENTMESH_WORKFLOW_NAME);
final String selectorType = properties.getProperty(ExampleConstants.EVENTMESH_SELECTOR_TYPE);

EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder()
.serverAddr(eventMeshIp)
.serverPort(Integer.parseInt(eventMeshGrpcPort))
.producerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_PRODUCER_GROUP)
.env("PRD").idc("DEFAULT").password("password")
.sys("DEFAULT").build();

EventMeshGrpcProducer eventMeshGrpcProducer = new EventMeshGrpcProducer(eventMeshClientConfig);
eventMeshGrpcProducer.init();

NacosSelector nacosSelector = new NacosSelector();
nacosSelector.init();
SelectorFactory.register(selectorType, nacosSelector);

EventMeshWorkflowClientConfig eventMeshWorkflowClientConfig = EventMeshWorkflowClientConfig.builder().serverName(workflowServerName).build();
EventMeshWorkflowClient eventMeshWorkflowClient = new EventMeshWorkflowClient(eventMeshWorkflowClientConfig);

ExecuteRequest.Builder executeRequest = ExecuteRequest.newBuilder();
Map<String, String> content = new HashMap<>();
content.put("order_no", "workflowmessage");
executeRequest.setInput(new Gson().toJson(content));
executeRequest.setId("storeorderworkflow");
ExecuteResponse response = eventMeshWorkflowClient.getWorkflowClient().execute(executeRequest.build());
logger.info("received response: {}", response.toString());

Thread.sleep(60000);
try (EventMeshGrpcProducer ignore = eventMeshGrpcProducer) {
// ignore
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.eventmesh.grpc.sub;

import org.apache.eventmesh.client.catalog.EventMeshCatalogClient;
import org.apache.eventmesh.client.catalog.config.EventMeshCatalogClientConfig;
import org.apache.eventmesh.client.grpc.config.EventMeshGrpcClientConfig;
import org.apache.eventmesh.client.grpc.consumer.EventMeshGrpcConsumer;
import org.apache.eventmesh.client.grpc.consumer.ReceiveMsgHook;
import org.apache.eventmesh.client.selector.SelectorFactory;
import org.apache.eventmesh.client.tcp.common.EventMeshCommon;
import org.apache.eventmesh.client.workflow.EventMeshWorkflowClient;
import org.apache.eventmesh.client.workflow.config.EventMeshWorkflowClientConfig;
import org.apache.eventmesh.common.EventMeshMessage;
import org.apache.eventmesh.common.ExampleConstants;
import org.apache.eventmesh.common.protocol.workflow.protos.ExecuteRequest;
import org.apache.eventmesh.common.protocol.workflow.protos.ExecuteResponse;
import org.apache.eventmesh.selector.NacosSelector;
import org.apache.eventmesh.util.Utils;

import java.util.Map;
import java.util.Optional;
import java.util.Properties;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class WorkflowAsyncSubscribe implements ReceiveMsgHook<EventMeshMessage> {

public static WorkflowAsyncSubscribe handler = new WorkflowAsyncSubscribe();
public static EventMeshWorkflowClient workflowClient;

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 eventMeshGrpcPort = properties.getProperty(ExampleConstants.EVENTMESH_GRPC_PORT);
final String serverName = "express";
final String catalogServerName = properties.getProperty(ExampleConstants.EVENTMESH_CATALOG_NAME);
final String workflowServerName = properties.getProperty(ExampleConstants.EVENTMESH_WORKFLOW_NAME);
final String selectorType = properties.getProperty(ExampleConstants.EVENTMESH_SELECTOR_TYPE);

EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder()
.serverAddr(eventMeshIp)
.serverPort(Integer.parseInt(eventMeshGrpcPort))
.consumerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_CONSUMER_GROUP)
.env("env").idc("default").password("password")
.sys("default").build();

EventMeshGrpcConsumer eventMeshGrpcConsumer = new EventMeshGrpcConsumer(eventMeshClientConfig);
eventMeshGrpcConsumer.init();
eventMeshGrpcConsumer.registerListener(handler);

NacosSelector nacosSelector = new NacosSelector();
nacosSelector.init();
SelectorFactory.register(selectorType, nacosSelector);

EventMeshCatalogClientConfig eventMeshCatalogClientConfig = EventMeshCatalogClientConfig.builder().serverName(catalogServerName)
.appServerName(serverName).build();
EventMeshCatalogClient eventMeshCatalogClient = new EventMeshCatalogClient(eventMeshCatalogClientConfig, eventMeshGrpcConsumer);
eventMeshCatalogClient.init();

EventMeshWorkflowClientConfig eventMeshWorkflowClientConfig = EventMeshWorkflowClientConfig.builder().serverName(workflowServerName).build();
workflowClient = new EventMeshWorkflowClient(eventMeshWorkflowClientConfig);

Thread.sleep(60000);
eventMeshCatalogClient.destroy();
}

@Override
public Optional<EventMeshMessage> handle(EventMeshMessage msg) throws Exception {
log.info("receive async msg: {}", msg);
Map<String, String> props = msg.getProp();
String workflowInstanceId = props.get("workflowinstanceid");
String taskInstanceId = props.get("workflowtaskinstanceid");

ExecuteRequest executeRequest = ExecuteRequest.newBuilder().setId("storeorderworkflow")
.setTaskInstanceId(taskInstanceId)
.setInstanceId(workflowInstanceId).build();
ExecuteResponse response = workflowClient.getWorkflowClient().execute(executeRequest);
log.info("receive workflow msg: {}", response.getInstanceId());
return Optional.empty();
}

@Override
public String getProtocolType() {
return EventMeshCommon.EM_MESSAGE_PROTOCOL_NAME;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.eventmesh.grpc.sub;

import org.apache.eventmesh.client.catalog.EventMeshCatalogClient;
import org.apache.eventmesh.client.catalog.config.EventMeshCatalogClientConfig;
import org.apache.eventmesh.client.grpc.config.EventMeshGrpcClientConfig;
import org.apache.eventmesh.client.grpc.consumer.EventMeshGrpcConsumer;
import org.apache.eventmesh.client.grpc.consumer.ReceiveMsgHook;
import org.apache.eventmesh.client.selector.SelectorFactory;
import org.apache.eventmesh.client.tcp.common.EventMeshCommon;
import org.apache.eventmesh.client.workflow.EventMeshWorkflowClient;
import org.apache.eventmesh.client.workflow.config.EventMeshWorkflowClientConfig;
import org.apache.eventmesh.common.EventMeshMessage;
import org.apache.eventmesh.common.ExampleConstants;
import org.apache.eventmesh.common.protocol.workflow.protos.ExecuteRequest;
import org.apache.eventmesh.common.protocol.workflow.protos.ExecuteResponse;
import org.apache.eventmesh.selector.NacosSelector;
import org.apache.eventmesh.util.Utils;

import java.util.Map;
import java.util.Optional;
import java.util.Properties;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class WorkflowOrderAsyncSubscribe implements ReceiveMsgHook<EventMeshMessage> {

public static WorkflowOrderAsyncSubscribe handler = new WorkflowOrderAsyncSubscribe();
public static EventMeshWorkflowClient workflowClient;

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 eventMeshGrpcPort = properties.getProperty(ExampleConstants.EVENTMESH_GRPC_PORT);
final String serverName = "order";
final String workflowServerName = properties.getProperty(ExampleConstants.EVENTMESH_WORKFLOW_NAME);
final String catalogServerName = properties.getProperty(ExampleConstants.EVENTMESH_CATALOG_NAME);
final String selectorType = properties.getProperty(ExampleConstants.EVENTMESH_SELECTOR_TYPE);

EventMeshGrpcClientConfig eventMeshClientConfig = EventMeshGrpcClientConfig.builder()
.serverAddr(eventMeshIp)
.serverPort(Integer.parseInt(eventMeshGrpcPort))
.consumerGroup(ExampleConstants.DEFAULT_EVENTMESH_TEST_CONSUMER_GROUP)
.env("test").idc("default").password("password")
.sys("default").build();

EventMeshGrpcConsumer eventMeshGrpcConsumer = new EventMeshGrpcConsumer(eventMeshClientConfig);
eventMeshGrpcConsumer.init();
eventMeshGrpcConsumer.registerListener(handler);

NacosSelector nacosSelector = new NacosSelector();
nacosSelector.init();
SelectorFactory.register(selectorType, nacosSelector);

EventMeshCatalogClientConfig eventMeshCatalogClientConfig = EventMeshCatalogClientConfig.builder().serverName(catalogServerName)
.appServerName(serverName).build();
EventMeshCatalogClient eventMeshCatalogClient = new EventMeshCatalogClient(eventMeshCatalogClientConfig, eventMeshGrpcConsumer);
eventMeshCatalogClient.init();

EventMeshWorkflowClientConfig eventMeshWorkflowClientConfig = EventMeshWorkflowClientConfig.builder().serverName(workflowServerName).build();
workflowClient = new EventMeshWorkflowClient(eventMeshWorkflowClientConfig);

Thread.sleep(60000);
eventMeshCatalogClient.destroy();
}

@Override
public Optional<EventMeshMessage> handle(EventMeshMessage msg) throws Exception {
log.info("receive async msg: {}", msg);

Map<String, String> props = msg.getProp();
String workflowInstanceId = props.get("workflowinstanceid");
String taskInstanceId = props.get("workflowtaskinstanceid");

ExecuteRequest executeRequest = ExecuteRequest.newBuilder().setId("storeorderworkflow")
.setTaskInstanceId(taskInstanceId)
.setInstanceId(workflowInstanceId).build();
ExecuteResponse response = workflowClient.getWorkflowClient().execute(executeRequest);
log.info("receive workflow msg: {}", response.getInstanceId());
return Optional.empty();
}

@Override
public String getProtocolType() {
return EventMeshCommon.EM_MESSAGE_PROTOCOL_NAME;
}
}
Loading