Skip to content

Instantly share code, notes, and snippets.

@AdamBien
Created June 21, 2024 04:45
Show Gist options
  • Save AdamBien/463d60f4fa37489120df7816d74ff3b3 to your computer and use it in GitHub Desktop.
Save AdamBien/463d60f4fa37489120df7816d74ff3b3 to your computer and use it in GitHub Desktop.
@nlisker
Copy link

nlisker commented Jun 30, 2024

Hi Adam,

I have several questions about CDI and EJB in Jakarta 10 (or 11). I split them into different comments. Feel free to skip between them to others' questions and come back to them later.

Passivation
@Stateful EJBs can be passivated. Is passivation still considered useful? It requires all the object graph to be Serializable, which is something Java is trying to move away from. CDI's @ConversationScoped, @SessionScoped and @RequestScoped can also hold state, but aren't passivated. Is there a reason to use @Stateful considering that the trend is to move from EJB to CDI?

Simplified use case: 5 users need to guess a number that the server generates. Each client sends its guess, the server waits until all clients send their guesses, then returns a win/lose response to each client. How should the class that holds the guesses be annotated?

@???
class Guesses {
    Map<UserID, Integer> guesses;
}

@nlisker
Copy link

nlisker commented Jun 30, 2024

Pooling
@Stateless EJBs are pooled. CDI doesn't use pooling from what I know. @Dependent (the default) and @ApplicationScoped could be used instead as stateless classes. Again, if the direction is to move to CDI, what's the right way to replace @Stateless? Wouldn't @ApplicationScoped perform worse because it's a singleton?

Simplified use case: classes that manipulate a state, like a vehicle assembly line where each assembler is ready to receive many parts lists.

@??? // Stateful, RequestScoped...
class Parts {
    List<Wheel> wheels;
    List<Window> windows;
    List<Door> windows;
    List<Axis> axes;
    // ...
}

@??? // Stateless, ApplicationScoped...
class WheelAssembler {

    @Inject
    Parts parts;

    void assembleFrontWheels(int num) {
        for (int i = 0; i < num; i++) {
            parts.wheels.get(i).connect(parts.axes.get(i));
        }
    }

    void assembleRearWheels(int num) { /* connect num wheels to rear axis */  }
}

@??? // Stateless, ApplicationScoped...
class WindowAssembler {

    @Inject
    Parts parts;

    void assembleFrontWindows(int num) { /* connect num windows to front doors*/  }
    void assembleRearWindows(int num) { /* connect num windows to rear doors */  }
}

@nlisker
Copy link

nlisker commented Jun 30, 2024

Separation of @Entity and beans
We're told not to mix @Entity and EJB/CDI annotations on the same class. For classes that both hold data and have a behavior that requires injections, how should they be modeled? Splitting the data from the behavior looks like requiring data-oriented over object-oriented design (I'm not against).

Simplified use case: each automotive part (there are hundreds such parts) has its data and a function that is called on it from another class. Here is the approach for 2 of these classes.
In plain OO Java:

class ActionRegistar {
    List<Action> actions;

    void registerAction(Action action) { actions.add(action); } 
}

class Wheel {
    int size;

    public void roll(ActionRegistar registar) {
        Action roll = doRoll(size);
        registar.registerAction(roll);
    }
}

class Horn {
    int volume;

    public void honk(ActionRegistar registar) {
        Action honk = doHonk(volume);
        registar.registerAction(honk); // fined if too loud!
    }
}

and in Jakarta:

@Stateful/@RequestScoped
class ActionRegistar {
    List<Action> actions;

    void registerAction(Action action) { actions.add(action); } 
}

@Entity
class Wheel implements Part {
    int size;

    // can't add roll() here because no injections (unless using CDI lookup)
}

@???
class WheelAction implements Action {

    @Inject
    ActionRegistar registar;

    public void roll(Wheel wheel) {
        Action roll = doRoll(wheel.size);
        registar.registerAction(roll);
    }
}

// similar for Horn

If this is any good to begin with, how would I get the action class from the data class? Like this?

@ApplicationScoped/@Stateless
class ActionPerformer {

    @Inject
    private Instance<Action> instance;

    void performAction(Part part) {
        switch (part) { // pattern matching for switch
            case Wheel wheel -> {
                WheelAction action = instance.select(WheelAction.class).get();
                action.roll(wheel);
            }
            case Horn horn -> {
                HornAction action = instance.select(HornAction.class).get();
                action.honk(horn);
            }
            // case...
        };
    }
}

@AdamBien
Copy link
Author

AdamBien commented Jul 1, 2024

Questions asked at geecon session: "Containerless, Bloatless, YAML-less, Serverless Java #slideless" (https://2024.geecon.org/speakers/info.html?id=894) / https://youtu.be/qw7mgPE-SdQ?si=Jue1Wd2Zz7khffC5 :

  • Why is Kubernetes recommended in private dc but not in public cloud?
  • Why one should not mvn clean?
  • Can you show what is actually inside function.zip file?
  • Why maven not gradle?
  • Is it a good idea to building lambda as a quarkus native image?
  • As the bucket's name is generated, would the bucket be recreated after every deploy of the lambda?
  • What vscode plugins are you using for this kind of development in Java?
  • How to manage multiple environments in this setup ( staging | prod )
  • How to share s3 information with another project for example lambda?
  • How are you versioning and deploying the app for green/blue deployments to run two versions at a time?

@AdamBien
Copy link
Author

AdamBien commented Jul 1, 2024

The time machine, questions from 24th airhacks.tv from Mar 07, 2016:

  • Is Java EE Dying?
  • Cross-component business transactions and BCE
  • Webjars?!
  • Content negotiation
  • Concurrency, SLSB and throttling
  • Runtime pluggability and Java EE, CDI, OSGi
  • Groovy-based DSL for configurability?
  • Caches in crippled network environments
  • DAOs, Entities, CRUD and duplication
  • CDI in EAR
  • Portlets vs. Single Page apps
  • Annotation-based configuration
  • Managing common data with REST (aka microservices)
  • Streams vs. old collections
  • How to deal with abstract classes in BCE?

Questions gist: https://gist.github.com/AdamBien/ad9e7cdc110c634489ca

@AdamBien
Copy link
Author

AdamBien commented Jul 1, 2024

"Adam, do you think Java is losing space in the development of database engines? What do you see in Java development that will remedy this?" via (http://youtube.com/bienadam): https://studio.youtube.com/video/vAI_29c-p8M/comments

@kucharzyk
Copy link

Hi Adam,

I would like to ask for a good (and free for commercial use) Java library for document generation (pdf, docx).
Over the years I've been using different tools like Apache FOP, Jasper Reports, Crystal Reports, Apache POI, iText, openhtmltopdf.
Do we have now any good alternatives for reporting in Java world nowadays? Could you recommend anything?

@AdamBien
Copy link
Author

Hi Adam,

I would like to ask for a good (and free for commercial use) Java library for document generation (pdf, docx). Over the years I've been using different tools like Apache FOP, Jasper Reports, Crystal Reports, Apache POI, iText, openhtmltopdf. Do we have now any good alternatives for reporting in Java world nowadays? Could you recommend anything?

Please re-ask here: https://gist.github.com/AdamBien/edbfe339f2f1fbc7e5e2b219d51d5735 Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment