abstract class field set in @Scheduled cannnot be accessed by implementation instance

43 Views Asked by At

I wrote a abstract class to manage timed activities in a game. The game uses SpringBoot and cglib proxy.

public abstract class TimedImpl extends PlanImpl {
    protected TimedPlan schedule;

    public TimedPlan getSchedule() {
        return schedule;
    }

    @Scheduled(cron = CronConst.PER_MINUTE_1)
    public final void onSchedule() {
        Plan plan = getOngoingPlans()
                .stream()
                .min(Comparator.comparingInt(Plan::getId))
                .orElse(null);
        if (schedule != null && (plan == null || plan.getId() != schedule.getId())) {
            // end
            onEnd();
            schedule = null;
        }
        if (plan != null) {
            if (schedule == null) {
                schedule = new TimedPlan(plan);
            }

            long time = TimeUtil.currentTimeMills();
            int progress = 0;
            long[] period = plan.getPeriod();
            while (progress < period.length && time >= period[progress]) progress ++;
            if (schedule.getState() == progress) {
                return;
            }

            schedule.setState(progress);
            switch (schedule.getState()) {
                case 1:
                    onPrepare();
                    break;
                case 2:
                    onBegin();
                    break;
            }
        }
    }

    protected void onPrepare() {

    }

    protected void onBegin() {

    }

    protected void onEnd() {

    }
}

I defined a KillBossServiceImpl to extends TimedImpl. This activity starts from 8:00 AM and end at 10:00 PM.

At the start of the activity, the schedule was assigned in onSchedule() and not null. But afterwards, before the activity end, each time I call the getSchedule() from member methods or onEnd() in KillBossServiceImpl, it returns null.

However, each minute upon the execution of onSchedule(), the schedule is the previous assigned value and not null.

After some google, it seems there's two diffrent instances of KillBossServiceImpl, and one is proxy instance generated by cglib. How to call the getSchedule on right instance?


Update:

It turns out that onSchdule is using the proxy instance by printing:

this.getClass().getName(); // com.xx.xx.KillBossServiceImpl$$EnhancerBySpringCGLIB$$1
0

There are 0 best solutions below