In this application I am creating a simple java micro service that consumes from a kafka topic.
This implementation uses the spring-kafka framework. The code for this example can be found here: https://github.com/SFRJ/offerskafka
sudo apt install openjdk-8-jdk
java -versionopenjdk version "1.8.0_212" OpenJDK Runtime Environment (build 1.8.0_212-8u212-b03-0ubuntu1.18.10.1-b03) OpenJDK 64-Bit Server VM (build 25.212-b03, mixed mode)
sudo apt install openjdk-11-jdk
java -versionopenjdk version "11.0.3" 2019-04-16 OpenJDK Runtime Environment (build 11.0.3+7-Ubuntu-1ubuntu218.10.1) OpenJDK 64-Bit Server VM (build 11.0.3+7-Ubuntu-1ubuntu218.10.1, mixed mode, sharing)
update-java-alternatives --listjava-1.11.0-openjdk-amd64 1111 /usr/lib/jvm/java-1.11.0-openjdk-amd64 java-1.8.0-openjdk-amd64 1081 /usr/lib/jvm/java-1.8.0-openjdk-amd64
sudo update-alternatives --config java [sudo] password for computername: There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode * 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode 2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode Press <enter> to keep the current choice[*], or type selection number:
sudo update-alternatives --config javac There are 2 choices for the alternative javac (providing /usr/bin/javac). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/javac 1111 auto mode * 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/javac 1111 manual mode 2 /usr/lib/jvm/java-8-openjdk-amd64/bin/javac 1081 manual mode Press <enter> to keep the current choice[*], or type selection number:
cat /etc/environment PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games" JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64/"
cat ~/.bashrc export M2_HOME=/home/javing/maven export M2=$M2_HOME/bin export PATH=$JAVA_HOME/bin:$M2:$PATH
#My aliases alias jv='java -version' alias j8='sudo update-java-alternatives -s java-1.8.0-openjdk-amd64;jv;homej8' alias j11='sudo update-java-alternatives -s java-1.11.0-openjdk-amd64;jv;homej11' alias homej8='export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/;echo $"JAVA_HOME set to:";echo $JAVA_HOME;s' alias homej11='export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/;echo $"JAVA_HOME set to:";echo $JAVA_HOME;s' alias s='source ~/.bashrc'
j11 openjdk version "11.0.3" 2019-04-16 OpenJDK Runtime Environment (build 11.0.3+7-Ubuntu-1ubuntu218.10.1) OpenJDK 64-Bit Server VM (build 11.0.3+7-Ubuntu-1ubuntu218.10.1, mixed mode, sharing) JAVA_HOME set to: /usr/lib/jvm/java-11-openjdk-amd64/bin/
group 'com.javing.customAnnotations'
version '1.0-SNAPSHOT'
project.ext {
aspectjVersion = '1.8.4'
}
apply plugin: 'java'
apply plugin: 'aspectj.gradle'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.6"
}
}
dependencies {
compile 'org.aspectj:aspectjrt:1.8.4'
compile 'org.aspectj:aspectjweaver:1.8.4'
compile 'org.aspectj:aspectjtools:1.8.4'
compile 'junit:junit:4.12'
}
package spike;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface HelloAnnotation {
public boolean isRun() default true;
}
package spike;
public class HelloApp {
public static void main(String[] args) {
HelloApp helloApp = new HelloApp();
helloApp.work();
}
@HelloAnnotation
public void work() {
System.out.println("Hello world!");
}
}
package spike;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class HelloAspect {
@Around("execution(* *(..)) && @annotation(spike.HelloAnnotation)")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("Before");
Object proceed = pjp.proceed();
System.out.println("After");
return proceed;
}
}

import com.github.tomakehurst.wiremock.WireMockServer;
import java.util.Scanner;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
public class FakeThirdPartySystem {
public static void main(String[] args) {
WireMockServer server = new WireMockServer(8081);
server.start();
server.stubFor(get(urlEqualTo("/someUrl"))
.willReturn(aResponse()
.withStatus(200)
.withFixedDelay(10000)
.withBody("Slow reply!")));
//This is just so that the app doesn't exit straight away
Scanner scanner = new Scanner(System.in);
System.out.println("Press enter to exit");
scanner.nextLine();
server.stop();
}
}
public List<BigDecimal> simpleMap(List<Integer> numbers) {
return numbers.map(n -> m1(n));
}
private BigDecimal m1(Integer i) {
return new BigDecimal(i);
}
public List<BigDecimal> flatMapping(List<Integer> numbers) {
return numbers.flatMap(n -> m2(n));
}
private List<BigDecimal> m2(Integer i) {
return List.of(new BigDecimal(i));
}
public List<Try<Option<String>>> returningARedundantOption(List<Integer> numbers) {
return numbers.map(n -> m3(n));
}
private Try<Option<String>> m3(Integer i) {
//Imagine this option is the result of intereacting with other code
// e.g some dao object
return Try.success(Option.some(""));
}
public List<Try<String>> removingRedundancy(List<Integer> numbers) {
return numbers.map(n -> {
return m3(n).flatMap(Option::toTry);
});
}
//Same as above
public List<Try<String>> removingRedundancy(List<Integer> numbers) {
return numbers.map(n -> m3(n).flatMap(Option::toTry));
}
private Try<Option<String>> m3(Integer i) {
return Try.success(Option.some(""));
}
public Try<List<String>> usingSequence(Set<Integer> ids) {
Set<Try<String>> result = ids.map(id -> m4(id).flatMap(Option::toTry));
return Try.sequence(result).map(Seq::toList);
}
//Same as above
public Try<List<String>> spike2(Set<Integer> ids) {
return Try.sequence(ids.map(id -> m4(id).flatMap(Option::toTry))).map(Seq::toList);
}
private Try<Option<String>> m4(Integer id) {
Try.success(Option.of("something" + id));
}
For more information about the vavr.io framework: http://www.vavr.io/
1: boolean isSafeToEat(List<String> allergicFoods) {
2: for (String ingredient : getIngredients()) {
3: if (allergicFoods.contains(ingredient)) {
4: return false;
5: }
6: }
7: return true;
8: }
1: boolean isSafeToEat(List<String> allergicFoods) {
2: return Collections.disjoint(ingredients, allergicFoods);
3: }