Spring boot Spring集成DSL-JdbcPollingChannelAdapter结果未排队

Spring boot Spring集成DSL-JdbcPollingChannelAdapter结果未排队,spring-boot,spring-integration,spring-integration-dsl,Spring Boot,Spring Integration,Spring Integration Dsl,我发誓我已经做到了,但是当我在几个月后(以及升级到Boot 1.5.9)能够回到它的时候,我遇到了一些问题 我设置了一个JdbcPollingChannelAdapter,可以很好地执行receive(),但是当我将适配器放入一个只对适配器结果进行排队的流中时,队列上的running.receive总是返回null(不过我可以在控制台日志中看到适配器的SQL正在执行) 测试如下。为什么我可以从适配器获取结果,但不能将结果排队?提前感谢您的帮助 @RunWith(SpringRunner.clas

我发誓我已经做到了,但是当我在几个月后(以及升级到Boot 1.5.9)能够回到它的时候,我遇到了一些问题

我设置了一个JdbcPollingChannelAdapter,可以很好地执行receive(),但是当我将适配器放入一个只对适配器结果进行排队的流中时,队列上的running.receive总是返回null(不过我可以在控制台日志中看到适配器的SQL正在执行)

测试如下。为什么我可以从适配器获取结果,但不能将结果排队?提前感谢您的帮助

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureTestDatabase
@JdbcTest
public class JdbcpollingchanneladapterdemoTests {

  @Autowired
  @Qualifier("dataSource")
  DataSource dataSource;

  private static PollableChannel outputQueue;

    @BeforeClass
    public static void setupClass() {
    outputQueue = MessageChannels.queue().get();
        return;
    }

    @Test
    public void Should_HaveQueue() {
        assertThat(outputQueue, instanceOf(QueueChannel.class));
    }

    @Test
  @Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD,
      statements = "Create Table DEMO (CODE VARCHAR(5));")
  @Sql(executionPhase = ExecutionPhase.AFTER_TEST_METHOD,
      statements = "Drop Table DEMO ;")
    public void Should_Not_HaveMessageOnTheQueue_When_No_DemosAreInTheDatabase() {
        Message<?> message = outputQueue.receive(5000);
        assertThat(message, nullValue()) ;
    }

  @Test
  @Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD,
      statements = "Create Table DEMO (CODE VARCHAR(5));")
  @Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD,
      statements = "Insert into DEMO (CODE) VALUES ('12345');")
  @Sql(executionPhase = ExecutionPhase.AFTER_TEST_METHOD,
      statements = "Drop Table DEMO ;")
  public void Should_HaveMessageOnTheQueue_When_DemosIsInTheDatabase() {
    assertThat(outputQueue, instanceOf(QueueChannel.class));
    Message<?> message = outputQueue.receive(5000);
    assertThat(message, notNullValue());
    assertThat(message.getPayload().toString(), equalTo("15317")) ;
  }

  @Test
  @Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD,
      statements = "Create Table DEMO (CODE VARCHAR(5));")
  @Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD,
      statements = "Insert into DEMO (CODE) VALUES ('12345');")
  @Sql(executionPhase = ExecutionPhase.AFTER_TEST_METHOD,
      statements = "Drop Table DEMO ;")
  public void get_message_directly_from_adapter() {
    JdbcPollingChannelAdapter adapter =
        new JdbcPollingChannelAdapter(dataSource, "SELECT CODE FROM DEMO");
    adapter.setRowMapper(new DemoRowMapper());
    adapter.setMaxRowsPerPoll(1);
    Message<?> message = adapter.receive();
    assertThat(message, notNullValue());
  }


  private static class Demo {

    private String demo;

    String getDemo() {
      return demo;
    }

    void setDemo(String value) {
      this.demo = value;
    }

    @Override
    public String toString() {
      return "Demo [value=" + this.demo + "]";
    }
  }

  public static class DemoRowMapper implements RowMapper<Demo> {

    @Override
    public Demo mapRow(ResultSet rs, int rowNum) throws SQLException {
      Demo demo = new Demo();
      demo.setDemo(rs.getString("CODE"));
      return demo;
    }
  }

  @Component
  public static class MyFlowAdapter extends IntegrationFlowAdapter {

    @Autowired
    @Qualifier("dataSource")
    DataSource dataSource;

    @Override
    protected IntegrationFlowDefinition<?> buildFlow() {

      JdbcPollingChannelAdapter adapter =
          new JdbcPollingChannelAdapter(dataSource, "SELECT CODE FROM DEMO");
      adapter.setRowMapper(new DemoRowMapper());
      adapter.setMaxRowsPerPoll(1);

      return from(adapter,
          c -> c.poller(Pollers.fixedRate(1000L, 2000L)
              .maxMessagesPerPoll(1)
              .get()))
          .channel(outputQueue);
    }
  }
}
@RunWith(SpringRunner.class)
@春靴测试
@自动配置测试数据库
@jdbc测试
公共类JdbcpollingchanneladapterdemoTests{
@自动连线
@限定符(“数据源”)
数据源数据源;
专用静态可轮询通道输出队列;
@课前
公共静态void setupClass(){
outputQueue=MessageChannels.queue().get();
返回;
}
@试验
public void应_HaveQueue(){
断言(outputQueue,instanceOf(QueueChannel.class));
}
@试验
@Sql(executionPhase=executionPhase.BEFORE\u TEST\u方法,
语句=“创建表演示(代码VARCHAR(5));”)
@Sql(executionPhase=executionPhase.AFTER\u TEST\u方法,
语句=“Drop Table DEMO;”)
public void在数据库()中没有演示时不应在其上有消息{
Message Message=outputQueue.receive(5000);
断言(消息,nullValue());
}
@试验
@Sql(executionPhase=executionPhase.BEFORE\u TEST\u方法,
语句=“创建表演示(代码VARCHAR(5));”)
@Sql(executionPhase=executionPhase.BEFORE\u TEST\u方法,
语句=“插入演示(代码)值('12345');”)
@Sql(executionPhase=executionPhase.AFTER\u TEST\u方法,
语句=“Drop Table DEMO;”)
当在数据库()中演示时,public void应在其上显示消息{
断言(outputQueue,instanceOf(QueueChannel.class));
Message Message=outputQueue.receive(5000);
断言(消息,notNullValue());
断言(message.getPayload().toString(),equalTo(“15317”);
}
@试验
@Sql(executionPhase=executionPhase.BEFORE\u TEST\u方法,
语句=“创建表演示(代码VARCHAR(5));”)
@Sql(executionPhase=executionPhase.BEFORE\u TEST\u方法,
语句=“插入演示(代码)值('12345');”)
@Sql(executionPhase=executionPhase.AFTER\u TEST\u方法,
语句=“Drop Table DEMO;”)
public void直接从适配器()获取消息{
JdbcPollingChannelAdapter适配器=
新的JdbcPollingChannelAdapter(数据源,“从演示中选择代码”);
setRowMapper(新的DemoRowMapper());
适配器.setMaxRowsPerPoll(1);
Message=adapter.receive();
断言(消息,notNullValue());
}
私有静态类演示{
私有字符串演示;
字符串getDemo(){
返回演示;
}
void setDemo(字符串值){
this.demo=值;
}
@凌驾
公共字符串toString(){
返回“Demo[value=“+this.Demo+”]”;
}
}
公共静态类DemoRowMapper实现了RowMapper{
@凌驾
公共演示mapRow(ResultSet rs,int rowNum)抛出SQLException{
Demo=newdemo();
setDemo(rs.getString(“代码”);
返回演示;
}
}
@组成部分
公共静态类MyFlowAdapter扩展了IntegrationFlowAdapter{
@自动连线
@限定符(“数据源”)
数据源数据源;
@凌驾
受保护的集成流定义buildFlow(){
JdbcPollingChannelAdapter适配器=
新的JdbcPollingChannelAdapter(数据源,“从演示中选择代码”);
setRowMapper(新的DemoRowMapper());
适配器.setMaxRowsPerPoll(1);
从(适配器、,
c->c.轮询器(固定轮询器(1000L,2000L)
.maxMessagesPerPoll(1)
.get())
.通道(输出队列);
}
}
}
编辑我已经尽可能地简化了它,重构为下面的代码。测试通过一个具有通用消息源的流,并在具有JdbcPollingChannelAdapter消息源的流上失败。我只是不清楚应该如何配置第二个消息源,以便它像第一个消息源一样成功

  @Test
  @Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD,
      statements = "Create Table DEMO (CODE VARCHAR(5));")
  @Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD,
      statements = "Insert into DEMO (CODE) VALUES ('12345');")
  public void Should_HaveMessageOnTheQueue_When_UnsentDemosIsInTheDatabase() {

this.genericFlowContext.registration(new GenericFlowAdapter()).register();

PollableChannel genericChannel = this.beanFactory.getBean("GenericFlowAdapterOutput",
    PollableChannel.class);

this.jdbcPollingFlowContext.registration(new JdbcPollingFlowAdapter()).register();

PollableChannel jdbcPollingChannel = this.beanFactory.getBean("JdbcPollingFlowAdapterOutput",
    PollableChannel.class);

assertThat(genericChannel.receive(5000).getPayload(), equalTo("15317"));

assertThat(jdbcPollingChannel.receive(5000).getPayload(), equalTo("15317"));
  }

  private static class GenericFlowAdapter extends IntegrationFlowAdapter {

@Override
protected IntegrationFlowDefinition<?> buildFlow() {
  return from(getObjectMessageSource(),
      e -> e.poller(Pollers.fixedRate(100)))
      .channel(c -> c.queue("GenericFlowAdapterOutput"));
}

private MessageSource<Object> getObjectMessageSource() {
  return () -> new GenericMessage<>("15317");
}
}

private static class JdbcPollingFlowAdapter extends IntegrationFlowAdapter {

@Autowired
@Qualifier("dataSource")
DataSource dataSource;

@Override
protected IntegrationFlowDefinition<?> buildFlow() {
  return from(getObjectMessageSource(),
      e -> e.poller(Pollers.fixedRate(100)))
      .channel(c -> c.queue("JdbcPollingFlowAdapterOutput"));
}

private MessageSource<Object> getObjectMessageSource() {
  JdbcPollingChannelAdapter adapter =
      new JdbcPollingChannelAdapter(dataSource, "SELECT CODE FROM DEMO");
  adapter.setRowMapper(new DemoRowMapper());
  adapter.setMaxRowsPerPoll(1);
  return adapter;
}
  }
@测试
@Sql(executionPhase=executionPhase.BEFORE\u TEST\u方法,
语句=“创建表演示(代码VARCHAR(5));”)
@Sql(executionPhase=executionPhase.BEFORE\u TEST\u方法,
语句=“插入演示(代码)值('12345');”)
当在数据库()中未设置演示时,public void应在其上显示消息{
这个.genericFlowContext.registration(新的GenericFlowAdapter()).register();
PollableChannel genericChannel=this.beanFactory.getBean(“GenericFlowAdapterOutput”,
PollableChannel.class);
这个.jdbcPollingFlowContext.registration(新的JdbcPollingFlowAdapter()).register();
PollableChannel jdbcPollingChannel=this.beanFactory.getBean(“JdbcPollingFlowAdapterOutput”,
PollableChannel.class);
资产(genericChannel.receive(5000.getPayload(),equalTo(“15317”));
资产(jdbcPollingChannel.receive(5000.getPayload(),equalTo(“15317”));
}
私有静态类GenericFlowAdapter扩展了IntegrationFlowAdapter{
@凌驾
受保护的集成流定义buildFlow(){
从(getObjectMessageSource())返回,
e->e.poller(poller.fixedRate(100)))
.channel(c->c.queue(“GenericFlowAdapterOutput”);
}
私有消息源getObjectMessageSource(){
return()->newgenericmessage(“15317”);
}
}
私有静态类JdbcPollingFlowAdapter扩展了IntegrationFlowAdapter{
@自动连线
@限定符(“数据源”)
数据源数据源;
@凌驾
受保护的集成流定义buildFlow(){
从(getObjectMessageSource())返回,
e->e.poller(poller.fixedRate(100)))
.channel(c->c.queue(“JdbcPollingFlowAdapterOutput”);
}
私有消息源getObjectMes
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD,
      statements = "Insert into DEMO (CODE) VALUES ('12345');",
      config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED))