-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add SDK manager * fix: SDKManagerTest * fix: use SimpleEntry to replace Pair * style: better name for SDK codes * style * style * style: just rename SDK * fix: checkstyle
- Loading branch information
1 parent
a165e04
commit 058ba06
Showing
24 changed files
with
998 additions
and
12 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
...c/main/java/org/apache/eventmesh/dashboard/console/function/SDK/AbstractSDKOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* 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.dashboard.console.function.SDK; | ||
|
||
public abstract class AbstractSDKOperation<T> implements SDKOperation<T> { | ||
|
||
protected T castClient(Object client) { | ||
try { | ||
return (T) client; | ||
} catch (ClassCastException e) { | ||
throw new IllegalArgumentException("Client is not of the expected type", e); | ||
} | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
...console/src/main/java/org/apache/eventmesh/dashboard/console/function/SDK/SDKManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* 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.dashboard.console.function.SDK; | ||
|
||
import org.apache.eventmesh.dashboard.console.function.SDK.config.CreateSDKConfig; | ||
import org.apache.eventmesh.dashboard.console.function.SDK.operation.NacosConfigSDKOperation; | ||
import org.apache.eventmesh.dashboard.console.function.SDK.operation.NacosNamingSDKOperation; | ||
import org.apache.eventmesh.dashboard.console.function.SDK.operation.NacosSDKOperation; | ||
import org.apache.eventmesh.dashboard.console.function.SDK.operation.RedisSDKOperation; | ||
import org.apache.eventmesh.dashboard.console.function.SDK.operation.RocketMQProduceSDKOperation; | ||
import org.apache.eventmesh.dashboard.console.function.SDK.operation.RocketMQPushConsumerSDKOperation; | ||
import org.apache.eventmesh.dashboard.console.function.SDK.operation.RocketMQRemotingSDKOperation; | ||
|
||
import java.util.AbstractMap.SimpleEntry; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
|
||
|
||
/** | ||
* SDK manager is a singleton to manage all SDK clients, it is a facade to create, delete and get a client. | ||
*/ | ||
public class SDKManager { | ||
|
||
private static final SDKManager INSTANCE = new SDKManager(); | ||
|
||
|
||
public static SDKManager getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
/** | ||
* inner key is the unique key of a client, such as (ip + port) they are defined in CreateClientConfig | ||
* | ||
* @see CreateSDKConfig#getUniqueKey() | ||
*/ | ||
|
||
private final Map<SDKTypeEnum, Map<String, Object>> clientMap = new ConcurrentHashMap<>(); | ||
|
||
private final Map<SDKTypeEnum, SDKOperation<?>> clientCreateOperationMap = new ConcurrentHashMap<>(); | ||
|
||
// register all client create operation | ||
{ | ||
for (SDKTypeEnum clientTypeEnum : SDKTypeEnum.values()) { | ||
clientMap.put(clientTypeEnum, new ConcurrentHashMap<>()); | ||
} | ||
|
||
clientCreateOperationMap.put(SDKTypeEnum.STORAGE_REDIS, new RedisSDKOperation()); | ||
|
||
clientCreateOperationMap.put(SDKTypeEnum.STORAGE_ROCKETMQ_REMOTING, new RocketMQRemotingSDKOperation()); | ||
clientCreateOperationMap.put(SDKTypeEnum.STORAGE_ROCKETMQ_PRODUCER, new RocketMQProduceSDKOperation()); | ||
clientCreateOperationMap.put(SDKTypeEnum.STORAGE_ROCKETMQ_CONSUMER, new RocketMQPushConsumerSDKOperation()); | ||
|
||
clientCreateOperationMap.put(SDKTypeEnum.META_NACOS, new NacosSDKOperation()); | ||
clientCreateOperationMap.put(SDKTypeEnum.META_NACOS_CONFIG, new NacosConfigSDKOperation()); | ||
clientCreateOperationMap.put(SDKTypeEnum.META_NACOS_NAMING, new NacosNamingSDKOperation()); | ||
|
||
} | ||
|
||
private SDKManager() { | ||
} | ||
|
||
public <T> SimpleEntry<String, T> createClient(SDKTypeEnum clientTypeEnum, CreateSDKConfig config) { | ||
return createClient(clientTypeEnum, config.getUniqueKey(), config); | ||
} | ||
|
||
public <T> SimpleEntry<String, T> createClient(SDKTypeEnum clientTypeEnum, String uniqueKey, CreateSDKConfig config) { | ||
|
||
Map<String, Object> clients = this.clientMap.get(clientTypeEnum); | ||
|
||
Object client = clients.get(uniqueKey); | ||
SimpleEntry<String, ?> result = new SimpleEntry<>(uniqueKey, client); | ||
if (Objects.isNull(client)) { | ||
SDKOperation<?> clientCreateOperation = this.clientCreateOperationMap.get(clientTypeEnum); | ||
result = clientCreateOperation.createClient(config); | ||
clients.put(result.getKey(), result.getValue()); | ||
} | ||
try { | ||
return (SimpleEntry<String, T>) result; | ||
} catch (Exception e) { | ||
throw new RuntimeException("create client error", e); | ||
} | ||
} | ||
|
||
public void deleteClient(SDKTypeEnum clientTypeEnum, String uniqueKey) { | ||
Map<String, Object> clients = this.clientMap.get(clientTypeEnum); | ||
SDKOperation<?> operation = this.clientCreateOperationMap.get(clientTypeEnum); | ||
try { | ||
operation.close(clients.get(uniqueKey)); | ||
} catch (Exception e) { | ||
throw new RuntimeException("close client error", e); | ||
} | ||
clients.remove(uniqueKey); | ||
} | ||
|
||
public Object getClient(SDKTypeEnum clientTypeEnum, String uniqueKey) { | ||
return this.clientMap.get(clientTypeEnum).get(uniqueKey); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...nsole/src/main/java/org/apache/eventmesh/dashboard/console/function/SDK/SDKOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* 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.dashboard.console.function.SDK; | ||
|
||
import org.apache.eventmesh.dashboard.console.function.SDK.config.CreateSDKConfig; | ||
|
||
import java.util.AbstractMap.SimpleEntry; | ||
|
||
/** | ||
* Operation to create and close a client, the operations will be store in the SDKManager | ||
* | ||
* @param <T> SDK client | ||
*/ | ||
public interface SDKOperation<T> { | ||
|
||
public SimpleEntry<String, T> createClient(CreateSDKConfig clientConfig); | ||
|
||
|
||
public void close(Object client); | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...onsole/src/main/java/org/apache/eventmesh/dashboard/console/function/SDK/SDKTypeEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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.dashboard.console.function.SDK; | ||
|
||
public enum SDKTypeEnum { | ||
|
||
RUNTIME, | ||
|
||
STORAGE_ROCKETMQ_REMOTING, | ||
|
||
STORAGE_ROCKETMQ_PRODUCER, | ||
|
||
STORAGE_ROCKETMQ_CONSUMER, | ||
|
||
STORAGE_REDIS, | ||
|
||
META_NACOS, | ||
META_NACOS_CONFIG, | ||
|
||
META_NACOS_NAMING, | ||
|
||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...in/java/org/apache/eventmesh/dashboard/console/function/SDK/config/CreateNacosConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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.dashboard.console.function.SDK.config; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class CreateNacosConfig implements CreateSDKConfig { | ||
|
||
private String serverAddress; | ||
|
||
@Override | ||
public String getUniqueKey() { | ||
return serverAddress; | ||
} | ||
} | ||
|
||
|
31 changes: 31 additions & 0 deletions
31
...in/java/org/apache/eventmesh/dashboard/console/function/SDK/config/CreateRedisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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.dashboard.console.function.SDK.config; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class CreateRedisConfig implements CreateSDKConfig { | ||
|
||
private String redisUrl; | ||
|
||
@Override | ||
public String getUniqueKey() { | ||
return redisUrl; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...java/org/apache/eventmesh/dashboard/console/function/SDK/config/CreateRocketmqConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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.dashboard.console.function.SDK.config; | ||
|
||
import org.apache.rocketmq.client.consumer.listener.MessageListener; | ||
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class CreateRocketmqConfig implements CreateSDKConfig { | ||
|
||
// common | ||
private String nameServerUrl; | ||
private String brokerUrl; | ||
|
||
//consumer | ||
private String consumerGroup; | ||
private MessageModel messageModel = MessageModel.CLUSTERING; | ||
|
||
//producer | ||
private String producerGroup; | ||
|
||
//topic | ||
private String topic; | ||
private String subExpression = "*"; | ||
|
||
private MessageListener messageListener; | ||
|
||
|
||
@Override | ||
public String getUniqueKey() { | ||
if (nameServerUrl != null) { | ||
return nameServerUrl; | ||
} else if (brokerUrl != null) { | ||
return brokerUrl; | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
.../java/org/apache/eventmesh/dashboard/console/function/SDK/operation/EtcdSDKOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* 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.dashboard.console.function.SDK.operation; | ||
|
||
import org.apache.eventmesh.dashboard.console.function.SDK.AbstractSDKOperation; | ||
import org.apache.eventmesh.dashboard.console.function.SDK.config.CreateSDKConfig; | ||
|
||
import java.util.AbstractMap.SimpleEntry; | ||
|
||
public class EtcdSDKOperation extends AbstractSDKOperation { | ||
@Override | ||
public SimpleEntry createClient(CreateSDKConfig clientConfig) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void close(Object client) { | ||
|
||
} | ||
} |
Oops, something went wrong.