How to add new schedule job dynamically with Spring

If you want to dynamically schedule tasks you can do it without spring by using ExecutorService in particular ScheduledThreadPoolExecutor Runnable task = () -> doSomething(); ScheduledExecutorService executor = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors()); // Schedule a task that will be executed in 120 sec executor.schedule(task, 120, TimeUnit.SECONDS); // Schedule a task that will be first run in 120 sec … Read more

Spring data CrudRepository exists

@Oleksandr’s answer is correct, but the only way I could get it to work is as follows. I’m using Eclipselink on PostgreSQL. public interface UserRepository extends JpaRepository<User, Long> { @Query(“SELECT CASE WHEN COUNT(u) > 0 THEN ‘true’ ELSE ‘false’ END FROM User u WHERE u.username = ?1”) public Boolean existsByUsername(String username); }

ReactiveCrudRepository to use Hibernate in spring

Is it possible to use Hibernate and Mysql with ReactiveCrudRepository instead of CrudRepository? TL;DR: Not with Hibernate and MySQL, but with R2DBC and Postgres, Microsoft SQL Server or H2. Take a look at Spring Data R2DBC. Long Version Why not JPA? With Hibernate/JPA included this won’t happen in the foreseeable future. JPA is based on … Read more

How to inject a value to bean constructor using annotations

First, you have to specify the constructor arg in your bean definition, and not in your injection points. Then, you can utilize spring’s @Value annotation (spring 3.0) @Component public class DefaultInterfaceParameters { @Inject public DefaultInterfaceParameters(@Value(“${some.property}”) String value) { // assign to a field. } } This is also encouraged as Spring advises constructor injection over … Read more

How to get formatted xml output from jaxb in spring?

<bean class=”org.springframework.oxm.jaxb.Jaxb2Marshaller”> <property name=”classesToBeBound”> <list> …. </list> </property> <property name=”marshallerProperties”> <map> <entry> <key> <util:constant static-field=”javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT” /> </key> <value type=”java.lang.Boolean”>true</value> </entry> </map> </property> </bean>

How to set a timeout in Spring 5 WebFlux WebClient

To set the read and connect timeout I use the method below, because the SO_TIMEOUT option is not available for channels using NIO (and giving the warning Unknown channel option ‘SO_TIMEOUT’ for channel ‘[id: 0xa716fcb2]’) ReactorClientHttpConnector connector = new ReactorClientHttpConnector( options -> options.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000) .compression(true) .afterNettyContextInit(ctx -> { ctx.addHandlerLast(new ReadTimeoutHandler(5000, TimeUnit.MILLISECONDS)); })); return WebClient.builder() .clientConnector(connector) … Read more