Skip to content

Commit fec5927

Browse files
Ahmed TawilaAhmed Tawila
authored andcommitted
Merge branch 'master' of https://github.com/eugenp/tutorials
2 parents b05e035 + b258e53 commit fec5927

11 files changed

Lines changed: 170 additions & 143 deletions

File tree

‎core-java-concurrency/src/test/java/com/baeldung/concurrent/executorservice/WaitingForThreadsToFinishTest.java‎

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.util.concurrent.*;
1010

1111
import static junit.framework.TestCase.assertTrue;
12-
import static org.junit.Assert.fail;
1312

1413
public class WaitingForThreadsToFinishTest {
1514

@@ -40,16 +39,13 @@ public void givenMultipleThreads_whenUsingCountDownLatch_thenMainShoudWaitForAll
4039
CountDownLatch latch = new CountDownLatch(2);
4140

4241
for (int i = 0; i < 2; i++) {
43-
WORKER_THREAD_POOL.submit(new Runnable() {
44-
@Override
45-
public void run() {
46-
try {
47-
Thread.sleep(1000);
48-
latch.countDown();
49-
} catch (InterruptedException e) {
50-
e.printStackTrace();
51-
Thread.currentThread().interrupt();
52-
}
42+
WORKER_THREAD_POOL.submit(() -> {
43+
try {
44+
Thread.sleep(1000);
45+
latch.countDown();
46+
} catch (InterruptedException e) {
47+
e.printStackTrace();
48+
Thread.currentThread().interrupt();
5349
}
5450
});
5551
}
@@ -83,13 +79,9 @@ public void givenMultipleThreads_whenInvokeAll_thenMainThreadShouldWaitForAllToF
8379
awaitTerminationAfterShutdown(WORKER_THREAD_POOL);
8480

8581
try {
86-
WORKER_THREAD_POOL.submit(new Callable<String>() {
87-
@Override
88-
public String call() throws Exception {
89-
fail("This thread should have been rejected !");
90-
Thread.sleep(1000000);
91-
return null;
92-
}
82+
WORKER_THREAD_POOL.submit((Callable<String>) () -> {
83+
Thread.sleep(1000000);
84+
return null;
9385
});
9486
} catch (RejectedExecutionException ex) {
9587
//

‎gradle/gradle-fat-jar/build.gradle‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
buildscript {
2+
repositories {
3+
jcenter()
4+
}
5+
dependencies {
6+
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.1'
7+
}
8+
}
9+
10+
apply plugin: 'java'
11+
apply plugin: 'com.github.johnrengelman.shadow'
12+
13+
repositories {
14+
mavenCentral()
15+
}
16+
17+
jar {
18+
manifest {
19+
attributes "Main-Class": "com.baeldung.fatjar.Application"
20+
}
21+
22+
from {
23+
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
24+
}
25+
}
26+
27+
28+
task customFatJar(type: Jar) {
29+
manifest {
30+
attributes 'Main-Class': 'com.baeldung.fatjar.Application'
31+
}
32+
baseName = 'all-in-one-jar'
33+
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
34+
with jar
35+
}
36+
37+
38+
dependencies{
39+
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
40+
compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
41+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.fatjar;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
7+
public class Application {
8+
9+
static final Logger logger = LoggerFactory.getLogger(Application.class);
10+
11+
public static void main(String[] args) {
12+
13+
logger.info("Hello at Baeldung!");
14+
}
15+
16+
}
Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,16 @@
11
package com.baeldung.spring.cloud.consul.discovery;
22

3-
import java.net.URI;
4-
5-
import org.springframework.beans.factory.annotation.Autowired;
63
import org.springframework.boot.autoconfigure.SpringBootApplication;
74
import org.springframework.boot.builder.SpringApplicationBuilder;
8-
import org.springframework.cloud.client.discovery.DiscoveryClient;
95
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
10-
import org.springframework.context.annotation.Bean;
11-
import org.springframework.http.HttpStatus;
12-
import org.springframework.http.ResponseEntity;
13-
import org.springframework.web.bind.annotation.GetMapping;
14-
import org.springframework.web.bind.annotation.RequestMapping;
15-
import org.springframework.web.bind.annotation.RestController;
16-
import org.springframework.web.client.RestTemplate;
176

187
@SpringBootApplication
198
@EnableDiscoveryClient
20-
@RestController
219
public class DiscoveryClientApplication {
2210

23-
@Bean
24-
public RestTemplate restTemplate() {
25-
return new RestTemplate();
26-
}
27-
28-
@Autowired
29-
private DiscoveryClient discoveryClient;
30-
31-
@GetMapping("/discoveryClient")
32-
public String home() {
33-
return restTemplate().getForEntity(serviceUrl().resolve("/ping"), String.class)
34-
.getBody();
35-
}
36-
37-
@GetMapping("/ping")
38-
public String ping() {
39-
return "pong";
40-
}
41-
42-
@RequestMapping("/my-health-check")
43-
public ResponseEntity<String> myCustomCheck() {
44-
return new ResponseEntity<>(HttpStatus.OK);
45-
}
46-
47-
public URI serviceUrl() {
48-
return discoveryClient.getInstances("myApp")
49-
.stream()
50-
.findFirst()
51-
.map(si -> si.getUri())
52-
.orElse(null);
53-
}
54-
5511
public static void main(String[] args) {
5612
new SpringApplicationBuilder(DiscoveryClientApplication.class).web(true)
5713
.run(args);
5814
}
15+
5916
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.spring.cloud.consul.discovery;
2+
3+
import java.net.URI;
4+
import java.util.Optional;
5+
6+
import javax.naming.ServiceUnavailableException;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.cloud.client.discovery.DiscoveryClient;
10+
import org.springframework.http.HttpStatus;
11+
import org.springframework.http.ResponseEntity;
12+
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.RestController;
14+
import org.springframework.web.client.RestClientException;
15+
import org.springframework.web.client.RestTemplate;
16+
17+
@RestController
18+
public class DiscoveryClientController {
19+
20+
@Autowired
21+
private DiscoveryClient discoveryClient;
22+
23+
private final RestTemplate restTemplate = new RestTemplate();
24+
25+
@GetMapping("/discoveryClient")
26+
public String discoveryPing() throws RestClientException, ServiceUnavailableException {
27+
URI service = serviceUrl().map(s -> s.resolve("/ping"))
28+
.orElseThrow(ServiceUnavailableException::new);
29+
return restTemplate.getForEntity(service, String.class)
30+
.getBody();
31+
}
32+
33+
@GetMapping("/ping")
34+
public String ping() {
35+
return "pong";
36+
}
37+
38+
@GetMapping("/my-health-check")
39+
public ResponseEntity<String> myCustomCheck() {
40+
return new ResponseEntity<>(HttpStatus.OK);
41+
}
42+
43+
public Optional<URI> serviceUrl() {
44+
return discoveryClient.getInstances("myApp")
45+
.stream()
46+
.findFirst()
47+
.map(si -> si.getUri());
48+
}
49+
50+
}

‎spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/health/ServiceDiscoveryApplication.java‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,9 @@
22

33
import org.springframework.boot.autoconfigure.SpringBootApplication;
44
import org.springframework.boot.builder.SpringApplicationBuilder;
5-
import org.springframework.http.HttpStatus;
6-
import org.springframework.http.ResponseEntity;
7-
import org.springframework.web.bind.annotation.RequestMapping;
8-
import org.springframework.web.bind.annotation.RestController;
95

106
@SpringBootApplication
11-
@RestController
127
public class ServiceDiscoveryApplication {
13-
@RequestMapping("/my-health-check")
14-
public ResponseEntity<String> myCustomCheck() {
15-
String message = "Testing my healh check function";
16-
return new ResponseEntity<>(message, HttpStatus.FORBIDDEN);
17-
}
18-
19-
@RequestMapping("/ping")
20-
public String ping() {
21-
return "pong";
22-
}
238

249
public static void main(String[] args) {
2510
new SpringApplicationBuilder(ServiceDiscoveryApplication.class).web(true)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.spring.cloud.consul.health;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
public class ServiceDiscoveryController {
10+
11+
@GetMapping("/ping")
12+
public String ping() {
13+
return "pong";
14+
}
15+
16+
@GetMapping("/my-health-check")
17+
public ResponseEntity<String> myCustomCheck() {
18+
String message = "Testing my healh check function";
19+
return new ResponseEntity<>(message, HttpStatus.FORBIDDEN);
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,16 @@
11
package com.baeldung.spring.cloud.consul.properties;
22

3-
import org.springframework.beans.factory.annotation.Autowired;
4-
import org.springframework.beans.factory.annotation.Value;
53
import org.springframework.boot.autoconfigure.SpringBootApplication;
64
import org.springframework.boot.builder.SpringApplicationBuilder;
7-
import org.springframework.web.bind.annotation.RequestMapping;
85
import org.springframework.web.bind.annotation.RestController;
96

107
@SpringBootApplication
118
@RestController
129
public class DistributedPropertiesApplication {
1310

14-
@Value("${my.prop}")
15-
String value;
16-
17-
@Autowired
18-
private MyProperties properties;
19-
20-
@RequestMapping("/getConfigFromValue")
21-
public String getConfigFromValue() {
22-
return value;
23-
}
24-
25-
@RequestMapping("/getConfigFromProperty")
26-
public String getConfigFromProperty() {
27-
return properties.getProp();
28-
}
29-
3011
public static void main(String[] args) {
3112
new SpringApplicationBuilder(DistributedPropertiesApplication.class).web(true)
3213
.run(args);
3314
}
15+
3416
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.spring.cloud.consul.properties;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
public class DistributedPropertiesController {
10+
11+
@Value("${my.prop}")
12+
String value;
13+
14+
@Autowired
15+
private MyProperties properties;
16+
17+
@GetMapping("/getConfigFromValue")
18+
public String getConfigFromValue() {
19+
return value;
20+
}
21+
22+
@GetMapping("/getConfigFromProperty")
23+
public String getConfigFromProperty() {
24+
return properties.getProp();
25+
}
26+
27+
}

‎spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/properties/MyProperties.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
@Configuration
99
@ConfigurationProperties("my")
1010
public class MyProperties {
11+
1112
private String prop;
1213

1314
public String getProp() {
@@ -17,4 +18,5 @@ public String getProp() {
1718
public void setProp(String prop) {
1819
this.prop = prop;
1920
}
21+
2022
}

0 commit comments

Comments
 (0)