0

基本サービスを拡張する一連のサービスがあります。このベース サービスでは、データベースをポーリングし、その内容に基づいて通知を送信するように設計されたクラスをインスタンス化します。このポーリングのタイミングは、Spring によって処理されます。私が期待しているのは、ベース サービスを拡張するサービスごとにこのポーラーのインスタンスがあるはずですが、@Scheduled アノテーションを配置する場所によっては機能しません。

私が欲しいのはこれです:

public class Base {
    private Poller p = new Poller(this);

    // the rest of the service code
}

public class Poller{

    Base b;

    public Poller(Base B){
        b=B;
    }

    @Scheduled(fixedDelay=5000)
    public void poll(){
        //do stuff
        System.out.println(b.name); //doesn't work, causes really unhelpful errors
        System.out.println("----"); //prints as expected, but only once
                                    //regardless of how many extending services exist
    }
}

ただし、すべてのエクステンダー間で 1 つのポーラーをインスタンス化するだけのようです。次のように構成すると:

public class Base {
    private Poller p = new Poller(this);

    // the rest of the service code

    @Scheduled(fixedDelay=5000)
    public void poll(){
        p.poll();
    }
}

public class Poller{

    Base b;

    public Poller(Base B){
        b=B;
    }

    public void poll(){
        //do stuff
        System.out.println(b.name); //prints the name of the service for each extender
        System.out.println("----"); //prints as expected, once for each extender
    }
}

期待どおりに機能しますが、ここでの設計目標にはうまく適合しません。

各拡張サービスが独自のインスタンスを取得するようにしながら、スケジュールされたアノテーションをポーラーにとどめる方法はありますか?

4

1 に答える 1