I am working on a JPOS project using Q2's ISORequestListener. When I received an ISOMsg, I want to start a backend service thread. But the backend service always return null and spring beans can not be autowired within ISORequestlistner (I am following this).
Is there anyway I can inject backend service to ThreadPool at the beginning of Spring Boot?
This is my current listener:
@Component
public class ISO8583Listener implements ISORequestListener {
@Autowired
private BackendServ backendServ ;
public ISO8583Listener() {
AutowiredAnnotationBeanPostProcessor bpp = new
AutowiredAnnotationBeanPostProcessor();
WebApplicationContext currentContext =
WebApplicationContextLocator.getCurrentWebApplicationContext();
bpp.setBeanFactory(currentContext.getAutowireCapableBeanFactory());
bpp.processInjection(this);
}
@Override
public boolean process (ISOSource source, ISOMsg m) {
try {
if ("200".equals (m.getMTI())) {
//some backend service process here
//end
m.setResponseMTI();
source.send(m);
}
} catch (ISOException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
And my configuration:
@Configuration
public class WebApplicationContextLocator implements ServletContextInitializer {
private static WebApplicationContext webApplicationContext;
public static WebApplicationContext getCurrentWebApplicationContext() {
return webApplicationContext;
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
webApplicationContext =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
}
}
One way to achieve what you are trying to do, is by registering the backendServ in the NameRegistrar and then get it in the request listener constructor. It wouldn´t be a component since it is created by the jPOS framework not the Spring framework:
package com.example.jpos_demo;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.ISORequestListener;
import org.jpos.iso.ISOSource;
import org.jpos.util.NameRegistrar;
public class DemoRequestListener implements ISORequestListener {
BackendServ serv;
public DemoRequestListener() throws NameRegistrar.NotFoundException {
serv = NameRegistrar.get("serv", 1000L); /
}
@Override
public boolean process(ISOSource isoSource, ISOMsg isoMsg) {
try {
if ("200".equals (m.getMTI())) {
//some backend service process here
//end
m.setResponseMTI();
source.send(m);
}
} catch (ISOException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
}
To register the BackendServ in NameRegistrar just write the following code next to where you instantiate your bean:
NameRegistrar.register("serv", backendServ);
One last word, this won't work, and I don't know a workaround, for spring-dev-tools, this is because of the way spring-dev-tools handles the classloader for the hot reload.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With