Skip to content

Commit

Permalink
[feat]: cache RefrenceClient
Browse files Browse the repository at this point in the history
  • Loading branch information
cxntsh committed Dec 21, 2021
1 parent 91a5f4c commit 2e7b15c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import com.zmops.zeus.dto.DataMessage;
import com.zmops.zeus.dto.ItemValue;
import com.zmops.zeus.facade.DynamicProcotol;
import com.zmops.zeus.iot.server.receiver.module.CamelReceiverModule;
import com.zmops.zeus.iot.server.receiver.service.ReferenceClientService;
import com.zmops.zeus.server.library.module.ModuleManager;
import com.zmops.zeus.server.runtime.SofaStarter;
import com.zmops.zeus.server.runtime.api.client.ReferenceClient;
import com.zmops.zeus.server.runtime.api.client.param.ReferenceParam;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.support.DefaultProducer;
Expand All @@ -20,25 +19,17 @@
@SuppressWarnings("all")
public class ArkBizProducer extends DefaultProducer {

private final SofaStarter sofaStarter;
private final ReferenceClient referenceClient;

private final String methodName;
private final ModuleManager moduleManager;

private final DynamicProcotol dynamicClient;
private final DynamicProcotol dynamicProcotol;

public ArkBizProducer(Endpoint endpoint, ModuleManager moduleManager, String uniqueId, String methodName) {
super(endpoint);
this.moduleManager = moduleManager;
this.sofaStarter = new SofaStarter();
this.referenceClient = this.sofaStarter.getSofaRuntimeContext().getClientFactory().getClient(ReferenceClient.class);
this.methodName = methodName;

ReferenceParam<DynamicProcotol> referenceParam = new ReferenceParam<>();
referenceParam.setInterfaceType(DynamicProcotol.class);
referenceParam.setUniqueId(uniqueId);
dynamicClient = referenceClient.reference(referenceParam);

ReferenceClientService referenceClientService = moduleManager.find(CamelReceiverModule.NAME)
.provider().getService(ReferenceClientService.class);
dynamicProcotol = referenceClientService.getDynamicProtocolClient(uniqueId);
}

@Override
Expand All @@ -48,7 +39,7 @@ public void process(Exchange exchange) throws Exception {
message.setBody(exchange.getMessage().getBody());
message.setHeaders(exchange.getMessage().getHeaders());

List<ItemValue> itemValueList = dynamicClient.protocolHandler(message);
List<ItemValue> itemValueList = dynamicProcotol.protocolHandler(message);

if (itemValueList == null) {
itemValueList = Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.zmops.zeus.iot.server.receiver.module;

import com.zmops.zeus.iot.server.receiver.service.CamelContextHolderService;
import com.zmops.zeus.iot.server.receiver.service.ReferenceClientService;
import com.zmops.zeus.server.library.module.ModuleDefine;

import java.util.ArrayList;
Expand All @@ -23,6 +24,7 @@ public CamelReceiverModule() {
public Class<?>[] services() {
List<Class<?>> classes = new ArrayList<>();
classes.add(CamelContextHolderService.class);
classes.add(ReferenceClientService.class);
return classes.toArray(new Class[]{});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
import com.zmops.zeus.iot.server.receiver.handler.zabbix.ZabbixSenderComponent;
import com.zmops.zeus.iot.server.receiver.module.CamelReceiverModule;
import com.zmops.zeus.iot.server.receiver.service.CamelContextHolderService;
import com.zmops.zeus.iot.server.receiver.service.ReferenceClientService;
import com.zmops.zeus.iot.server.sender.module.ZabbixSenderModule;
import com.zmops.zeus.server.library.module.*;
import com.zmops.zeus.server.runtime.SofaStarter;
import com.zmops.zeus.server.runtime.api.client.ReferenceClient;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.engine.PooledExchangeFactory;
import org.apache.camel.model.ModelCamelContext;

/**
Expand All @@ -19,6 +24,8 @@ public class CamelReceiverProvider extends ModuleProvider {

private ModelCamelContext camelContext;

private ReferenceClient referenceClient;

public CamelReceiverProvider() {
this.camelReceiverConfig = new CamelReceiverConfig();
}
Expand All @@ -41,7 +48,16 @@ public ModuleConfig createConfigBeanIfAbsent() {
@Override
public void prepare() throws ServiceNotProvidedException, ModuleStartException {
camelContext = new DefaultCamelContext(); // master 只有一个 CamelContext
camelContext.disableJMX();
camelContext.setTracing(true);
camelContext.adapt(ExtendedCamelContext.class).setExchangeFactory(new PooledExchangeFactory());

// biz-ark JVM 通信客户端
SofaStarter sofaStarter = new SofaStarter();
this.referenceClient = sofaStarter.getSofaRuntimeContext().getClientFactory().getClient(ReferenceClient.class);

this.registerServiceImplementation(CamelContextHolderService.class, new CamelContextHolderService(camelContext, getManager()));
this.registerServiceImplementation(ReferenceClientService.class, new ReferenceClientService(referenceClient, getManager()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.zmops.zeus.iot.server.receiver.service;

import com.zmops.zeus.facade.DynamicProcotol;
import com.zmops.zeus.server.library.module.ModuleManager;
import com.zmops.zeus.server.library.module.Service;
import com.zmops.zeus.server.runtime.api.client.ReferenceClient;
import com.zmops.zeus.server.runtime.api.client.param.ReferenceParam;

import java.util.concurrent.ConcurrentHashMap;

/**
* @author nantian created at 2021/12/21 15:20
* <p>
* 动态协议代理
*/
public class ReferenceClientService implements Service {

private final ReferenceClient referenceClient;
private final ModuleManager moduleManager;

// 缓存调用 客户端
private static final ConcurrentHashMap<String, DynamicProcotol> dynamicProtoMap = new ConcurrentHashMap<>();

public ReferenceClientService(ReferenceClient client, ModuleManager moduleManager) {
this.referenceClient = client;
this.moduleManager = moduleManager;
}


public ReferenceClient getReferenceClient() {
return referenceClient;
}


/**
* 根据服务ID,动态获取代理客户端
*
* @param serviceId -
* @return DynamicProcotol
*/
public DynamicProcotol getDynamicProtocolClient(String serviceId) {

DynamicProcotol client = dynamicProtoMap.get(serviceId);
if (client != null) {
return client;
}

ReferenceParam<DynamicProcotol> referenceParam = new ReferenceParam<>();
referenceParam.setInterfaceType(DynamicProcotol.class);
referenceParam.setUniqueId(serviceId);

DynamicProcotol client2 = referenceClient.reference(referenceParam);
dynamicProtoMap.put(serviceId, client2);

return client2;
}
}

0 comments on commit 2e7b15c

Please sign in to comment.