Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
46thAirhacksQ&A.md
@deratzmann
Copy link

deratzmann commented Dec 29, 2017

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.

Bastian

@sco0ter
Copy link

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!
Christian

@afrunt
Copy link

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
Thanks!

I created the workaround here, it works, but there should be easier solution https://github.com/afrunt/examples/tree/master/misc/jaxrs-bean-validation-cdi-custom-validator

sources of original example are here https://github.com/afrunt/examples/tree/master/java-ee-8-examples/bean-validation-custom-validator

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

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

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

@Named
@ApplicationScoped
public class JavaEE8BookValidator implements ConstraintValidator<JavaEE8Book, String> {
    @Inject
    BookService bookService;

    @Override
    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

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

    @Override
    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;
        }

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

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

        }
    }
}

@stefanrinderle
Copy link

stefanrinderle commented Jan 4, 2018

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 logging.properties 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
Stefan

@dempile
Copy link

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.

@comdotlinux
Copy link

comdotlinux commented Jan 5, 2018

Hello!
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?

@sjetesjete
Copy link

sjetesjete commented Jan 8, 2018

Hi Adam,
are you open for companies sponsoring your airhacks-webshows?

@vanuatoo
Copy link

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