Skip to content

Instantly share code, notes, and snippets.

LifecycleBase.start() -> Connector.startInternal() -> Protocol.start() (eg Http11NioProtocol) -> Endpoint.bind() (eg NioEndpoint)

NioEndpoint bind -> initServerSocket

package playground
import java.net.InetSocketAddress
import java.nio.ByteBuffer
import java.nio.channels.AsynchronousChannelGroup
import java.nio.channels.AsynchronousServerSocketChannel
import java.nio.channels.AsynchronousSocketChannel
import java.nio.channels.CompletionHandler
import java.nio.charset.StandardCharsets.UTF_8
import java.time.Clock

intent

produce a docker image with 3 main layers:

  • OS / java - rarely changes, good caching and sharing among apps in a kubernetes cluster
  • dependencies - changes are not too frequent, good caching among app versions
  • app jar - changes with every version, but should be quite compact

how to

in build.gradle.kts:

@xhanin
xhanin / MailhogContainer.kt
Created April 1, 2021 05:29
A simple Kotlin Mailhog Test Container - allow to use mailhog to test sending emails with a real smtp server
package mailhog
/*
dependencies:
testImplementation("org.testcontainers:testcontainers:1.15.2")
testImplementation(platform("org.http4k:http4k-bom:4.3.5.4"))
testImplementation("org.http4k:http4k-core")
testImplementation("org.http4k:http4k-client-apache") {
because("mailhog http api messages access")
public class SafeCachingFactorizer implements Servlet {
private static class FactorResult { BigInteger number; BigInteger[] factors; }
private volatile FactorResult lastResult = null;
public void service(ServletRequest req, ServletResponse resp) {
BigInteger i = extractFromRequest(req);
FactorResult result = this.lastResult;
if (result == null || !i.equals(result.number)) {
result = new FactorResult() { { number = i; factors = factor(i); } };
@xhanin
xhanin / JacksonDeserializerTest.java
Created October 23, 2015 15:42
jackson deserializer unwrapping a data field
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
@xhanin
xhanin / BsonJSR310Module.java
Created June 10, 2015 09:59
RESTX issue 204 workaround
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import de.undercouch.bson4jackson.BsonGenerator;
import de.undercouch.bson4jackson.serializers.BsonSerializer;
@xhanin
xhanin / RestxSpringContextLoaderListener.java
Created February 12, 2015 12:55
restx spring integration
package restx.spring;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContextEvent;
/**
* User: xavierhanin
@xhanin
xhanin / JongoModule.java
Created May 20, 2014 14:00
Secured Jongo
@Module(priority = -100)
public class JongoModule {
JacksonMapper.Builder getJacksonMapperBuilder() {
return new JacksonMapper.Builder()
.registerModule(new BsonJodaTimeModule())
.withView(Views.Private.class)
;
}
@xhanin
xhanin / AppModule.java
Last active August 29, 2015 14:00
RESTX JDBC support with BoneCP for connexion pool and Flyway for migration
// In AppModule class, or could be in a dedicated module.
@Provides
public DBI dbi(@Named("datasource") DataSource ds) {
return new DBI(ds);
}