【问题标题】:spring integration JDBC DB-Poller. How to implement Row-Mapper to recieve rows to Task activatorspring 集成 JDBC DB-Poller。如何实现 Rowmapper 以接收行到任务激活器
【发布时间】:2014-06-05 15:51:35
【问题描述】:

请看链接:

Spring Integration: Inbound-channel-adapter update query parameter exception when using RowMapper

我正在做类似的配置,但不确定如何将行映射器与拆分器一起使用。

我正在尝试创建一个 db-poller 并希望使用 row-mapper 在 service-activator 中接收传入的行,但消息没有到达那里..

这是我的配置。请指教。

我的配置:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:task="http://www.springframework.org/schema/task" 
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc-3.0.xsd">

    <context:component-scan base-package="com.checkfree.isolutions" />

    <int-jdbc:inbound-channel-adapter id="invoiceInbound" query="select * from invoice where status = '0'" auto-startup="true"
      channel="invoiceInboundChannel" data-source="dataSource" row-mapper="rowMapper"
      update="update invoice set status = 'PROCESSED' where id in (:id) " max-rows-per-poll="1">
        <int:poller max-messages-per-poll="1" fixed-rate="10000">
        <int:transactional transaction-manager="transactionManager" 
                isolation="DEFAULT"
                propagation="REQUIRED" 
                read-only="false" 
                timeout="1000"/>
        </int:poller>
    </int-jdbc:inbound-channel-adapter>

    <!-- Add RowMapper tag : row-mapper="rowMapper" -->

    <bean id="rowMapper" class="com.checkfree.isolutions.InvoiceMapper"></bean>

    <int:chain input-channel="invoiceInboundChannel" output-channel="filteredChannel">
            <int:splitter ref="invoiceSplitter" />
    </int:chain>

    <int:channel id="filteredChannel" />    

    <int:service-activator id="taskActivator" input-channel="filteredChannel" ref="taskCreator"  method="processInvoice">
    </int:service-activator>


</beans>

这是我的服务激活器:

@Component("taskCreator")
public class TaskCreator {

    static Logger logger = Logger.getLogger(TaskCreator.class.getName());

    @Autowired
    InvoiceService invoiceService;

    @Transactional(readOnly = false)
    public void processInvoice(Object invoiceObj) throws Exception {
        System.out.println("row received.." + invoiceObj.getClass().getCanonicalName());
    }
}

我无法在控制台中看到打印消息“row received..”。

但在调试 rowmapper 中,每次调用都会调用 mapRow 方法。

public class InvoiceMapper implements RowMapper {

    @Override
    public Invoice mapRow(ResultSet rs, int rowNum) throws SQLException {

        Invoice invoice = new Invoice();
         --
         --
    }
}

我想要实现的是我的 TaskCreator 应该以行映射器 (InvoiceMapper) 格式接收传入的表行。

我刚刚更正了我的配置。

当我打开调试时,我可以看到控制权转到 RowMapper。

问题:我不知道如何将 row-mapper 与 spiltter 一起使用。谁能举个例子..

你能不能给点建议。提前谢谢..

问候,希夫

【问题讨论】:

  • invoiceSplitter 看起来像什么?打开 DEBUG 日志记录并通过渠道 (preSend, postSend) 跟踪消息。
  • 加里,感谢您的回复。我已经更正了我的配置。我可以使用 invoicesplitter 接收消息(如果我删除 row-mapper 属性)。当我添加行映射器属性时,不会调用 processInvoice。在调试模式下,我可以看到控制转到行映射器类,但控制永远不会转到 TastCreate(服务激活器)类。所以如果我减去行映射器条目,那么我的代码工作得很好。但是当我添加一些 JDBC 特定转换所需的行映射器时。然后控制永远不会转到服务激活器类。

标签: java spring spring-integration spring-jdbc


【解决方案1】:

这里有两个问题:

  1. 没有理由使用&lt;splitter&gt;,如果您希望只有一行 - max-rows-per-poll="1"

  2. invoiceInboundChannel - &lt;chain&gt; 有两个订阅者,&lt;splitter&gt;&lt;service-activator&gt;。由于RoundRobinLoadBalancingStrategy

  3. ,它可以正常工作,但与您预期的不同

那么,也许您的目标是将filteredChannel 作为&lt;service-activator&gt; 的输入?

【讨论】:

  • 嗨 修复了这个问题并使用过滤通道作为服务激活器的输入。我的代码在拆分器上运行良好。 (不使用行映射器)。但是当我添加属性行映射器时,我在服务激活器中没有收到任何东西。我的问题:如何使用带有拆分器的行映射器在服务激活器类中接收消息。
  • 好吧,打开org.springframework.integration 类别的调试,看看在行映射器之后你的消息发生了什么。对我来说,你的行映射器没有返回任何东西,或者你做错了。或者有一些异常,但是你不注意……
  • 谢谢阿特姆。 Eclipse 调试有助于找出我得到的真正异常。请在bottem中查看最终答案。
【解决方案2】:

我假设您的“invoiceInboundChannel”频道是DirectChannel,根据定义,它为每个发送的消息调用一个订阅者。

您已为该频道配置了多个订阅者(链和服务激活器)。这就是你面临这个问题的原因。

这里是解决方案。将service-activatorinput-channel改成filteredChannel:

<int:service-activator id="taskActivator" input-channel="filteredChannel" ref="taskCreator"  method="processInvoice">
</int:service-activator>

希望您将根据您的拆分器 (invoiceSplitter) 实现将拆分的消息接收到您的服务激活器 (taskActivator)。

【讨论】:

  • 谢谢阿桑。我解决了。那。(如果我在配置中删除行映射器属性)。但是当我添加行映射器属性时,不会调用 processInvoice。在调试模式下,我可以看到控制转到行映射器类,但控制永远不会转到 TastCreate(服务激活器)类。所以如果我减去行映射器条目,那么我的代码工作得很好。但是当我添加一些 JDBC 特定转换所需的行映射器时。然后控制永远不会进入服务激活器类。所以问题是如何在这个场景中使用行映射器来接收服务激活器类中的行。
【解决方案3】:

谢谢我的问题解决了。

问题不在于配置。但问题是我在原始发票表中的主键为 RAW 类型(加密格式),而我的发票类显示与 Long 相同的字段。因此,在运行更新时,我得到了“ORA-00932:不一致的数据类型:预期的 BINARY 得到了 NUMBER”。但是调试后我可以弄清楚这一点。

感谢大家的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    • 2013-01-08
    • 2018-10-14
    • 1970-01-01
    • 2017-01-30
    相关资源
    最近更新 更多