如何在CXF生成的客户端中以编程方式更改JMS主题?

时间:2012-02-01 12:15:30

标签: wsdl jms cxf activemq

我有WSDL文件,其中包含:

    <wsdl:port name="NotificationConsumerSoapJms"
        binding="tns_NotificationConsumer:NotificationConsumerSoapJmsBinding">
        <jms:address destinationStyle="topic"
            jndiConnectionFactoryName="ConnectionFactory"
            jndiDestinationName="dynamicTopics/test.DefaultTopic">

            <jms:JMSNamingProperty name="java.naming.factory.initial"
                value="org.apache.activemq.jndi.ActiveMQInitialContextFactory" />
            <jms:JMSNamingProperty name="java.naming.provider.url"
                value="tcp://localhost:61616" />
        </jms:address>
    </wsdl:port>

这是由CXF生成的JMS客户端:

    URL wsdlURL = EfaNbiWebService.WSDL_LOCATION;
    if (args.length > 0 && args[0] != null && !"".equals(args[0])) { 
        File wsdlFile = new File(args[0]);
        try {
            if (wsdlFile.exists()) {
                wsdlURL = wsdlFile.toURI().toURL();
            } else {
                wsdlURL = new URL(args[0]);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    EfaNbiWebService ss = new EfaNbiWebService(wsdlURL, SERVICE_NAME);
    NotificationConsumer port = ss.getNotificationConsumerSoapJms();  


    {
    System.out.println("Invoking notify...");
    org.tmforum.mtop.fmw.xsd.hdr.v1.Header _notify_mtopHeader = null;
    org.tmforum.mtop.fmw.xsd.notmsg.v1.Notify _notify_mtopBody = null;
    port.notify(_notify_mtopHeader, _notify_mtopBody);


    }

如您所见,主题名称存储在WSDL文件中。那么如何以编程方式更改主题名称?

1 个答案:

答案 0 :(得分:1)

我找到了解决方案:

    JMSConfiguration jmsConfig = new JMSConfiguration();

    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://" + activeMQServer + ":61616");
    jmsConfig.setConnectionFactory(connectionFactory);
    jmsConfig.setTargetDestination("dynamicTopics/Test.Notification_" + topic);
    jmsConfig.setReconnectOnException(false);
    //jmsConfig.se
    jmsConfig.setPubSubDomain(true);
    jmsConfig.setUseJms11(true);
    jmsConfig.setTransactionManager(new JmsTransactionManager(connectionFactory));

    JMSConfigFeature jmsConfigFeature = new JMSConfigFeature();
    jmsConfigFeature.setJmsConfig(jmsConfig);

    String address = "jms:jndi:dynamicTopics/Test.Notification_" + topic + "?jndiInitialContextFactory=org.apache.activemq.jndi.ActiveMQInitialContextFactory&jndiConnectionFactoryName=ConnectionFactory&jndiURL=tcp://" + activeMQServer + ":61616";
    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    // And specify the transport ID with SOAP over JMS specification
    factory.setTransportId(JMSSpecConstants.SOAP_JMS_SPECIFICATION_TRANSPORTID);
    factory.setServiceClass(NotificationConsumer.class);
    factory.getFeatures().add(jmsConfigFeature);
    factory.setAddress(address);
    NotificationConsumer client = (NotificationConsumer)factory.create();

祝你有个美好的一天,伙计们:)