Cant serialize the Job class while Scheduling due to bean injection

45 Views Asked by At

I am trying to schedule an sms job using https://github.com/kagkarlsson/db-scheduler but when I am trying to serialize my job class it is throwing an error with both GsonSerializer and JacksonSeralizer. I think this is happening because I am injecting beans in this class which the serializer is not able to serialize. But I cant declare them transient also as I need them in the execution of the job. Suggest a workaround for this.Here is the code for my class:

public class SmsEscalationJob extends TaskSchedulerJob implements Serializable {
    private final String eventId;
    private String escalationLevel;
    private final SmsService smsService;
    private final SosEscalationMatrixDTO escalationMatrix;
    private final EventDao eventDao;
    private final SchedulerService schedulerService;

    public SmsEscalationJob(
            SosEscalationMatrixDTO escalationMatrix,
            SchedulerService schedulerService,
            EventDao eventDao,
            String eventId,
            String escalationLevel,
            SmsService smsService) {
        this.escalationMatrix = escalationMatrix;
        this.schedulerService = schedulerService;
        this.eventDao = eventDao;
        this.eventId = eventId;
        this.escalationLevel = escalationLevel;
        this.smsService = smsService;
    }

    @Override
    public TaskSchedulerType getTaskSchedulerType() {
        return TaskSchedulerType.SMS_ESCALATION_TASK_SCHEDULER;
    }

    @Override
    public void run() {
        if (Strings.isNullOrEmpty(escalationLevel) || escalationLevel.equals("L3")) {
            log.error("Error while sending sms no escalation level available after L3");
            return;
        }
        Optional<EventEntity> eventEntity = eventDao.getByEventId(eventId);
        if (eventEntity.isEmpty()) {
            log.info("Cannot schedule Sms Escalation Job since event is null for event id {}", eventId);
            return;
        }
        EventDTO event = EventMapper.INSTANCE.entityToDTO(eventEntity.get());
        log.info("Initiating SMS Escalation Job for buId {} with event id {} and escalation {}", event.getBuId(), event.getEventId(), escalationLevel);
        smsService.sendSms(event, escalationMatrix);
        long time = -1;
        String escalationLevelPhoneNumber = null;
        switch (escalationLevel) {
            case "L1":
                time = Long.parseLong(escalationMatrix.getL2EscalationInterval());
                escalationLevelPhoneNumber = escalationMatrix.getL2EscalationPhoneNumber();
                escalationLevel = "L2";
                break;
            case "L2":
                time = Long.parseLong(escalationMatrix.getL3EscalationInterval());
                escalationLevelPhoneNumber = escalationMatrix.getL3EscalationPhoneNumber();
                escalationLevel = "L3";
                break;
            default:
                log.error("Error while sending sms no escalation level available after L3");
                break;
        }
        if (time == -1 || Strings.isNullOrEmpty(escalationLevelPhoneNumber)) {
            log.error("Escalation time {} or escalation phone number {} not set for event {}", time, escalationLevelPhoneNumber, event.getEventId());
            return;
        }
        SmsEscalationJob smsEscalationJob = new SmsEscalationJob(escalationMatrix, schedulerService, eventDao, event.getEventId(), escalationLevel, smsService);
        schedulerService.scheduleOneTimeJob(TaskSchedulerType.SMS_ESCALATION_TASK_SCHEDULER, smsEscalationJob, ChronoUnit.SECONDS, time);
        smsService.saveEventWithUpdatedEscalation(event.getEventId(), escalationLevel);
    }
}

Here is the exception:

java.lang.reflect.InaccessibleObjectException: Unable to make protected java.security.cert.X509Certificate() accessible: module java.base does not "opens java.security.cert" to unnamed module @f2ff811
    at java.base/java.lang.reflect.AccessibleObject.throwInaccessibleObjectException(AccessibleObject.java:391) ~[na:na]
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:367) ~[na:na]
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:315) ~[na:na]
    at java.base/java.lang.reflect.Constructor.checkCanSetAccessible(Constructor.java:194) ~[na:na]
    at java.base/java.lang.reflect.Constructor.setAccessible(Constructor.java:187) ~[na:an]

0

There are 0 best solutions below