如何将日志记录添加到 jdbc pollar 适配器 [英] How to add logging to a jdbc pollar adapter
问题描述
我正在尝试实现 BeanPostProcessor 以返回我自己的 JdbcPollingChannelAdapter,这将帮助我添加一些日志记录并了解轮询活动.我在下面有一些代码,请帮助完成这个或者如果有更好的方法.
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.
谢谢
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;
}
}
公共类 MyPostProcessor 实现了 BeanPostProcessor {
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;
}
}
推荐答案
JdbcPollingChannelAdapter
是一个 MessageSource<?
endpoint
- SourcePollingChannelAdapter
,它有这个代码:
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;
}
那么,仅仅配置 org.springframework.integration.endpoint.SourcePollingChannelAdapter
日志类别还不够吗?
So, isn't it enough for you to just configure org.springframework.integration.endpoint.SourcePollingChannelAdapter
logging category?
从另一方面来说,使用您的自定义 MessageSource>
实现是无止境的,而 BeanPostProcessor
确实是一种开销.您只需要将 CustomAdapter
配置为通用
并从另一个通用 bean 定义 - SourcePollingChannelAdapterFactoryBean
中使用它.
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
.
是的,通过这种定制,您失去了 Spring Integration XML 的功能.但谁说 XML 是万能的 - Spring 集成 Java DSL?
Right, with that customization you lose the Spring Integration XML power. But who said that XML is a panacea - Spring Integration Java DSL?
更新
因为你有一个自定义的MessageSource>
,你可以直接使用它:
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"/>
任何特定于协议的 <*:inbound-channel-adapter>
只是为了方便起见,但它们都和默认的一样.
Any protocol-specific <*:inbound-channel-adapter>
is just for convenience, but they all do them same as default one.
这篇关于如何将日志记录添加到 jdbc pollar 适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!