Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?

Hi Adam, me and my colleague are writing a basic CRUD, Java EE application for school. I was wondering if you can give use any feedback on it? Suggestions, toughs, good practices advice, so on... Thanks!

Hi mr. Bien.

  1. How to audit / history log, the history of entity (jpa) ? For example, like eclipselink history policy (but maybe with auto generated history table) And how to handle the changes in entity, for example when column is changed, removed, different type etc.... (for history table)

  2. How to run test in a container, eg. with arquillian and glassfish-embeded or its better remote or maybe another solutions ? (for cdi inject and jpa, persistence context with 2 persistence unit or more, hsqldb or directly to mysql server or something)

All stuff is in JavaEE, docker, payara...

Thanks for the answers and for VERY good content.

Hi Adam,
(2 unrelated questions)

  1. I see discussions about getting rid of EJB in favor of CDI . Can you please elaborate a bit more and share your opinion on that subject?

  2. What's the best way to add metrics to my JavaEE app? I know that once my metrics are exposed as JSON I can use Firehose, but in terms of coding these metrics (or even use prometheus natively), what is the best way?
    For example, for a rest BCE project, Interceptor seemed to me like the obvious answer, but I think it falls short when it comes to measuring something "Business related" (like how many entities where created, or measuring the time of the process). So I thought emitting events should do the trick, but I'll have to weave the entire code with events (or Decorators?) which seems not that clean...
    I know that Eclipse Microprofile addresses this issue, and I wonder how to accomplish it with full JavaEE

Thank you

zorcic commented Dec 13, 2017

Hi Adam,

if you see DAOs as a defect. Do you also think detlaspike data should not be used?
With EntityManager you will have to e.g. pass 'MyEntity.class' to createNamedQuery(..), you need to define a NamedQuery to select all entities and setParameter(..) is not type-safe.

Thank you for a lot of very good content,

Hi Adam,

I saw your video “A little Rest with JAX-RS 2.0 and Java EE7” and had an issue with asynchronous services. Please see resource "developers".

Do you have any advice?


vernetto commented Dec 16, 2017

docker swarm versus kubernetes versus openshift... which is best in which scenario? Personally I find Openshift way too complicated and I struggle to understand its avantage over plain Kubernetes, I am not buying the "source to image" paradigm. Thank you so much for the great shows, at in our office in Zürich we are many of your followers and disciples of the Prophet of Lean Java EE, the Ueli Steck of Software !

omega09 commented Dec 21, 2017

Hi Adam,

I missed the previous gist. If you want an update on my old issue, I found that for my case the simplest solution to work with the RequestScoped bean which was holding the state was adding a AroundInvoke interceptor to extract the state from its holder bean and assign it to a field in the stateless beans. I didn't like passing the state around as an argument to all the methods of all stateless beans.

Anyhow, my questions:

  1. About stateless bean pools: If I have 1 method for each mathematical operation (public int add(int a, int b)...), does it matter if I put each method in a corresponding stateless bean or all 4 in one? I know that in the first case there will be less of each bean in the pool and in the latter more of the single one. Should I just do what makes the most sense? Is it harmful to have a "god object" stateless bean or the opposite?
  2. As a freelancer you need to teach yourself. How do you keep up with so many technologies that update constantly? What are your sources?

Also thank you for very good advice ;)

Hi Adam.
What would you prefer for a Server push implementation: websocket or SSE? Are These Protocols suitable for long living client to server Connections? E.g. for near realtime information update (stock update for products).

Thank you and Best regards from Hamburg.


sco0ter commented Jan 1, 2018

Hi Adam,

I want to serve a socket from a Java EE container and wonder if it’s a good idea to start accepting client connections in a container managed thread, e.g. using ManagedExecutorService.
It seems reasonable for me, but on the other hand I’ve found out, that serving a socket is prohibited in EJBs, "Because if an enterprise bean is listening on a socket, it can't be passivated -- it must always be available.“

I’ve looked into JCA as an alternative to communicate with a „socket accepting application", but it’s way more complicated.
An another alternative I've found would be to start listening to the socket in a Servlet, but it seems to be a misuse, because it's for HTTP communication.

I also need outbound communication, not only inbound.

Concretely, I want to write an XMPP (Chat) Server which listens on port 5222 for client connections, but use all the Java EE goodies (CDI, JPA, JAX-RS, …). XMPP allows for alternative connection methods which are HTTP and WebSockets. Both could be simply realized with JavaEE APIs, but not the TCP/socket connections.
My idea is to use Netty for managing socket connections and thought I could pass a ManagedExecutorService to Netty’s EventLoopGroup:
new NioEventLoopGroup(1, managedExecutorService)

What's the best architectural approach? JCA, Servlets or a startup bean with ManagedExecutorService (or similar thread handling)?

Thanks, and a happy new year!

afrunt commented Jan 3, 2018

Happy New Year, Adam!
Just one question about Java EE 8 and JAX-RS + Bean Validation + CDI.
How to get work CDI injection within custom validators? I prepared simple example and seems like everything is ok with it, but it is failing when it tries to inject BookService into the custom ConstraintValidator. How to configure the correct behavior of JAX-RS + Bean Validation + CDI? Obviously, I can get every dependency I needed via CDI.current().select(SomeClass.class).get() and it works fine, but it looks dirty and quirky. This example doesn't work on Glassfish 4,5 and Payara 4,5, but it works perfect on openliberty server and on wildfly. Anyway, I need to run it on payara 5 somehow. It also works on TomEE, but only when Validator specified explicitly on custom validation annotation, but it doesn't when specified at META-INF/services/javax.validation.ConstraintValidator

I created the workaround here, it works, but there should be easier solution

sources of original example are here

@Constraint(validatedBy = {})
public @interface JavaEE8Book {
    String message() default "{com.afrunt.example.validation.constraints.javaee8book.message}";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

public class JavaEE8BookValidator implements ConstraintValidator<JavaEE8Book, String> {
    BookService bookService;

    public boolean isValid(String name, ConstraintValidatorContext context) {
        //return CDI.current().select(BookService.class).get().isValidBookName(name);
        return bookService.isValidBookName(name);

Workaround for Glassfish/Payara 5

public class CDIProviderResolver implements ContextResolver<ValidationConfig> {
    ResourceContext context;

    public ValidationConfig getContext(Class<?> type) {
        return new ValidationConfig().constraintValidatorFactory(new CDIConstraintValidatorFactory(context));

    public static class CDIConstraintValidatorFactory implements ConstraintValidatorFactory {
        private ResourceContext context;

        CDIConstraintValidatorFactory(ResourceContext context) {
            this.context = context;

        public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) {
            try {
                return CDI.current().select(key).get();
            } catch (Exception e) {
                return context.getResource(key);

        public void releaseInstance(ConstraintValidator<?, ?> instance) {


Hi Adam,

i really like your really thin war files which are fast to deploy and I'm currently trying to have a really small war for my small private project too. The setup is based on your java ee maven archetype with the glassfish docker image.

To keep the war small, i tried to work with the built in java.util.logging instead of slf4j which i used before. By default, the logger logs into the server log of glassfish. But I'd like to have a single log file only for the log entries of my application. I tried to add a file to the project to define the FileHandler but this file then overrides all the properties of the server. How do you achieve to keep the server config untouched and add logging config? How do you do that in your projects?

In addition i would like to send the log entries via filebeat to ELK. Would you add filebeat to the docker image or would you have a dedicated filebeat docker container with access to the application log files? Or another way?

Thanks in advance

dempile commented Jan 4, 2018

Hi Adam,
1-When you deploy an ear or war to a client server, is there a good mecanism in order to obfuscate the code so the client cant reverse engineer it.
2- If we obfuscate the code , do the application server still be able to execute it properly?
3- What is the best way to add a license to your JEE application when you want to deploy it in a client server.

Thanks for the videos on youtube and the online courses on Vimeo! Really appreciate it.
Question :
I know your view on external libraries and I do understand the reason behind avoiding them.
However, Since I really started using streams, there are times when you are using e.g. IntStream and then getting a value from a structure. (they don't provide Iterator or list output -- don't ask why :) )
Now further down the stream I might also want the integer generated by the stream but normally you cannot as once you do like a map(i -> ds.getObjectAt(i)) the i is not available anymore!
Here I have been saved many a times by Pair as I can have a Object with both values in the Stream.

What do you do in your projects? Create your own each time? or is this good candidate for using Apache Commons?

vanuatoo commented Jan 8, 2018

  1. How to organize role based access to JAX-RS resources?
  2. Please recommend good book on Unit Testing
  3. How to test JAX-RS Resources?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment