问题
I am trying to implement BeanPostProcessor to return my own JdbcPollingChannelAdapter that will help me to add some logging and know about polling activity. I have some code below, please help to complete this or if there is a better way.
Thanks
public class CustomAdapter extends JdbcPollingChannelAdapter {
private final static Logger logger = LoggerFactory.getLogger(CustomAdapter .class);
public CustomJdbcPollingChannelAdapter(DataSource dataSource, String selectQuery)
{
super(dataSource, selectQuery);
}
@Override
public Message<Object> receive()
{
Message<Object> polledData = super.receive();
if(polledData == null || polledData.getPayload() == null)
logger.info("received no data..............");
return polledData;
}
}
public class MyPostProcessor implements BeanPostProcessor {
private final static Logger logger = LoggerFactory.getLogger(MyPostProcessor .class);
public Object postProcessAfterInitialization(Object bean, String beanName)throws BeansException
{
/* not able to figure what goes here for this to work.
logger.info(bean+"..................."+beanName);
if(bean instanceof org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean)
{
//SourcePollingChannelAdapterFactoryBean bean = (SourcePollingChannelAdapterFactoryBean)bean;
//bean...??
return bean;
}
else
{
return bean;
}
*/
return bean;
}
public Object postProcessBeforeInitialization(Object bean, String beanName)throws BeansException
{
//logger.info(bean+"................................................."+beanName);
return bean;
}
}
回答1:
The JdbcPollingChannelAdapter is a MessageSource<?> and it is a component to be used from active endpoint - SourcePollingChannelAdapter, which has this code:
if (message == null) {
if (this.logger.isDebugEnabled()){
this.logger.debug("Received no Message during the poll, returning 'false'");
}
result = false;
}
else {
if (this.logger.isDebugEnabled()){
this.logger.debug("Poll resulted in Message: " + message);
}
if (holder != null) {
holder.setMessage(message);
}
this.handleMessage(message);
result = true;
}
So, isn't it enough for you to just configure org.springframework.integration.endpoint.SourcePollingChannelAdapter logging category?
From other side, there are no stops to use your custom MessageSource<?> implementation and BeanPostProcessor is really an overhead. You just need to configure your CustomAdapter as a generic <bean> and use it from another generic bean definition - SourcePollingChannelAdapterFactoryBean.
Right, with that customization you lose the Spring Integration XML power. But who said that XML is a panacea - Spring Integration Java DSL?
UPDATE
Since you have a custom MessageSource<?> you can use it directly from:
<bean id="jdbcSource" class="com.my.proj.adapter.CustomAdapter">
<constructor-arg ref="dataSource"/>
<constructor-arg value="SELECT * FROM foo"/>
<property name="updateSql" value="UPDATE foo set status=1 where id in (:id)"/>
</bean>
<int:inbound-channel-adapter ref="jdbcSource" channel="jdbcPollingChannel"/>
Any protocol-specific <*:inbound-channel-adapter> is just for convenience, but they all do them same as default one.
来源:https://stackoverflow.com/questions/24431995/how-to-add-logging-to-a-jdbc-pollar-adapter