Skip to main content

Posts

Showing posts from February, 2025

Spring implement Thread pool executor

  With Spring one can implement Thread Pool simply by org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor 1) Create  ThreadPoolConfig as below -  import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; @Configuration public class ThreadPoolConfig { @Bean public ThreadPoolTaskExecutor threadPoolExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor .setCorePoolSize(50); executor .setMaxPoolSize(1000); executor .setAwaitTerminationSeconds(30); executor .setPrestartAllCoreThreads( true ); // executor.initialize(); return executor ; } } 2) Autowire  ThreadPoolTaskExecutor where needed -  @Autowired public ThreadPoolTaskExecutor taskExecutor ; logger .info( "Active Threads: {}, Pool Size: {}, Queue Size: {}" , taskE...

Spring MongoDB Log Connection Pool Details - Active, Used, Waiting

  We couldn't find any direct way to log Mongo Connection pool Size. So, we did implement an indirect way as below.  This may be incorrect at times when dealing with Sharded MongoDB having Primaty & Secondary nodes. Because, connection may be used based on read prefrence - Primary, primaryPreferred, Secondary, etc. But, this gives an understanding if connections are used efficiently and there is no wait to acquire connections from pool. This can be further enhanced to log correct connection pool statistics.  1) Implement  MyConnectionPoolListener  as below -  import java.util.concurrent.atomic.AtomicInteger; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.mongodb.event.ConnectionCheckOutFailedEvent; import com.mongodb.event.ConnectionCheckOutStartedEvent; import com.mongodb.event.ConnectionCheckedInEvent; import com.mongodb.event.ConnectionCheckedOutEvent; import com.mongodb.event.ConnectionClosedEvent; import com.mongodb.event.Conne...