如何添加记录到一个JDBC POLLAR适配器(How to add logging to a jd

2019-10-20 09:01发布

我想实现的BeanPostProcessor回到我自己的JdbcPollingChannelAdapter,这将有助于我添加一些记录和了解投票活动。 我有一些代码如下,请帮忙完成这个或是否有更好的办法。

谢谢

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 {

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;
}

}

Answer 1:

所述JdbcPollingChannelAdapter是一个MessageSource<?> ,它是将被从活动使用的组件endpoint - SourcePollingChannelAdapter ,其具有这样的代码:

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日志记录类别?

来自对岸,有没有停下来使用自定义MessageSource<?>实施和BeanPostProcessor是一个真正的开销。 你只需要配置CustomAdapter作为一种通用<bean>和其他普通的bean定义使用它- SourcePollingChannelAdapterFactoryBean

右,与定制你失去了Spring集成XML动力。 但谁说,XML是万能的- Spring集成Java的DSL ?

UPDATE

既然你有一个自定义的MessageSource<?>你可以直接使用它:

<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>仅仅是为了方便,但他们都做他们一样的默认值。



文章来源: How to add logging to a jdbc pollar adapter