My application receives messages, extracts data and persists the extracted data to a database. Data is received via a Apache Camel channel, added to a FIFO. The following code takes the next message from the FIFO and processes it. However, in order to do this it needs to get a bean from the Spring application context:
private static void dispatch(Message msg) {
if (msg == null) {
return;
}
// TODO: This really violates IoC.
DomainObjectFactory factory = (DomainObjectFactory) ApplicationContextProvider.getApplicationContext().getBean("domainObjectFactoryBean", DomainObjectFactory.class);
// do something with message
This is the service class:
@Service
public class ApplicationContextProvider implements ApplicationContextAware {
private static final Logger log = LoggerFactory.getLogger(ApplicationContextProvider.class);
private static ApplicationContext ctx = null;
public static ApplicationContext getApplicationContext() {
return getCtx();
}
@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
log.debug("Loading context");
ApplicationContextProvider.setCtx(ctx);
}
public static ApplicationContext getCtx() {
return ctx;
}
public static void setCtx(ApplicationContext ctx) {
ApplicationContextProvider.ctx = ctx;
}
}
reading a message from the FIFO:
void process(Object obj) {
Message msg = (Message) obj;
try {
Dispatcher.process(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
This is really weak code, but I can't work out how to avoid it? That is how to use Spring IoC to link the removal of a message from the FIFO to the message processing without having to retrieve the bean from the context.
Any advice / guidance appreciated