有没有办法以编程方式为axis2客户端配置jms传输?

时间:2011-07-27 19:56:01

标签: axis2

我知道如何在axis2.xml中配置jms传输,但我需要使用可在运行时更改的特定应用程序属性来配置它。

1 个答案:

答案 0 :(得分:1)

我找到了解决方案,希望它会有所帮助

package com.mycompany.client.config;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.naming.Context;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNode;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.description.Parameter;
import org.apache.axis2.description.ParameterInclude;
import org.apache.axis2.description.TransportInDescription;
import org.apache.axis2.description.TransportOutDescription;
import org.apache.axis2.engine.AxisConfiguration;
import org.apache.axis2.transport.TransportListener;
import org.apache.axis2.transport.TransportSender;
import org.apache.axis2.transport.jms.JMSConstants;
import org.apache.axis2.transport.jms.JMSListener;
import org.apache.axis2.transport.jms.JMSSender;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mycompany.client.config.exceptions.ConfigurationException;
import com.mycompany.client.jms.JMSWebServiceClient;
import com.mycompany.stub.FtthRequestStub;

public class ConfigurationManager {

    static Log log  = LogFactory.getLog(ConfigurationManager.class);
    private static final String DEFAULT_TRANSPORT_PARAMETER_NAME = "default";   

    public JMSWebServiceClient configureJMSClient() throws Exception  {

        FtthRequestStub stub;
        ConfigurationContext ctx;

        boolean isAuthenticationEnabled = false;
        Map<String,String> jmsTransportProps = getAxisTransportConfiguration(isAuthenticationEnabled);
        String url = buildUrlFromMap(jmsTransportProps);

        ctx = ConfigurationContextFactory.createDefaultConfigurationContext();

        //transport in
        TransportListener tl = new JMSListener();
        TransportInDescription transportIn = new TransportInDescription("jms");
        addParameterToTransport(jmsTransportProps,transportIn);
        transportIn.setReceiver(tl);


        // transport out
        TransportSender transportSender = new JMSSender();
        TransportOutDescription transportOut = new TransportOutDescription("jms");
        addParameterToTransport(jmsTransportProps, transportOut);
        transportOut.setSender(transportSender);
        transportSender.init(ctx, transportOut); //very important call because transport senders are not initialized during createDefaultConfigurationContext() invocation. 

        AxisConfiguration axisConfiguration = ctx.getAxisConfiguration();
        axisConfiguration.addTransportIn(transportIn);
        axisConfiguration.addTransportOut(transportOut);

        stub = new FtthRequestStub(ctx,url);

        String replyTo = getReplyDestination();

        if(StringUtils.isNotEmpty(replyTo)) {
            stub._getServiceClient().getServiceContext().setProperty(JMSConstants.JMS_REPLY_TO, replyTo);
        }

        JMSWebServiceClient client = new JMSWebServiceClient(stub);

        return client;

    }

    private void addParameterToTransport(Map<String,String> jmsTransportProps,
            ParameterInclude transportInOut) throws ConfigurationException {

        try {
            Parameter parameter = new Parameter();
            parameter.setName(DEFAULT_TRANSPORT_PARAMETER_NAME);
            parameter.setValue(buildParameterOMElementFromMap(DEFAULT_TRANSPORT_PARAMETER_NAME, jmsTransportProps, false));
            transportInOut.addParameter(parameter);
        } catch (AxisFault e) {
            throw new ConfigurationException(e.getMessage());
        }

    }

    private static OMElement createRootParameterOMElement(String name) {

        OMFactory omFactory = OMAbstractFactory.getOMFactory();
        OMElement defaultReceiverParameter = omFactory.createOMElement(new javax.xml.namespace.QName("parameter"));
        OMAttribute nameAttribute = omFactory.createOMAttribute("name", null, name);

        defaultReceiverParameter.addAttribute(nameAttribute);
        return defaultReceiverParameter;
    }

    private static OMElement buildParameterOMElementFromMap(String rootElementName, Map<String,String> properties, boolean rootIsLocked) {

        OMFactory omFactory = OMAbstractFactory.getOMFactory();

        //creating root <parameter>
        OMElement root = omFactory.createOMElement(new javax.xml.namespace.QName("parameter"));
        OMAttribute attribute = omFactory.createOMAttribute("name", null, rootElementName);
        root.addAttribute(attribute);
        attribute = omFactory.createOMAttribute("locked", null, String.valueOf(rootIsLocked));
        root.addAttribute(attribute);

        //creating child <parameter> elements
        for(String key : properties.keySet())
        {   
            OMElement child = omFactory.createOMElement(new javax.xml.namespace.QName("parameter"));
            attribute = omFactory.createOMAttribute("name", null, key);
            child.addAttribute(attribute);
            attribute = omFactory.createOMAttribute("locked", null, Boolean.FALSE.toString());
            child.addAttribute(attribute);

            OMNode text = omFactory.createOMText(properties.get(key));
            child.addChild(text);
            root.addChild(child);
        }
        return root;

    }

    private static String buildUrlFromMap(Map<String,String> properties) {

        StringBuffer url = new StringBuffer();
        url.append(JMSConstants.JMS_PREFIX);
        url.append(getDestination()).append("?"); //send destination

        for(String key : properties.keySet()) { 
            url.append(key).append("=").append(properties.get(key)).append("&");
        }

        return StringUtils.chop(url.toString());

    }

    private static String getDestination() {
        return "testQueue";
    }

    private static String getReplyDestination() {
        return "replyQueue";
    }

    private Map<String,String> getAxisTransportConfiguration(boolean authenticationEnabled) {

        Map<String,String> properties = new HashMap<String,String>();
        properties.put(JMSConstants.PARAM_CONFAC_JNDI_NAME, "QueueConnectionFactory");
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
        properties.put(Context.PROVIDER_URL, "tcp://localhost:61616");

        if(authenticationEnabled) {
            properties.put(JMSConstants.PARAM_JMS_USERNAME, "username");
            properties.put(JMSConstants.PARAM_JMS_PASSWORD, "password");
        }

        return properties;


    }

}