Spring 异步方法不适用于 EndPoint
Spring Async method not working with EndPoint
我的 Async 实现无法正常工作,但似乎我已正确完成所有操作。
我正在从端点调用我的异步方法 class。
请看下面我的代码,协助我解决问题。
我的端点class如下:
@Endpoint
public class EndpointDummy {
private static final String TARGET_NAMESPACE = "http://www.abc.xyz.com/GetAcctInfo";
@Autowired
private DummyService service;
@PayloadRoot(localPart = "GetAcctInfoRq", namespace = TARGET_NAMESPACE)
public @ResponsePayload GetAcctInfoRs handleRequest(@RequestPayload GetAcctInfoRq request, MessageContext messageContext) throws JAXBException, TransformerException {
/*****************************************************************
* Call the handler function
* This handler function is asynchronous
*****************************************************************/
service.handleRequest();
/*****************************************************************
* Send response back
*****************************************************************/
SaajSoapMessage soapResponse = (SaajSoapMessage) messageContext.getResponse();
SoapHeader respheader = soapResponse.getSoapHeader();
MsgHdrRs msgHdrRs = new MsgHdrRs();
JAXBContext jaxbContext = JAXBContext.newInstance(MsgHdrRs.class);
jaxbContext.createMarshaller().marshal(msgHdrRs, respheader.getResult());
GetAcctInfoRs response = new GetAcctInfoRs();
return response;
}
}
正如您在上面看到的,我的 EndPoint class 调用了 DummyService 方法 handleRequest。
Dummy Service class 用@Service 注释,方法 handRequest 用@Async 注释,如下所示:
@Service
public class DummyService {
private Logger logger = Logger.getLogger(DummyService.class);
@Async("taskExecutorServiceImpl")
public void handleRequest() {
logger.info("DummyService: 1");
try {
Thread.sleep(20000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
logger.info("DummyService: 2");
}
}
我还定义了我的配置 class 如下:
@Configuration
@EnableAsync
public class ThreadConfig {
@Bean(name = "taskExecutorServiceImpl")
public ThreadPoolTaskExecutor specificTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.initialize();
return executor;
}
}
请帮助我解决问题。我是 Spring 框架的新手,非常感谢这方面的任何帮助。
谢谢
问题已解决,与上述代码无关。
问题出在配置文件中,与上面正确的代码无关。
我的 Async 实现无法正常工作,但似乎我已正确完成所有操作。
我正在从端点调用我的异步方法 class。
请看下面我的代码,协助我解决问题。
我的端点class如下:
@Endpoint
public class EndpointDummy {
private static final String TARGET_NAMESPACE = "http://www.abc.xyz.com/GetAcctInfo";
@Autowired
private DummyService service;
@PayloadRoot(localPart = "GetAcctInfoRq", namespace = TARGET_NAMESPACE)
public @ResponsePayload GetAcctInfoRs handleRequest(@RequestPayload GetAcctInfoRq request, MessageContext messageContext) throws JAXBException, TransformerException {
/*****************************************************************
* Call the handler function
* This handler function is asynchronous
*****************************************************************/
service.handleRequest();
/*****************************************************************
* Send response back
*****************************************************************/
SaajSoapMessage soapResponse = (SaajSoapMessage) messageContext.getResponse();
SoapHeader respheader = soapResponse.getSoapHeader();
MsgHdrRs msgHdrRs = new MsgHdrRs();
JAXBContext jaxbContext = JAXBContext.newInstance(MsgHdrRs.class);
jaxbContext.createMarshaller().marshal(msgHdrRs, respheader.getResult());
GetAcctInfoRs response = new GetAcctInfoRs();
return response;
}
}
正如您在上面看到的,我的 EndPoint class 调用了 DummyService 方法 handleRequest。
Dummy Service class 用@Service 注释,方法 handRequest 用@Async 注释,如下所示:
@Service
public class DummyService {
private Logger logger = Logger.getLogger(DummyService.class);
@Async("taskExecutorServiceImpl")
public void handleRequest() {
logger.info("DummyService: 1");
try {
Thread.sleep(20000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
logger.info("DummyService: 2");
}
}
我还定义了我的配置 class 如下:
@Configuration
@EnableAsync
public class ThreadConfig {
@Bean(name = "taskExecutorServiceImpl")
public ThreadPoolTaskExecutor specificTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.initialize();
return executor;
}
}
请帮助我解决问题。我是 Spring 框架的新手,非常感谢这方面的任何帮助。
谢谢
问题已解决,与上述代码无关。 问题出在配置文件中,与上面正确的代码无关。