我试图实现一个 Axis2 服务,它接收用户请求并将它们作为事件发布到 CEP,使用 carbon databridge thrift(通过 'org.wso2.carbon.databridge.agent.thrift.DataPublisher')
我遵循了 wso2cep-3.1.0/samples/producers/activity-monitor 中提供的代码示例
请看下面的代码片段
public class GatewayServiceSkeleton{
private static Logger logger = Logger.getLogger(GatewayServiceSkeleton.class);
public RequestResponse request(Request request)throws AgentException,
MalformedStreamDefinitionException,StreamDefinitionException,
DifferentStreamDefinitionAlreadyDefinedException,
MalformedURLException,AuthenticationException,DataBridgeException,
NoStreamDefinitionExistException,TransportException, SocketException,
org.wso2.carbon.databridge.commons.exception.AuthenticationException
{
final String GATEWAY_SERVICE_STREAM = "gateway.cep";
final String VERSION = "1.0.0";
final String PROTOCOL = "tcp://";
final String CEPHOST = "cep.gubnoi.com";
final String CEPPORT = "7611";
final String CEPUSERNAME = "admin";
final String CEPPASSWORD = "admin";
Object[] metadata = { request.getDeviceID(), request.getViewID()};
Object[] correlationdata = { request.getSessionID()};
Object[] payloaddata = {request.getBucket()};
KeyStoreUtil.setTrustStoreParams();
KeyStoreUtil.setKeyStoreParams();
DataPublisher dataPublisher = new DataPublisher(PROTOCOL + CEPHOST + ":" + CEPPORT, CEPUSERNAME, CEPPASSWORD);
//create event
Event event = new Event (GATEWAY_SERVICE_STREAM + ":" + VERSION, System.currentTimeMillis(), metadata, correlationdata, payloaddata);
//Publish event for a valid stream
dataPublisher.publish(event);
//stop
dataPublisher.stop();
RequestResponse response = new RequestResponse();
response.setSessionID(request.getSessionID());
response.setDeviceID(request.getDeviceID());
response.setViewID(request.getViewID());
response.setBucket(request.getBucket());
return response;
}
还有一个实用程序类将密钥存储参数设置如下
public class KeyStoreUtil {
static File filePath = new File("../../../repository/resources/security");
public static void setTrustStoreParams() {
String trustStore = filePath.getAbsolutePath();
System.setProperty("javax.net.ssl.trustStore", trustStore + "/client-truststore.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
}
public static void setKeyStoreParams() {
String keyStore = filePath.getAbsolutePath();
System.setProperty("Security.KeyStore.Location", keyStore + "/wso2carbon.jks");
System.setProperty("Security.KeyStore.Password", "wso2carbon");
}
}
我将服务上传到 wso2as-5.2.1,并使用 SOAPUI 调用服务
请求返回错误消息“无法为 TCP 借用客户端”
我调试,发现问题可能出在“KeyStoreUtil”类上,
其中'filePath'以某种方式重新调整了'null',
static File filePath = new File("../../../repository/resources/security");
并导致这条线上的失败
DataPublisher dataPublisher = new DataPublisher(PROTOCOL + CEPHOST + ":" + CEPPORT, CEPUSERNAME, CEPPASSWORD);
我想如果我使用“CARBON_HOME”的值来确定密钥库的位置可能是一个更好的主意
所以我的问题是:
我怎样才能在 Java 代码中获得“CARBON_HOME”的值?
那说。如果您想得更多:该服务将被调用无数次;而“setTrustStoreParams”和“setKeyStoreParams”只需要在服务器/服务启动时执行一次。
那么,有没有更好的方法从服务代码中删除“setTrustStoreParams”和“setKeyStoreParams”,或者作为可配置项实现?
请指教
谢谢