java - 我可以在调用 Axis 2 Java 中的 stub 之前设置 MessageContext 的属性吗

标签 java web-services wso2 apache-axis webservice-client

我使用 AXIS 2 通过名为 ChannelConnectServiceStub 的 stub 来调用 WS 方法。

生成 stub 和 ConfigurationContext :

public class TestWSClient {

    private void init() throws Exception {

        String                  proxyUrl                    = "http://subdom.dom.com/testpath/TestConnect.asmx";
        ConfigurationContext    ctx                         = ConfigurationContextFactory.createConfigurationContextFromFileSystem("/rootFolder/Axis2/axis2-1.4.1/repository", "/rootFolder/Axis2/axis2-1.4.1/conf/axis2.xml");
        ChannelConnectServiceStub channelConnectServiceStub = new ChannelConnectServiceStub(ctx,proxyUrl);

        ctx.setProperty("testid", "testidval"); // Approach 1
        channelConnectServiceStub._getServiceClient().getServiceContext().setProperty("testid", "testidval"); // Approach 2

    }
}

我正在使用 LogHandler 来记录消息请求和响应。

日志处理程序:

class LogHandler extends AbstractHandler implements Handler {

    @Override
    public InvocationResponse invoke(MessageContext messageContext) throws AxisFault {

        String testID       = null;
        String invokeStr    = null;
        String axisService  = null;
        String action       = null;

        invokeStr       = messageContext.getEnvelope().toString();
        axisService     = messageContext.getAxisService().getName();
        action          = messageContext.getAxisMessage().getAxisOperation().getInputAction();

        testID = (String) messageContext.getProperty("testid");// Approach 1
        testID = (String) messageContext.getServiceContext().getProperty("testid");// Approach 2

        return InvocationResponse.CONTINUE;
    }

}

我想从创建和调用 stub 到 LogHandler 类的点传递一个属性(“testid”)。我已经提到了我采取的两种方法。

两者都在传递值。但问题是,有多个客户端线程使用同一个 TestWSClient 来使用服务。因此,当涉及 LogHandler 时,不同客户端设置的不同值可以互换。 (但是invokeStr、AxisService和action没有这个问题)。

  1. 有没有办法在之前将属性传递给 MessageContext stub 被调用?
  2. 任何人都可以帮助将属性(property)从 stub 转移到 LogHandler 无需在多线程中交换值 环境。

我也尝试了下面的一个,但失败了,因为 operationContext 为 NULL。

OperationContext operationContext = stub._getServiceClient().getLastOperationContext();
logger.info("operationContext : " + operationContext);

if (operationContext != null) {

    MessageContext outMessageContext = operationContext.getMessageContext("Out");

    if (outMessageContext != null) {
        logger.info("outMessageContext.getEnvelope().toString() : " + outMessageContext.getEnvelope().toString());
        outMessageContext.setProperty("Portal", getPortal());
    }

    MessageContext inMessageContext = operationContext.getMessageContext("In");
    logger.info("inMessageContext : " + inMessageContext);

    if (inMessageContext != null) {
        logger.info("inMessageContext.getEnvelope().toString() : " + inMessageContext.getEnvelope().toString());
        inMessageContext.setProperty("Portal", getPortal());
    }

}

最佳答案

确保获取 ConfigurationContext 的单例实例。

当您从 ServiceContext 执行 setProperty 和 getProperty 操作时,请注意您正在获取每个 jvm 的 Property 对象的共享副本,因此不要使用“testid”键,而是使用唯一的键,

例如: 在 stub 初始化之后的客户端代码中,而不是,

channelConnectServiceStub._getServiceClient().getServiceContext()
  .setProperty("testid","testidval");

尝试

channelConnectServiceStub._getServiceClient().getServiceContext()
 .setProperty(stub._getServiceClient().getServiceContext().getName(), "testidval");

要检索属性,请在日志处理程序中使用相同的 key (msgContext.getServiceContext().getName() 每个流都是唯一的)

而不是

messageContext.getServiceContext().getProperty("testid");

尝试

messageContext.getServiceContext()
 .getProperty(msgContext.getServiceContext().getName());

另请注意,当您将值存储在 jvm 共享 Property 对象上时,为了避免内存增长,请在不再需要该值时将其删除。

messageContext.getServiceContext()
 .removeProperty(msgContext.getServiceContext().getName();

关于java - 我可以在调用 Axis 2 Java 中的 stub 之前设置 MessageContext 的属性吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17653849/

相关文章:

java - 我的编程项目无法运行

Java Spring MVC 文件夹/命名约定

Java 在我的字符串中添加双引号

tomcat - centos上的wso2-apps需要安装tomcat吗?

logging - WSO2 ESB日志太大,如何设置?

java - 如何使用 jmockit 模拟 RestTemplate getForObject 方法?

java - 我们可以将请求文件名参数化到Karate中的Read方法吗?

java - 部署 CXF Restful WebServices 时如何避免与 Spring 相关的警告

wso2 - 在某些情况下在反向代理后面配置 WSO2

java - 用 java 托管 RESTful Web 服务