bekwam courses

WildFly Scheduler Auth

November 12, 2020

This article demonstrates how to enabled a JavaEE scheduled operation to access a Role Based Access Control (RBAC) enabled EJB. The RolesAllowed annotation is used to secure the EJB. A scheduled operation uses the EJB and is outfitted with the RunAs annotation. Unlike the RESTful web service interacting with the EJB, there is no user identity coming through the authentication subsystem.

The JavaEE code described this article is on GitHub.

The demonstration is a form processor. An initial RESTful web service POST saves a form. A process later awakens and processes the form. This is a pattern used when the data intake stage of an application is separated for performance or other reasons from processing. For instance, the data intake can be quick insert into an RDBMS which is followed up with a process stage that can include time consuming operations. The operation can even include a manual review.

The following UML class diagram shows the classes involved in the demonstration. FormResource is a JAX-RS endpoint that receives POST-ed form data and provides a GET that will verify the submitted forms. FormBean is a data access object that interacts with an in-memory datasource that ships with WildFly "H2". ProcessorBean has a scheduled operation that will use FormBean to mark any received forms as "PROCESSED" and also applies a timestamp.

UML Class Diagram
RESTful API and Scheduled Access to an EJB

In this sequence diagram, the flow starts with a POST of a form. The form is saved to the in-memory database by FormBean's create() call. Every two minutes, the ProcessorBean calls FormBean. FormBean checks for any RECEIVED forms. If any are found, their state is automatically changed to PROCESSED.

UML Sequence Diagram
Sequence of Calls Verifying Solution


Java Persistence Architecture (JPA) is not a focus for this article, but a few notes are in order. Firstly, H2 is an in-memory datasource that ships with WildFly. So for this demo, there are no special management console operations. Secondly, we're relying on JPA to create the DB objects automatically.

In GitHub, look for the src/main/resources/META-INF folder's persistence.xml. It relates the WAR file to the out-of-the-box H2 datasource.

"Form" is a JPA entity with fields for id, data, and processing. It's purpose in the demo is to show the data intake stage which results in a Form record being written out with the submitted data, a generated id, a RECEIVED state, and a receivedOn timestamp set. Later processing will update the state to PROCESSED and write out a second timestamp processedOn.


The JAX-RS class is secured in the web.xml file. This is in additional security to the RolesAllowed placed on FormBean. It's important because the security-constraint will make sure that a 401 Unauthorized is returned to the API caller rather than an ugly 500 error. While the 500 does secure the app, it makes developing against the API more difficult.

<?xml version="1.0"?>

<web-app xmlns=""
            <web-resource-name>Protected pages</web-resource-name>

FormResource delegates immediately to FormBean. On the GET call to return a list of Forms, it's using the Form Entity object as a transport.

public class FormResource {

    private FormBean formBean;

    public List<Form> getAllForms() { return formBean.findAllForms(); }

    public void postForm(@FormParam("mydata") String mydata) {
        formBean.receiveForm(new Form(mydata));


FormBean does most of the work for the application. It writes the data out to the H2 database and contains the business logic for how to mark RECEIVED items and how to process RECEIVED items. It's configured to allow calls from identities with the role "user" and also the role "sys".

public class FormBean {

    private EntityManager em;

    public void create(Form form) {

    public void update(Form form) {}

    public List<Form> findAllForms() {
        Query q = em.createQuery("SELECT f FROM Form f ORDER BY f.processedOn DESC", Form.class);
        return q.getResultList();

    public List<Form> findReceivedForms() {
        Query q = em.createQuery("SELECT f FROM Form f WHERE f.formStateType = 'RECEIVED'", Form.class);
        return q.getResultList();

    public void receiveForm(Form form) {

    public void processForms() {
                .forEach( f -> {

Role "user" is set up in the Basic Authentication scheme as described in this article. The command was run to add a user with the role to and These are files and a Realm "ApplicationRealm" that ships with WildFly.

Role "sys" is not defined in ApplicationRealm. Rather, it's a role that will be used by ProcessorBean. The RunAs annotation asserts that these functions are running as the specified role. If the annotation is left off, when the scheduled operation comes due, the call into FormBean will fail because of RBAC.

public class ProcessorBean {

    private FormBean formBean;

    @Schedule(minute = "*/2", hour = "*", persistent = false)
    public void processForms() throws InterruptedException {

This demonstration showed how a scheduled operation can use the RunAs annotation to assume a role. That allows access to EJBs secured with RBAC. RunAs is important when there is a later reaper process that's executing some functions or business logic at a later stage. This later stage won't have a user providing an identity and it's up to RunAs to provide one.

Headshot of Carl Walker

By Carl Walker

President and Principal Consultant of Bekwam, Inc