Skip to content
Advertisement

CDI – Unsatisfied dependencies using @Qualifer on classes that extend an abstract class and implement an interface

In Payara 5, Jakarta EE 8, I try to inject all the qualified beans and then select a specific one using a qualifier as shown right below:

@Stateless
public class ScheduledTaskExecutor {

@Inject
@Any
private Instance<QCScheduledTask> scheduledTasks;


@Asynchronous
public void executeTask(final String taskName, final String jobID) {
    final ScheduledTaskQualifier qualifier = new ScheduledTaskQualifier(taskName);
    final QCScheduledTask scheduler = scheduledTasks.select(qualifier).get();
    scheduler.execute(jobID);
}

}

public interface QCScheduledTask {

public void execute(final String jobID);

}

The abstract class which extends the interface along with an implementer:

public abstract class AbstractQCScheduledTask implements QCScheduledTask {

private String jobID;

protected abstract void executeTask();

public void execute(final String jobID) {
    //
}


protected void updateStatus(final TaskStatus status) {
   //
}

}


@Stateless
@QCScheduled(taskName = "TASK_BACKGROUND_JOB_EVALUATION")
public class BackgroundJobEvaluationExecuter extends AbstractQCScheduledTask {

@Inject
private BackgroundJobEvaluator backgroundJobEvaluator;

@Override
protected void executeTask() {

}

}

And the qualifier

    @Qualifier
    @Retention(RUNTIME)
    @Target({METHOD, FIELD, PARAMETER, TYPE})
    public @interface QCScheduled {

    /**
     * Task name discriminator
     *
     * @return
     */
     String taskName();
   
    }

The result is unsatisfied dependencies error.

The same code works on JavaEE 7 application server. I could not find any difference in the JakartaEE 8 specification, besides, I think that there should not be a restriction on using interfaces and abstract classes together for a runtime resolution of the desired bean implementer.

Advertisement

Answer

Annotate the interface QCScheduledTask with @jakarta.ejb.Local

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement