イベントジェネリック型に基づいてイベント処理を構築したいと考えています。
イベント クラス:
public class PushNotificationStoreEvent<T extends Push> extends ApplicationEvent {
private static final long serialVersionUID = -3791059956099310853L;
public T push;
public PushNotificationStoreEvent(Object source, T push) {
super(source);
this.push = push;
}
}
Pushクラスのマーカー インターフェイスは次のPushNotificationNewとおりですPushNotificationFail。
リスナー:
@Async
@EventListener
public void storeNewPush(PushNotificationStoreEvent<PushNotificationNew> event) {
pushNewRepoService.save(event.push);
}
@Async
@EventListener
public void storeFailPush(PushNotificationStoreEvent<PushNotificationFail> event) {
pushFailRepoService.save(event.push);
}
問題:リスナーのPushNotificationStoreEvent<PushNotificationNew>との区別がありません。PushNotificationStoreEvent<PushNotificationFail>
つまり、すべてのイベントPushNotificationStoreEventがすべてのリスナーによって処理されます。
ケースごとに個別のイベント クラスを作成するか、1 つのリスナーを使用instanceofしevent.pushて適切なアクションを選択することで、これを解決できます。多分あなたはより良い解決策を知っていますか?