在事务中发送CXF JMS消息

时间:2018-10-18 12:51:10

标签: soap transactions jms cxf

我正在使用CXF 3.2.6和Spring 5.1.0创建使用JaxWsProxyFactoryBean调用HTTP的HTTP SOAP Web服务。 (单向)基于SOAP JMS的Web服务。 如果(本地)webservice方法中发生异常,我希望不发送消息。所以我想 在提交或回滚发送JMS消息的事务中运行的方法。

我尝试了几种注入交易行为的方法 我将事务注释添加到MyHttpWebServiceImpl并定义了JmsTransactionManager bean, 创建一个设置事务管理器的JMSConfiguration,并将功能添加到端点。 但是,这没有效果。 setTransactionManager也已弃用,但未指定我应该做什么 代替。 不幸的是,ActiveMQConnectionFactory也没有设置事务管理器的方法。 我发现很少有文档介绍Spring事务和CXF应该如何一起使用。

没有交易支持的示例代码:

@Service
public class MyHttpWebServiceImpl implements MyHttpWebService {
    private MyJmsWebService client;

    public void set(MyJmsWebService client) {
        this.client = client;
    }

    public MyResponse update(MyRequest request) {
        client.update(request);
        throw new RuntimeException("test rollback"); //doesn't work
    }    
}

@Configuration
public class MyConfiguration {
    // JMS webservice endpoint configuration ommitted
    private MyHttpWebService webService;

    @AutoWired
    public void setWebService(MyHttpWebService webService) {
        this.webService = webService;
    }

    @Bean
    public ConnectionFactory getConnectionFactory() {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
        factory.setBrokerURL("tcp://localhost:61616");
        return factory;
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public Endpoint httpSoapEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), webService);
        endpoint.publish("/myws");
        return endpoint;            
    }

    @Bean
    public MyJmsWebService client() {
        return (MyJmsWebService) proxyFactoryBean().create();
    }

    @Bean
    public JaxWsProxyFactoryBean proxyFactoryBean() {
        String address = "jms:queue:MyQueue?sessionTransacted=true";
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setFeatures(Collections.singletonList(new ConnectionFactoryFeature(connectionFactory)));
        factory.setTransportId(JMSSpecConstants.SOAP_JMS_SPECIFICATION_TRANSPORTID);
        factory.setServiceClass(MyJmsWebService.class);
        factory.setAddress(address);
        return factory;
    }
}

0 个答案:

没有答案
相关问题