diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/executorservice/WaitingForThreadsToFinishTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/executorservice/WaitingForThreadsToFinishTest.java index 17b71aa35b..7e2bf590fd 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/concurrent/executorservice/WaitingForThreadsToFinishTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/executorservice/WaitingForThreadsToFinishTest.java @@ -9,7 +9,6 @@ import java.util.List; import java.util.concurrent.*; import static junit.framework.TestCase.assertTrue; -import static org.junit.Assert.fail; public class WaitingForThreadsToFinishTest { @@ -40,16 +39,13 @@ public class WaitingForThreadsToFinishTest { CountDownLatch latch = new CountDownLatch(2); for (int i = 0; i < 2; i++) { - WORKER_THREAD_POOL.submit(new Runnable() { - @Override - public void run() { - try { - Thread.sleep(1000); - latch.countDown(); - } catch (InterruptedException e) { - e.printStackTrace(); - Thread.currentThread().interrupt(); - } + WORKER_THREAD_POOL.submit(() -> { + try { + Thread.sleep(1000); + latch.countDown(); + } catch (InterruptedException e) { + e.printStackTrace(); + Thread.currentThread().interrupt(); } }); } @@ -83,13 +79,9 @@ public class WaitingForThreadsToFinishTest { awaitTerminationAfterShutdown(WORKER_THREAD_POOL); try { - WORKER_THREAD_POOL.submit(new Callable() { - @Override - public String call() throws Exception { - fail("This thread should have been rejected !"); - Thread.sleep(1000000); - return null; - } + WORKER_THREAD_POOL.submit((Callable) () -> { + Thread.sleep(1000000); + return null; }); } catch (RejectedExecutionException ex) { // diff --git a/gradle/gradle-fat-jar/build.gradle b/gradle/gradle-fat-jar/build.gradle new file mode 100644 index 0000000000..6afad80652 --- /dev/null +++ b/gradle/gradle-fat-jar/build.gradle @@ -0,0 +1,41 @@ +buildscript { + repositories { + jcenter() + } + dependencies { + classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.1' + } +} + +apply plugin: 'java' +apply plugin: 'com.github.johnrengelman.shadow' + +repositories { + mavenCentral() +} + +jar { + manifest { + attributes "Main-Class": "com.baeldung.fatjar.Application" + } + + from { + configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } + } +} + + +task customFatJar(type: Jar) { + manifest { + attributes 'Main-Class': 'com.baeldung.fatjar.Application' + } + baseName = 'all-in-one-jar' + from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } + with jar +} + + +dependencies{ + compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25' + compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25' +} \ No newline at end of file diff --git a/gradle/gradle-fat-jar/src/main/java/com/baeldung/fatjar/Application.java b/gradle/gradle-fat-jar/src/main/java/com/baeldung/fatjar/Application.java new file mode 100644 index 0000000000..470d89c332 --- /dev/null +++ b/gradle/gradle-fat-jar/src/main/java/com/baeldung/fatjar/Application.java @@ -0,0 +1,16 @@ +package com.baeldung.fatjar; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class Application { + + static final Logger logger = LoggerFactory.getLogger(Application.class); + + public static void main(String[] args) { + + logger.info("Hello at Baeldung!"); + } + +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/discovery/DiscoveryClientApplication.java b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/discovery/DiscoveryClientApplication.java index b4f42219a4..d013969ad3 100644 --- a/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/discovery/DiscoveryClientApplication.java +++ b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/discovery/DiscoveryClientApplication.java @@ -1,59 +1,16 @@ package com.baeldung.spring.cloud.consul.discovery; -import java.net.URI; - -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; -import org.springframework.context.annotation.Bean; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableDiscoveryClient -@RestController public class DiscoveryClientApplication { - @Bean - public RestTemplate restTemplate() { - return new RestTemplate(); - } - - @Autowired - private DiscoveryClient discoveryClient; - - @GetMapping("/discoveryClient") - public String home() { - return restTemplate().getForEntity(serviceUrl().resolve("/ping"), String.class) - .getBody(); - } - - @GetMapping("/ping") - public String ping() { - return "pong"; - } - - @RequestMapping("/my-health-check") - public ResponseEntity myCustomCheck() { - return new ResponseEntity<>(HttpStatus.OK); - } - - public URI serviceUrl() { - return discoveryClient.getInstances("myApp") - .stream() - .findFirst() - .map(si -> si.getUri()) - .orElse(null); - } - public static void main(String[] args) { new SpringApplicationBuilder(DiscoveryClientApplication.class).web(true) .run(args); } + } diff --git a/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/discovery/DiscoveryClientController.java b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/discovery/DiscoveryClientController.java new file mode 100644 index 0000000000..1436096d10 --- /dev/null +++ b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/discovery/DiscoveryClientController.java @@ -0,0 +1,50 @@ +package com.baeldung.spring.cloud.consul.discovery; + +import java.net.URI; +import java.util.Optional; + +import javax.naming.ServiceUnavailableException; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; + +@RestController +public class DiscoveryClientController { + + @Autowired + private DiscoveryClient discoveryClient; + + private final RestTemplate restTemplate = new RestTemplate(); + + @GetMapping("/discoveryClient") + public String discoveryPing() throws RestClientException, ServiceUnavailableException { + URI service = serviceUrl().map(s -> s.resolve("/ping")) + .orElseThrow(ServiceUnavailableException::new); + return restTemplate.getForEntity(service, String.class) + .getBody(); + } + + @GetMapping("/ping") + public String ping() { + return "pong"; + } + + @GetMapping("/my-health-check") + public ResponseEntity myCustomCheck() { + return new ResponseEntity<>(HttpStatus.OK); + } + + public Optional serviceUrl() { + return discoveryClient.getInstances("myApp") + .stream() + .findFirst() + .map(si -> si.getUri()); + } + +} diff --git a/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/health/ServiceDiscoveryApplication.java b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/health/ServiceDiscoveryApplication.java index 7db361eb4f..020d7d017c 100644 --- a/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/health/ServiceDiscoveryApplication.java +++ b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/health/ServiceDiscoveryApplication.java @@ -2,24 +2,9 @@ package com.baeldung.spring.cloud.consul.health; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; @SpringBootApplication -@RestController public class ServiceDiscoveryApplication { - @RequestMapping("/my-health-check") - public ResponseEntity myCustomCheck() { - String message = "Testing my healh check function"; - return new ResponseEntity<>(message, HttpStatus.FORBIDDEN); - } - - @RequestMapping("/ping") - public String ping() { - return "pong"; - } public static void main(String[] args) { new SpringApplicationBuilder(ServiceDiscoveryApplication.class).web(true) diff --git a/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/health/ServiceDiscoveryController.java b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/health/ServiceDiscoveryController.java new file mode 100644 index 0000000000..20deba993f --- /dev/null +++ b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/health/ServiceDiscoveryController.java @@ -0,0 +1,22 @@ +package com.baeldung.spring.cloud.consul.health; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class ServiceDiscoveryController { + + @GetMapping("/ping") + public String ping() { + return "pong"; + } + + @GetMapping("/my-health-check") + public ResponseEntity myCustomCheck() { + String message = "Testing my healh check function"; + return new ResponseEntity<>(message, HttpStatus.FORBIDDEN); + } + +} diff --git a/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/properties/DistributedPropertiesApplication.java b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/properties/DistributedPropertiesApplication.java index 919bf08921..c1d2b0acc5 100644 --- a/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/properties/DistributedPropertiesApplication.java +++ b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/properties/DistributedPropertiesApplication.java @@ -1,34 +1,16 @@ package com.baeldung.spring.cloud.consul.properties; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class DistributedPropertiesApplication { - @Value("${my.prop}") - String value; - - @Autowired - private MyProperties properties; - - @RequestMapping("/getConfigFromValue") - public String getConfigFromValue() { - return value; - } - - @RequestMapping("/getConfigFromProperty") - public String getConfigFromProperty() { - return properties.getProp(); - } - public static void main(String[] args) { new SpringApplicationBuilder(DistributedPropertiesApplication.class).web(true) .run(args); } + } diff --git a/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/properties/DistributedPropertiesController.java b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/properties/DistributedPropertiesController.java new file mode 100644 index 0000000000..da2d37eb76 --- /dev/null +++ b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/properties/DistributedPropertiesController.java @@ -0,0 +1,27 @@ +package com.baeldung.spring.cloud.consul.properties; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class DistributedPropertiesController { + + @Value("${my.prop}") + String value; + + @Autowired + private MyProperties properties; + + @GetMapping("/getConfigFromValue") + public String getConfigFromValue() { + return value; + } + + @GetMapping("/getConfigFromProperty") + public String getConfigFromProperty() { + return properties.getProp(); + } + +} diff --git a/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/properties/MyProperties.java b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/properties/MyProperties.java index 7accac168d..d92b18ed51 100644 --- a/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/properties/MyProperties.java +++ b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/properties/MyProperties.java @@ -8,6 +8,7 @@ import org.springframework.context.annotation.Configuration; @Configuration @ConfigurationProperties("my") public class MyProperties { + private String prop; public String getProp() { @@ -17,4 +18,5 @@ public class MyProperties { public void setProp(String prop) { this.prop = prop; } + } diff --git a/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/ribbon/RibbonClientApplication.java b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/ribbon/RibbonClientApplication.java deleted file mode 100644 index b2b54fbe8e..0000000000 --- a/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/ribbon/RibbonClientApplication.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.baeldung.spring.cloud.consul.ribbon; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.cloud.client.loadbalancer.LoadBalanced; -import org.springframework.context.annotation.Bean; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.client.RestTemplate; - -@SpringBootApplication -@RestController -public class RibbonClientApplication { - - @LoadBalanced - @Bean - RestTemplate getRestTemplate() { - return new RestTemplate(); - } - - @Autowired - RestTemplate restTemplate; - - @RequestMapping("/ribbonClient") - public String home() { - return restTemplate.getForObject("http://myApp/ping", String.class); - } - - @GetMapping("/ping") - public String ping() { - return "pong"; - } - - @RequestMapping("/my-health-check") - public ResponseEntity myCustomCheck() { - return new ResponseEntity<>(HttpStatus.OK); - } - - public static void main(String[] args) { - new SpringApplicationBuilder(RibbonClientApplication.class).web(true) - .run(args); - } -}