简体   繁体   中英

run common query for all derived DAO class on schedule

I have an abstract class that contains common methods:

public abstract class AbstractDAO<T> implements IFindTradeDAO {


    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public List<Trade> findOne(final int eventId) {
        return jdbcTemplate.query(getOneQuery(), new Object[]{eventId}, new RowMapper());
    }

    @Override
    public List<Event> getAll() {
        return jdbcTemplate.queryForList(getAllQuery(), Event.class);
    }


    protected abstract String getOneQuery();

    protected abstract String getAllQuery();

}

The derived class which provides type specific queries:

@Repository
public class TypeA extends AbstractDAO implements IFindTradeDAO {

    private static final String FIND = "sql/findA.sql";

    private static final String GET = "sql/getA.sql";


    @Override
    protected String getOneQuery() {
        return FIND;
    }

    @Override
    protected String getAllQuery() {
        return GET;
}

@Repository
public class TypeB extends AbstractDAO implements IFindTradeDAO {

    private static final String FIND = "sql/findB.sql";

    private static final String GET = "sql/getB.sql";


    @Override
    protected String getOneQuery() {
        return FIND;
    }

    @Override
    protected String getAllQuery() {

    }

}

Given I have this class structure, I want to invoke the getAllQuery() for all the derived class sql eg for TypeA and TypeB on schedule using

org.springframework.scheduling.annotation.Scheduled;

   @Component
    public class GetAllEvents {

        @Scheduled
        public void processUnprocessedEvents() {
           //invoke getAllQuery for all DAOS and combine results

        }

    }

I am not quite sure how I can derive a Component that will invoke all? Is there technique or example I can use to achieve this?

I'm not a Java programmer, but in .NET you'd need to put them all into some type of list or array, and call the function on each item.

So maybe create a method called Add(IFindTradeDAO dao) which adds each one to a list. And then call the method on each of those items.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM