commit
7b46999279
|
@ -11,3 +11,4 @@ This module contains articles about Java 10 core features
|
|||
- [Copying Sets in Java](https://www.baeldung.com/java-copy-sets)
|
||||
- [Converting between a List and a Set in Java](https://www.baeldung.com/convert-list-to-set-and-set-to-list)
|
||||
- [Java IndexOutOfBoundsException “Source Does Not Fit in Dest”](https://www.baeldung.com/java-indexoutofboundsexception)
|
||||
- [Collect a Java Stream to an Immutable Collection](https://www.baeldung.com/java-stream-immutable-collection)
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.java10.streams;
|
||||
|
||||
import static java.util.stream.Collectors.toUnmodifiableList;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class StreamToImmutableJava10UnitTest {
|
||||
|
||||
@Test
|
||||
public void whenUsingCollectorsToUnmodifiableList_thenSuccess() {
|
||||
List<String> givenList = Arrays.asList("a", "b", "c");
|
||||
List<String> result = givenList.stream()
|
||||
.collect(toUnmodifiableList());
|
||||
|
||||
System.out.println(result.getClass());
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@ import org.openjdk.jmh.runner.options.Options;
|
|||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
@ -18,12 +19,12 @@ import java.util.concurrent.TimeUnit;
|
|||
@State(Scope.Thread)
|
||||
public class PrimitivesListPerformance {
|
||||
|
||||
private List<Integer> arrayList = new ArrayList<>();
|
||||
private TIntArrayList tList = new TIntArrayList();
|
||||
private cern.colt.list.IntArrayList coltList = new cern.colt.list.IntArrayList();
|
||||
private IntArrayList fastUtilList = new IntArrayList();
|
||||
private List<Integer> arrayList = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
|
||||
private TIntArrayList tList = new TIntArrayList(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
|
||||
private cern.colt.list.IntArrayList coltList = new cern.colt.list.IntArrayList(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
|
||||
private IntArrayList fastUtilList = new IntArrayList(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
|
||||
|
||||
private int getValue = 10;
|
||||
private int getValue = 4;
|
||||
|
||||
@Benchmark
|
||||
public boolean addArrayList() {
|
||||
|
|
|
@ -21,14 +21,16 @@ public class ThreadPoolInParallelStreamIntegrationTest {
|
|||
long lastNum = 1_000_000;
|
||||
|
||||
List<Long> aList = LongStream.rangeClosed(firstNum, lastNum).boxed().collect(Collectors.toList());
|
||||
|
||||
ForkJoinPool customThreadPool = new ForkJoinPool(4);
|
||||
long actualTotal = customThreadPool
|
||||
.submit(() -> aList.parallelStream()
|
||||
.reduce(0L, Long::sum))
|
||||
.get();
|
||||
|
||||
assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal);
|
||||
try {
|
||||
long actualTotal = customThreadPool
|
||||
.submit(() -> aList.parallelStream().reduce(0L, Long::sum))
|
||||
.get();
|
||||
assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal);
|
||||
} finally {
|
||||
customThreadPool.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
## Core Java Lang (Part 4)
|
||||
|
||||
This module contains articles about core features in the Java language
|
||||
|
||||
- TODO
|
|
@ -0,0 +1,47 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-lang-4</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-lang-4</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-lang-4</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<jmh.version>1.28</jmh.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.finalkeyword;
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class BenchmarkRunner {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
org.openjdk.jmh.Main.main(args);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public static String concatNonFinalStrings() {
|
||||
String x = "x";
|
||||
String y = "y";
|
||||
return x + y;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public static String concatFinalStrings() {
|
||||
final String x = "x";
|
||||
final String y = "y";
|
||||
return x + y;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.finalkeyword;
|
||||
|
||||
import java.io.Console;
|
||||
|
||||
public class ClassVariableFinal {
|
||||
|
||||
static final boolean doX = false;
|
||||
static final boolean doY = true;
|
||||
|
||||
public static void main(String[] args) {
|
||||
Console console = System.console();
|
||||
if (doX) {
|
||||
console.writer().println("x");
|
||||
} else if (doY) {
|
||||
console.writer().println("y");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.finalkeyword;
|
||||
|
||||
import java.io.Console;
|
||||
|
||||
public class ClassVariableNonFinal {
|
||||
|
||||
static boolean doX = false;
|
||||
static boolean doY = true;
|
||||
|
||||
public static void main(String[] args) {
|
||||
Console console = System.console();
|
||||
if (doX) {
|
||||
console.writer().println("x");
|
||||
} else if (doY) {
|
||||
console.writer().println("y");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,131 @@
|
|||
package com.baeldung.classfile;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import com.baeldung.classfile.HelloWorld.HelloSomeone;
|
||||
|
||||
public class Outer {
|
||||
|
||||
// Static Nested class
|
||||
static class StaticNested {
|
||||
public String message() {
|
||||
return "This is a static Nested Class";
|
||||
}
|
||||
}
|
||||
|
||||
// Non-static Nested class
|
||||
class Nested {
|
||||
public String message() {
|
||||
return "This is a non-static Nested Class";
|
||||
}
|
||||
}
|
||||
|
||||
// Local class
|
||||
public String message() {
|
||||
class Local {
|
||||
private String message() {
|
||||
return "This is a Local Class within a method";
|
||||
}
|
||||
}
|
||||
Local local = new Local();
|
||||
return local.message();
|
||||
}
|
||||
|
||||
// Local class within if clause
|
||||
public String message(String name) {
|
||||
if (StringUtils.isEmpty(name)) {
|
||||
class Local {
|
||||
private String message() {
|
||||
return "This is a Local Class within if clause";
|
||||
}
|
||||
}
|
||||
Local local = new Local();
|
||||
return local.message();
|
||||
} else
|
||||
return "Welcome to " + name;
|
||||
}
|
||||
|
||||
// Anonymous Inner class extending a class
|
||||
public String greet() {
|
||||
Outer anonymous = new Outer() {
|
||||
public String greet() {
|
||||
return "Running Anonymous Class...";
|
||||
}
|
||||
};
|
||||
return anonymous.greet();
|
||||
}
|
||||
|
||||
// Anonymous inner class implementing an interface
|
||||
public String greet(String name) {
|
||||
|
||||
HelloWorld helloWorld = new HelloWorld() {
|
||||
public String greet(String name) {
|
||||
return "Welcome to " + name;
|
||||
}
|
||||
|
||||
};
|
||||
return helloWorld.greet(name);
|
||||
}
|
||||
|
||||
// Anonymous inner class implementing nested interface
|
||||
public String greetSomeone(String name) {
|
||||
|
||||
HelloSomeone helloSomeOne = new HelloSomeone() {
|
||||
public String greet(String name) {
|
||||
return "Hello " + name;
|
||||
}
|
||||
|
||||
};
|
||||
return helloSomeOne.greet(name);
|
||||
}
|
||||
|
||||
// Nested interface within a class
|
||||
interface HelloOuter {
|
||||
public String hello(String name);
|
||||
}
|
||||
|
||||
// Enum within a class
|
||||
enum Color {
|
||||
RED, GREEN, BLUE;
|
||||
}
|
||||
}
|
||||
|
||||
interface HelloWorld {
|
||||
|
||||
public String greet(String name);
|
||||
|
||||
// Nested class within an interface
|
||||
class InnerClass {
|
||||
public String greet(String name) {
|
||||
return "Inner class within an interface";
|
||||
}
|
||||
}
|
||||
|
||||
// Nested interface within an interfaces
|
||||
interface HelloSomeone {
|
||||
public String greet(String name);
|
||||
}
|
||||
|
||||
// Enum within an interface
|
||||
enum Directon {
|
||||
NORTH, SOUTH, EAST, WEST;
|
||||
}
|
||||
}
|
||||
|
||||
enum Level {
|
||||
LOW, MEDIUM, HIGH;
|
||||
|
||||
}
|
||||
|
||||
enum Foods {
|
||||
|
||||
DRINKS, EATS;
|
||||
|
||||
// Enum within Enum
|
||||
enum DRINKS {
|
||||
APPLE_JUICE, COLA;
|
||||
}
|
||||
|
||||
enum EATS {
|
||||
POTATO, RICE;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.classfile;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import com.baeldung.classfile.Outer.Nested;
|
||||
|
||||
public class OuterUnitTest {
|
||||
|
||||
@Test
|
||||
public void when_static_nestedClass_then_verifyOutput() {
|
||||
Outer.StaticNested nestedClass = new Outer.StaticNested();
|
||||
assertEquals("This is a static Nested Class", nestedClass.message());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_nestedClass_then_verifyOutput() {
|
||||
Outer outer = new Outer();
|
||||
Nested nestedClass = outer.new Nested();
|
||||
assertEquals("This is a non-static Nested Class", nestedClass.message());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_localClass_then_verifyOutput() {
|
||||
Outer outer = new Outer();
|
||||
assertEquals("This is a Local Class within a method", outer.message());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_localClassInIfClause_then_verifyOutput() {
|
||||
Outer outer = new Outer();
|
||||
assertEquals("Welcome to Baeldung", outer.message("Baeldung"));
|
||||
assertEquals("This is a Local Class within if clause", outer.message(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_anonymousClass_then_verifyOutput() {
|
||||
Outer outer = new Outer();
|
||||
assertEquals("Running Anonymous Class...", outer.greet());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_anonymousClassHelloWorld_then_verifyOutput() {
|
||||
Outer outer = new Outer();
|
||||
assertEquals("Welcome to Baeldung", outer.greet("Baeldung"));
|
||||
}
|
||||
}
|
|
@ -4,3 +4,4 @@
|
|||
- [Set Field Value With Reflection](https://www.baeldung.com/java-set-private-field-value)
|
||||
- [Checking If a Method is Static Using Reflection in Java](https://www.baeldung.com/java-check-method-is-static)
|
||||
- [Checking if a Java Class is ‘abstract’ Using Reflection](https://www.baeldung.com/java-reflection-is-class-abstract)
|
||||
- [Invoking a Private Method in Java](https://www.baeldung.com/java-call-private-method)
|
||||
|
|
|
@ -82,6 +82,7 @@
|
|||
<module>core-java-lang</module>
|
||||
<module>core-java-lang-2</module>
|
||||
<module>core-java-lang-3</module>
|
||||
<module>core-java-lang-4</module>
|
||||
<module>core-java-lang-math</module>
|
||||
<module>core-java-lang-math-2</module>
|
||||
<module>core-java-lang-math-3</module>
|
||||
|
|
|
@ -10,10 +10,8 @@ public class CircularBuffer<E> {
|
|||
|
||||
@SuppressWarnings("unchecked")
|
||||
public CircularBuffer(int capacity) {
|
||||
|
||||
this.capacity = (capacity < 1) ? DEFAULT_CAPACITY : capacity;
|
||||
this.data = (E[]) new Object[capacity];
|
||||
|
||||
this.data = (E[]) new Object[this.capacity];
|
||||
this.readSequence = 0;
|
||||
this.writeSequence = -1;
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ public class JavaRMIIntegrationTest {
|
|||
MessengerServiceImpl server = new MessengerServiceImpl();
|
||||
server.createStubAndBind();
|
||||
} catch (RemoteException e) {
|
||||
fail("Exception Occurred");
|
||||
fail("Exception Occurred: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -34,11 +34,9 @@ public class JavaRMIIntegrationTest {
|
|||
|
||||
String expectedMessage = "Server Message";
|
||||
assertEquals(responseMessage, expectedMessage);
|
||||
} catch (RemoteException e) {
|
||||
fail("Exception Occurred");
|
||||
} catch (NotBoundException nb) {
|
||||
fail("Exception Occurred");
|
||||
}
|
||||
} catch (RemoteException | NotBoundException e) {
|
||||
fail("Exception Occurred: " + e);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
# Kubernetes Java API Sample Code
|
||||
|
||||
This module contains sample code used to show how to use the Kubernetes client Java API.
|
||||
|
||||
Before running those samples, make sure that your environment is correctly configured to access
|
||||
a working Kubernetes cluster.
|
||||
|
||||
An easy way to check that everything is working as expected is issuing any *kubectl get* command:
|
||||
|
||||
```shell
|
||||
$ kubectl get nodes
|
||||
```
|
||||
If you get a valid response, then you're good to go.
|
|
@ -0,0 +1,35 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>kubernetes-parent</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>k8s-intro</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.kubernetes</groupId>
|
||||
<artifactId>client-java</artifactId>
|
||||
<version>11.0.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<configuration>
|
||||
<!-- http://maven.apache.org/plugins/maven-compiler-plugin/ -->
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,31 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
|
||||
import io.kubernetes.client.openapi.ApiClient;
|
||||
import io.kubernetes.client.openapi.apis.CoreV1Api;
|
||||
import io.kubernetes.client.openapi.models.V1NodeList;
|
||||
import io.kubernetes.client.util.Config;
|
||||
|
||||
/**
|
||||
* @author Philippe
|
||||
*
|
||||
*/
|
||||
public class ListNodes {
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
ApiClient client = Config.defaultClient();
|
||||
CoreV1Api api = new CoreV1Api(client);
|
||||
V1NodeList nodeList = api.listNode(null, null, null, null, null, null, null, null, 10, false);
|
||||
nodeList.getItems()
|
||||
.stream()
|
||||
.forEach((node) -> System.out.println(node.getMetadata()));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.kubernetes.intro;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ListNodesLiveTest {
|
||||
@Test
|
||||
void whenListNodes_thenSuccess() throws Exception {
|
||||
ListNodes.main(new String[] {});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>kubernetes-parent</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>k8s-intro</module>
|
||||
</modules>
|
||||
</project>
|
|
@ -7,5 +7,6 @@ This module contains articles about HTTP libraries.
|
|||
- [Jetty ReactiveStreams HTTP Client](https://www.baeldung.com/jetty-reactivestreams-http-client)
|
||||
- [Decode an OkHttp JSON Response](https://www.baeldung.com/okhttp-json-response)
|
||||
- [Retrofit 2 – Dynamic URL](https://www.baeldung.com/retrofit-dynamic-url)
|
||||
- [Adding Interceptors in OkHTTP](https://www.baeldung.com/java-okhttp-interceptors)
|
||||
- More articles [[<-- prev]](/libraries-http)
|
||||
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
### Relevant Articles:
|
||||
|
||||
- [Generate WSDL Stubs with Maven](https://www.baeldung.com/maven-wsdl-stubs)
|
|
@ -0,0 +1,45 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>maven-plugins</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>jaxws</artifactId>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>jaxws-maven-plugin</artifactId>
|
||||
<version>2.6</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>wsimport</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<wsdlDirectory>${project.basedir}/src/main/resources/</wsdlDirectory>
|
||||
<packageName>com.baeldung.soap.ws.client</packageName>
|
||||
<sourceDestDir>
|
||||
${project.build.directory}/generated-sources/
|
||||
</sourceDestDir>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-verifier-plugin</artifactId>
|
||||
<version>${maven.verifier.version}</version>
|
||||
<configuration>
|
||||
<verificationFile>../input-resources/verifications.xml</verificationFile>
|
||||
<failOnError>false</failOnError>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.soap.ws.client;
|
||||
|
||||
public class CountryNotFoundException extends RuntimeException {
|
||||
|
||||
public CountryNotFoundException() {
|
||||
super("Country not found!");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.soap.ws.client;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class CountryServiceClient {
|
||||
|
||||
private CountryService countryService;
|
||||
|
||||
public CountryServiceClient(CountryService countryService) {
|
||||
this.countryService = countryService;
|
||||
}
|
||||
|
||||
public String getCapitalByCountryName(String countryName) {
|
||||
return Optional.of(countryService.findByName(countryName))
|
||||
.map(Country::getCapital).orElseThrow(CountryNotFoundException::new);
|
||||
}
|
||||
|
||||
public int getPopulationByCountryName(String countryName) {
|
||||
return Optional.of(countryService.findByName(countryName))
|
||||
.map(Country::getPopulation).orElseThrow(CountryNotFoundException::new);
|
||||
}
|
||||
|
||||
public Currency getCurrencyByCountryName(String countryName) {
|
||||
return Optional.of(countryService.findByName(countryName))
|
||||
.map(Country::getCurrency).orElseThrow(CountryNotFoundException::new);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
<!-- Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. -->
|
||||
<!-- Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. -->
|
||||
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.ws.soap.baeldung.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://server.ws.soap.baeldung.com/" name="CountryServiceImplService">
|
||||
<types>
|
||||
<xsd:schema>
|
||||
<xsd:import namespace="http://server.ws.soap.baeldung.com/" schemaLocation="country.xsd"/>
|
||||
</xsd:schema>
|
||||
</types>
|
||||
<message name="findByName">
|
||||
<part name="arg0" type="xsd:string"/>
|
||||
</message>
|
||||
<message name="findByNameResponse">
|
||||
<part name="return" type="tns:country"/>
|
||||
</message>
|
||||
<portType name="CountryService">
|
||||
<operation name="findByName">
|
||||
<input wsam:Action="http://server.ws.soap.baeldung.com/CountryService/findByNameRequest" message="tns:findByName"/>
|
||||
<output wsam:Action="http://server.ws.soap.baeldung.com/CountryService/findByNameResponse" message="tns:findByNameResponse"/>
|
||||
</operation>
|
||||
</portType>
|
||||
<binding name="CountryServiceImplPortBinding" type="tns:CountryService">
|
||||
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
|
||||
<operation name="findByName">
|
||||
<soap:operation soapAction=""/>
|
||||
<input>
|
||||
<soap:body use="literal" namespace="http://server.ws.soap.baeldung.com/"/>
|
||||
</input>
|
||||
<output>
|
||||
<soap:body use="literal" namespace="http://server.ws.soap.baeldung.com/"/>
|
||||
</output>
|
||||
</operation>
|
||||
</binding>
|
||||
<service name="CountryServiceImplService">
|
||||
<port name="CountryServiceImplPort" binding="tns:CountryServiceImplPortBinding">
|
||||
<soap:address location="http://localhost:8888/ws/country"/>
|
||||
</port>
|
||||
</service>
|
||||
</definitions>
|
|
@ -0,0 +1,21 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. -->
|
||||
<xs:schema xmlns:tns="http://server.ws.soap.baeldung.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" version="1.0"
|
||||
targetNamespace="http://server.ws.soap.baeldung.com/">
|
||||
|
||||
<xs:complexType name="country">
|
||||
<xs:sequence>
|
||||
<xs:element name="capital" type="xs:string" minOccurs="0"></xs:element>
|
||||
<xs:element name="currency" type="tns:currency" minOccurs="0"></xs:element>
|
||||
<xs:element name="name" type="xs:string" minOccurs="0"></xs:element>
|
||||
<xs:element name="population" type="xs:int"></xs:element>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:simpleType name="currency">
|
||||
<xs:restriction base="xs:string">
|
||||
<xs:enumeration value="EUR"></xs:enumeration>
|
||||
<xs:enumeration value="INR"></xs:enumeration>
|
||||
<xs:enumeration value="USD"></xs:enumeration>
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
</xs:schema>
|
|
@ -0,0 +1,73 @@
|
|||
package com.baeldung.soap.ws.client;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class CountryServiceClientUnitTest {
|
||||
|
||||
CountryServiceClient countryServiceClient;
|
||||
CountryService countryService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
countryService = mock(CountryService.class);
|
||||
countryServiceClient = new CountryServiceClient(countryService);
|
||||
}
|
||||
|
||||
@DisplayName("Get capital by country name when country not found")
|
||||
@Test
|
||||
void givenCountryDoesNotExist_whenGetCapitalByCountryName_thenThrowsCountryNotFoundException() {
|
||||
doThrow(CountryNotFoundException.class).when(countryService).findByName(any());
|
||||
Assertions.assertThrows(CountryNotFoundException.class, () -> countryServiceClient.getCapitalByCountryName(any()));
|
||||
}
|
||||
|
||||
@DisplayName("Get capital by country name when country is India then should return capital")
|
||||
@Test
|
||||
void givenCountryIndia_whenGetCapitalByCountryName_thenShouldReturnCapital() {
|
||||
Country country = mock(Country.class);
|
||||
|
||||
doReturn("New Delhi").when(country).getCapital();
|
||||
doReturn(country).when(countryService).findByName("India");
|
||||
|
||||
Assertions.assertEquals("New Delhi", countryServiceClient.getCapitalByCountryName("India"));
|
||||
}
|
||||
|
||||
@DisplayName("Get population by country name when country not found")
|
||||
@Test
|
||||
void givenCountryDoesNotExist_getPopulationByCountryName_thenThrowsCountryNotFoundException() {
|
||||
doThrow(CountryNotFoundException.class).when(countryService).findByName(any());
|
||||
Assertions.assertThrows(CountryNotFoundException.class, () -> countryServiceClient.getPopulationByCountryName(any()));
|
||||
}
|
||||
|
||||
@DisplayName("Get population by country name when country is India then should return population")
|
||||
@Test
|
||||
void givenCountryIndia_getPopulationByCountryName_thenShouldReturnPopulation() {
|
||||
Country country = mock(Country.class);
|
||||
|
||||
doReturn(1000000).when(country).getPopulation();
|
||||
doReturn(country).when(countryService).findByName("India");
|
||||
|
||||
Assertions.assertEquals(1000000, countryServiceClient.getPopulationByCountryName("India"));
|
||||
}
|
||||
|
||||
@DisplayName("Get currency by country name when country not found")
|
||||
@Test
|
||||
void givenCountryDoesNotExist_getCurrencyByCountryName_thenThrowsCountryNotFoundException() {
|
||||
doThrow(CountryNotFoundException.class).when(countryService).findByName(any());
|
||||
Assertions.assertThrows(CountryNotFoundException.class, () -> countryServiceClient.getCurrencyByCountryName(any()));
|
||||
}
|
||||
|
||||
@DisplayName("Get currency by country name when country is India then should return currency")
|
||||
@Test
|
||||
void givenCountryIndia_getCurrencyByCountryName_thenShouldReturnCurrency() {
|
||||
Country country = mock(Country.class);
|
||||
|
||||
doReturn(Currency.INR).when(country).getCurrency();
|
||||
doReturn(country).when(countryService).findByName("India");
|
||||
|
||||
Assertions.assertEquals(Currency.INR, countryServiceClient.getCurrencyByCountryName("India"));
|
||||
}
|
||||
|
||||
}
|
|
@ -17,6 +17,7 @@
|
|||
<modules>
|
||||
<module>custom-rule</module>
|
||||
<module>maven-enforcer</module>
|
||||
<module>jaxws</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -31,7 +31,7 @@
|
|||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<spring.version>5.2.8.RELEASE</spring.version>
|
||||
<spring.version>5.3.3</spring.version>
|
||||
<spring-security.version>5.2.3.RELEASE</spring-security.version>
|
||||
<spring-boot-starter-test.version>1.5.10.RELEASE</spring-boot-starter-test.version>
|
||||
</properties>
|
||||
|
|
|
@ -50,7 +50,7 @@ public class Application implements CommandLineRunner {
|
|||
LOGGER.info("@@ findByFirstName() call...");
|
||||
repository.findByFirstName("Franz")
|
||||
.forEach(person -> LOGGER.info(person.toString()));
|
||||
LOGGER.info("@@ findByFirstName() call...");
|
||||
LOGGER.info("@@ updateByFirstName() call...");
|
||||
repository.updateByFirstName(2L, "Date Inferno");
|
||||
repository.findAll()
|
||||
.forEach(person -> LOGGER.info(person.toString()));
|
||||
|
|
|
@ -1,23 +1,20 @@
|
|||
package com.baeldung.springdatajdbcintro.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.springdatajdbcintro.entity.Person;
|
||||
import org.springframework.data.jdbc.repository.query.Modifying;
|
||||
import org.springframework.data.jdbc.repository.query.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.springdatajdbcintro.entity.Person;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface PersonRepository extends CrudRepository<Person,Long> {
|
||||
public interface PersonRepository extends CrudRepository<Person, Long> {
|
||||
|
||||
@Query("select * from person where first_name=:firstName")
|
||||
List<Person> findByFirstName(@Param("firstName") String firstName);
|
||||
List<Person> findByFirstName(String firstName);
|
||||
|
||||
@Modifying
|
||||
@Query("UPDATE person SET first_name = :name WHERE id = :id")
|
||||
boolean updateByFirstName(@Param("id") Long id, @Param("name") String name);
|
||||
|
||||
}
|
||||
|
|
2
pom.xml
2
pom.xml
|
@ -471,7 +471,7 @@
|
|||
<module>json-path</module>
|
||||
<module>jsoup</module>
|
||||
<module>jta</module>
|
||||
|
||||
<module>kubernetes</module>
|
||||
<!-- <module>lagom</module> --> <!-- Not a maven project -->
|
||||
<module>language-interop</module>
|
||||
<module>libraries-2</module>
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
### Relevant Articles:
|
||||
|
||||
- [A Guide to SAML with Spring Security](https://www.baeldung.com/spring-security-saml)
|
|
@ -11,7 +11,7 @@
|
|||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-spring-5</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>parent-spring-5/pom.xml</relativePath>
|
||||
<relativePath>../../parent-spring-5</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -19,6 +19,10 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
|
@ -29,6 +33,19 @@
|
|||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
<version>${swagger2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.resilience4j</groupId>
|
||||
<artifactId>resilience4j-timelimiter</artifactId>
|
||||
<version>1.6.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
package com.baeldung.requesttimeout;
|
||||
|
||||
import com.baeldung.requesttimeout.domain.Book;
|
||||
import com.baeldung.requesttimeout.domain.BookRepository;
|
||||
import io.github.resilience4j.timelimiter.TimeLimiter;
|
||||
import io.github.resilience4j.timelimiter.TimeLimiterConfig;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@RestController
|
||||
public class RequestTimeoutRestController {
|
||||
|
||||
private final BookRepository bookRepository;
|
||||
private final WebClient webClient;
|
||||
|
||||
public RequestTimeoutRestController(BookRepository bookRepository, WebClient webClient) {
|
||||
this.bookRepository = bookRepository;
|
||||
this.webClient = webClient;
|
||||
}
|
||||
|
||||
@GetMapping("/author/transactional")
|
||||
@Transactional(timeout = 1)
|
||||
public String getWithTransactionTimeout(@RequestParam String title) {
|
||||
return getAuthor(title);
|
||||
}
|
||||
|
||||
private final TimeLimiter ourTimeLimiter = TimeLimiter.of(TimeLimiterConfig.custom().timeoutDuration(Duration.ofMillis(500)).build());
|
||||
@GetMapping("/author/resilience4j")
|
||||
public Callable<String> getWithResilience4jTimeLimiter(@RequestParam String title) {
|
||||
return TimeLimiter.decorateFutureSupplier(ourTimeLimiter, () -> CompletableFuture.supplyAsync(() -> getAuthor(title)));
|
||||
}
|
||||
|
||||
@GetMapping("/author/mvc-request-timeout")
|
||||
public Callable<String> getWithMvcRequestTimeout(@RequestParam String title) {
|
||||
return () -> getAuthor(title);
|
||||
}
|
||||
|
||||
@GetMapping("/author/webclient")
|
||||
public String getWithWebClient(@RequestParam String title) {
|
||||
return webClient.get()
|
||||
.uri(uriBuilder -> uriBuilder
|
||||
.path("/author/transactional")
|
||||
.queryParam("title", title)
|
||||
.build())
|
||||
.retrieve()
|
||||
.bodyToMono(String.class)
|
||||
.block();
|
||||
}
|
||||
|
||||
private String getAuthor(String title) {
|
||||
bookRepository.wasteTime();
|
||||
return bookRepository.findById(title).map(Book::getAuthor).orElse("No book found for this title.");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.requesttimeout.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import reactor.netty.http.client.HttpClient;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
@Configuration
|
||||
public class WebClientConfiguration {
|
||||
|
||||
@Bean
|
||||
public WebClient webClient() {
|
||||
return WebClient.builder()
|
||||
.baseUrl("http://localhost:8080")
|
||||
.clientConnector(new ReactorClientHttpConnector(HttpClient.create().responseTimeout(Duration.ofMillis(250))))
|
||||
.build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.requesttimeout.domain;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Book {
|
||||
|
||||
@Id
|
||||
private String title;
|
||||
private String author;
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.requesttimeout.domain;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface BookRepository extends JpaRepository<Book, String> {
|
||||
|
||||
default int wasteTime() {
|
||||
int i = Integer.MIN_VALUE;
|
||||
while(i < Integer.MAX_VALUE) {
|
||||
i++;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
spring.mvc.async.request-timeout=750
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.requesttimeout;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import org.springframework.web.reactive.function.client.WebClientRequestException;
|
||||
|
||||
@SpringBootTest
|
||||
@RunWith(SpringRunner.class)
|
||||
public class RequestTimeoutTests {
|
||||
|
||||
private static final WebClient WEB_CLIENT = WebClient.builder().baseUrl("http://localhost:8080").build();
|
||||
|
||||
@Test(expected = WebClientRequestException.class)
|
||||
public void givenTransactionTimeout_whenTimeExpires_thenReceiveException() {
|
||||
getAuthor("transactional");
|
||||
}
|
||||
|
||||
@Test(expected = WebClientRequestException.class)
|
||||
public void givenResilience4jTimeLimiter_whenTimeExpires_thenReceiveException() {
|
||||
getAuthor("resilience4j");
|
||||
}
|
||||
|
||||
@Test(expected = WebClientRequestException.class)
|
||||
public void givenMvcRequestTimeout_whenTimeExpires_thenReceiveException() {
|
||||
getAuthor("mvc-request-timeout");
|
||||
}
|
||||
|
||||
@Test(expected = WebClientRequestException.class)
|
||||
public void givenWebClientTimeout_whenTimeExpires_thenReceiveException() {
|
||||
getAuthor("webclient");
|
||||
}
|
||||
|
||||
private void getAuthor(String authorPath) {
|
||||
WEB_CLIENT.get()
|
||||
.uri(uriBuilder -> uriBuilder
|
||||
.path("/author/" + authorPath)
|
||||
.queryParam("title", "title")
|
||||
.build())
|
||||
.retrieve()
|
||||
.bodyToMono(String.class)
|
||||
.block();
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
spring.mvc.async.request-timeout=750
|
Loading…
Reference in New Issue