Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
49970912ea
|
@ -0,0 +1,2 @@
|
|||
## Relevant Articles:
|
||||
- [Introduction to Apache CXF Aegis Data Binding](http://www.baeldung.com/aegis-data-binding-in-apache-cxf)
|
|
@ -0,0 +1,20 @@
|
|||
<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/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>cxf-aegis</artifactId>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>apache-cxf</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<properties>
|
||||
<cxf.version>3.1.8</cxf.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-databinding-aegis</artifactId>
|
||||
<version>${cxf.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.cxf.aegis;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Course {
|
||||
private int id;
|
||||
private String name;
|
||||
private String instructor;
|
||||
private Date enrolmentDate;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getInstructor() {
|
||||
return instructor;
|
||||
}
|
||||
|
||||
public void setInstructor(String instructor) {
|
||||
this.instructor = instructor;
|
||||
}
|
||||
|
||||
public Date getEnrolmentDate() {
|
||||
return enrolmentDate;
|
||||
}
|
||||
|
||||
public void setEnrolmentDate(Date enrolmentDate) {
|
||||
this.enrolmentDate = enrolmentDate;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.cxf.aegis;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface CourseRepo {
|
||||
String getGreeting();
|
||||
void setGreeting(String greeting);
|
||||
Map<Integer, Course> getCourses();
|
||||
void setCourses(Map<Integer, Course> courses);
|
||||
void addCourse(Course course);
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.cxf.aegis;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class CourseRepoImpl implements CourseRepo {
|
||||
private String greeting;
|
||||
private Map<Integer, Course> courses = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public String getGreeting() {
|
||||
return greeting;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setGreeting(String greeting) {
|
||||
this.greeting = greeting;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Integer, Course> getCourses() {
|
||||
return courses;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCourses(Map<Integer, Course> courses) {
|
||||
this.courses = courses;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCourse(Course course) {
|
||||
courses.put(course.getId(), course);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
<mappings>
|
||||
<mapping>
|
||||
<property name="instructor" ignore="true"/>
|
||||
</mapping>
|
||||
</mappings>
|
|
@ -0,0 +1,5 @@
|
|||
<mappings xmlns:ns="http://courserepo.baeldung.com">
|
||||
<mapping name="ns:Baeldung">
|
||||
<property name="greeting" style="attribute"/>
|
||||
</mapping>
|
||||
</mappings>
|
|
@ -0,0 +1,93 @@
|
|||
package com.baeldung.cxf.aegis;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLOutputFactory;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
|
||||
import org.apache.cxf.aegis.AegisContext;
|
||||
import org.apache.cxf.aegis.AegisReader;
|
||||
import org.apache.cxf.aegis.AegisWriter;
|
||||
import org.apache.cxf.aegis.type.AegisType;
|
||||
|
||||
public class BaeldungTest {
|
||||
private AegisContext context;
|
||||
private String fileName = "baeldung.xml";
|
||||
|
||||
@Test
|
||||
public void whenMarshalingAndUnmarshalingCourseRepo_thenCorrect() throws Exception {
|
||||
initializeContext();
|
||||
CourseRepo inputRepo = initCourseRepo();
|
||||
marshalCourseRepo(inputRepo);
|
||||
CourseRepo outputRepo = unmarshalCourseRepo();
|
||||
Course restCourse = outputRepo.getCourses().get(1);
|
||||
Course securityCourse = outputRepo.getCourses().get(2);
|
||||
assertEquals("Welcome to Beldung!", outputRepo.getGreeting());
|
||||
assertEquals("REST with Spring", restCourse.getName());
|
||||
assertEquals(new Date(1234567890000L), restCourse.getEnrolmentDate());
|
||||
assertNull(restCourse.getInstructor());
|
||||
assertEquals("Learn Spring Security", securityCourse.getName());
|
||||
assertEquals(new Date(1456789000000L), securityCourse.getEnrolmentDate());
|
||||
assertNull(securityCourse.getInstructor());
|
||||
}
|
||||
|
||||
private void initializeContext() {
|
||||
context = new AegisContext();
|
||||
Set<Type> rootClasses = new HashSet<Type>();
|
||||
rootClasses.add(CourseRepo.class);
|
||||
context.setRootClasses(rootClasses);
|
||||
Map<Class<?>, String> beanImplementationMap = new HashMap<>();
|
||||
beanImplementationMap.put(CourseRepoImpl.class, "CourseRepo");
|
||||
context.setBeanImplementationMap(beanImplementationMap);
|
||||
context.setWriteXsiTypes(true);
|
||||
context.initialize();
|
||||
}
|
||||
|
||||
private CourseRepoImpl initCourseRepo() {
|
||||
Course restCourse = new Course();
|
||||
restCourse.setId(1);
|
||||
restCourse.setName("REST with Spring");
|
||||
restCourse.setInstructor("Eugen");
|
||||
restCourse.setEnrolmentDate(new Date(1234567890000L));
|
||||
Course securityCourse = new Course();
|
||||
securityCourse.setId(2);
|
||||
securityCourse.setName("Learn Spring Security");
|
||||
securityCourse.setInstructor("Eugen");
|
||||
securityCourse.setEnrolmentDate(new Date(1456789000000L));
|
||||
CourseRepoImpl courseRepo = new CourseRepoImpl();
|
||||
courseRepo.setGreeting("Welcome to Beldung!");
|
||||
courseRepo.addCourse(restCourse);
|
||||
courseRepo.addCourse(securityCourse);
|
||||
return courseRepo;
|
||||
}
|
||||
|
||||
private void marshalCourseRepo(CourseRepo courseRepo) throws Exception {
|
||||
AegisWriter<XMLStreamWriter> writer = context.createXMLStreamWriter();
|
||||
AegisType aegisType = context.getTypeMapping().getType(CourseRepo.class);
|
||||
XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(new FileOutputStream(fileName));
|
||||
writer.write(courseRepo, new QName("http://aegis.cxf.baeldung.com", "baeldung"), false, xmlWriter, aegisType);
|
||||
xmlWriter.close();
|
||||
}
|
||||
|
||||
private CourseRepo unmarshalCourseRepo() throws Exception {
|
||||
AegisReader<XMLStreamReader> reader = context.createXMLStreamReader();
|
||||
XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(fileName));
|
||||
CourseRepo courseRepo = (CourseRepo) reader.read(xmlReader, context.getTypeMapping().getType(CourseRepo.class));
|
||||
xmlReader.close();
|
||||
return courseRepo;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
### Relevant Articles:
|
||||
- [Apache CXF Support for RESTful Web Services](http://www.baeldung.com/apache-cxf-rest-api)
|
|
@ -10,6 +10,7 @@
|
|||
<module>cxf-introduction</module>
|
||||
<module>cxf-spring</module>
|
||||
<module>cxf-jaxrs-implementation</module>
|
||||
<module>cxf-aegis</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
|
|
|
@ -3,3 +3,6 @@
|
|||
## Core Java 9 Examples
|
||||
|
||||
[Java 9 New Features](http://www.baeldung.com/new-java-9)
|
||||
|
||||
### Relevant Articles:
|
||||
- [Java 9 Stream API Improvements](http://www.baeldung.com/java-9-stream-api)
|
||||
|
|
|
@ -13,4 +13,6 @@
|
|||
*.ear
|
||||
|
||||
# Files generated by integration tests
|
||||
*.txt
|
||||
*.txt
|
||||
/bin/
|
||||
/temp
|
||||
|
|
|
@ -39,3 +39,10 @@
|
|||
- [How to Print Screen in Java](http://www.baeldung.com/print-screen-in-java)
|
||||
- [How to Convert String to different data types in Java](http://www.baeldung.com/java-string-conversions)
|
||||
- [Introduction to Java Generics](http://www.baeldung.com/java-generics)
|
||||
- [Generate equals() and hashCode() with Eclipse](http://www.baeldung.com/java-eclipse-equals-and-hashcode)
|
||||
- [A Guide To Java Regular Expressions API](http://www.baeldung.com/regular-expressions-java)
|
||||
- [Sorting in Java](http://www.baeldung.com/java-sorting)
|
||||
- [Getting Started with Java Properties](http://www.baeldung.com/java-properties)
|
||||
- [Grep in Java](http://www.baeldung.com/grep-in-java)
|
||||
- [Java - Combine Multiple Collections](http://www.baeldung.com/java-combine-multiple-collections)
|
||||
- [Simulated Annealing for Travelling Salesman Problem](http://www.baeldung.com/java-simulated-annealing-for-traveling-salesman)
|
||||
|
|
|
@ -1,289 +1,314 @@
|
|||
<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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>core-java</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>core-java</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>core-java</name>
|
||||
<name>core-java</name>
|
||||
|
||||
<dependencies>
|
||||
<dependencies>
|
||||
|
||||
<!-- utils -->
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.collections</groupId>
|
||||
<artifactId>collections-generic</artifactId>
|
||||
<version>${collections-generic.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<!-- utils -->
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.collections</groupId>
|
||||
<artifactId>collections-generic</artifactId>
|
||||
<version>${collections-generic.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-math3</artifactId>
|
||||
<version>${commons-math3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-math3</artifactId>
|
||||
<version>${commons-math3.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.bouncycastle</groupId>
|
||||
<artifactId>bcprov-jdk15on</artifactId>
|
||||
<version>${bouncycastle.version}</version>
|
||||
</dependency>
|
||||
<!-- web -->
|
||||
<dependency>
|
||||
<groupId>org.bouncycastle</groupId>
|
||||
<artifactId>bcprov-jdk15on</artifactId>
|
||||
<version>${bouncycastle.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- marshalling -->
|
||||
<dependency>
|
||||
<groupId>org.unix4j</groupId>
|
||||
<artifactId>unix4j-command</artifactId>
|
||||
<version>${unix4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.grep4j</groupId>
|
||||
<artifactId>grep4j</artifactId>
|
||||
<version>${grep4j.version}</version>
|
||||
</dependency>
|
||||
<!-- web -->
|
||||
|
||||
<!-- logging -->
|
||||
<!-- marshalling -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
<!-- <scope>runtime</scope> -->
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jcl-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
<!-- <scope>runtime</scope> --> <!-- some spring dependencies need to compile against jcl -->
|
||||
</dependency>
|
||||
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>log4j-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- test scoped -->
|
||||
<!-- logging -->
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
<!-- <scope>runtime</scope> -->
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jcl-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
<!-- <scope>runtime</scope> --> <!-- some spring dependencies need to compile against jcl -->
|
||||
</dependency>
|
||||
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>log4j-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-core</artifactId>
|
||||
<version>${org.hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-library</artifactId>
|
||||
<version>${org.hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-all</artifactId>
|
||||
<version>1.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.testng</groupId>
|
||||
<artifactId>testng</artifactId>
|
||||
<version>${testng.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-core</artifactId>
|
||||
<version>${org.hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-library</artifactId>
|
||||
<version>${org.hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>${commons-codec.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
<dependency>
|
||||
<groupId>org.testng</groupId>
|
||||
<artifactId>testng</artifactId>
|
||||
<version>${testng.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<build>
|
||||
<finalName>core-java</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<plugins>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>${commons-codec.version}</version>
|
||||
</dependency>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</dependencies>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<build>
|
||||
<finalName>core-java</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*LongRunningUnitTest.java</exclude>
|
||||
<exclude>**/*ManualTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</excludes>
|
||||
<testFailureIgnore>true</testFailureIgnore>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/libs</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/libs</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<classpathPrefix>libs/</classpathPrefix>
|
||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<classpathPrefix>libs/</classpathPrefix>
|
||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<shadedArtifactAttached>true</shadedArtifactAttached>
|
||||
<transformers>
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<shadedArtifactAttached>true</shadedArtifactAttached>
|
||||
<transformers>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>com.jolira</groupId>
|
||||
<artifactId>onejar-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<configuration>
|
||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
<attachToBuild>true</attachToBuild>
|
||||
<filename>${project.build.finalName}-onejar.${project.packaging}</filename>
|
||||
</configuration>
|
||||
<goals>
|
||||
<goal>one-jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>com.jolira</groupId>
|
||||
<artifactId>onejar-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<configuration>
|
||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
<attachToBuild>true</attachToBuild>
|
||||
<filename>${project.build.finalName}-onejar.${project.packaging}</filename>
|
||||
</configuration>
|
||||
<goals>
|
||||
<goal>one-jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>spring-boot</classifier>
|
||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>spring-boot</classifier>
|
||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>integration</id>
|
||||
<build>
|
||||
|
@ -318,35 +343,38 @@
|
|||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<!-- marshalling -->
|
||||
<jackson.version>2.8.5</jackson.version>
|
||||
<properties>
|
||||
<!-- marshalling -->
|
||||
<jackson.version>2.8.5</jackson.version>
|
||||
|
||||
<!-- logging -->
|
||||
<org.slf4j.version>1.7.21</org.slf4j.version>
|
||||
<logback.version>1.1.7</logback.version>
|
||||
<!-- logging -->
|
||||
<org.slf4j.version>1.7.21</org.slf4j.version>
|
||||
<logback.version>1.1.7</logback.version>
|
||||
|
||||
<!-- util -->
|
||||
<guava.version>19.0</guava.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<bouncycastle.version>1.55</bouncycastle.version>
|
||||
<commons-codec.version>1.10</commons-codec.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<commons-io.version>2.5</commons-io.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<collections-generic.version>4.01</collections-generic.version>
|
||||
<!-- util -->
|
||||
<guava.version>19.0</guava.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<bouncycastle.version>1.55</bouncycastle.version>
|
||||
<commons-codec.version>1.10</commons-codec.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<commons-io.version>2.5</commons-io.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<collections-generic.version>4.01</collections-generic.version>
|
||||
<unix4j.version>0.4</unix4j.version>
|
||||
<grep4j.version>1.8.7</grep4j.version>
|
||||
<lombok.version>1.16.12</lombok.version>
|
||||
|
||||
<!-- testing -->
|
||||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
<mockito.version>1.10.19</mockito.version>
|
||||
<testng.version>6.10</testng.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<!-- testing -->
|
||||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
<mockito.version>1.10.19</mockito.version>
|
||||
<testng.version>6.10</testng.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
|
||||
<!-- maven plugins -->
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
<!-- maven plugins -->
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
|
||||
</properties>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,2 @@
|
|||
### Relevant Articles:
|
||||
- [SHA-256 Hashing in Java](http://www.baeldung.com/sha-256-hashing-java)
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.algorithms;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class City {
|
||||
|
||||
private int x;
|
||||
private int y;
|
||||
|
||||
public City() {
|
||||
this.x = (int) (Math.random() * 500);
|
||||
this.y = (int) (Math.random() * 500);
|
||||
}
|
||||
|
||||
public double distanceToCity(City city) {
|
||||
int x = Math.abs(getX() - city.getX());
|
||||
int y = Math.abs(getY() - city.getY());
|
||||
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.algorithms;
|
||||
|
||||
public class SimulatedAnnealing {
|
||||
|
||||
private static Travel travel = new Travel(10);
|
||||
|
||||
public static double simulateAnnealing(double startingTemperature, int numberOfIterations, double coolingRate) {
|
||||
System.out.println("Starting SA with temperature: " + startingTemperature + ", # of iterations: " + numberOfIterations + " and colling rate: " + coolingRate);
|
||||
double t = startingTemperature;
|
||||
travel.generateInitialTravel();
|
||||
double bestDistance = travel.getDistance();
|
||||
System.out.println("Initial distance of travel: " + bestDistance);
|
||||
Travel bestSolution = travel;
|
||||
Travel currentSolution = bestSolution;
|
||||
|
||||
for (int i = 0; i < numberOfIterations; i++) {
|
||||
if (t > 0.1) {
|
||||
currentSolution.swapCities();
|
||||
double currentDistance = currentSolution.getDistance();
|
||||
if (currentDistance < bestDistance) {
|
||||
bestDistance = currentDistance;
|
||||
} else if (Math.exp((bestDistance - currentDistance) / t) < Math.random()) {
|
||||
currentSolution.revertSwap();
|
||||
}
|
||||
t *= coolingRate;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
if (i % 100 == 0) {
|
||||
System.out.println("Iteration #" + i);
|
||||
}
|
||||
}
|
||||
return bestDistance;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Optimized distance for travel: " + simulateAnnealing(10, 10000, 0.9995));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package com.baeldung.algorithms;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Travel {
|
||||
|
||||
private ArrayList<City> travel = new ArrayList<>();
|
||||
private ArrayList<City> previousTravel = new ArrayList<>();
|
||||
|
||||
public Travel(int numberOfCities) {
|
||||
for (int i = 0; i < numberOfCities; i++) {
|
||||
travel.add(new City());
|
||||
}
|
||||
}
|
||||
|
||||
public void generateInitialTravel() {
|
||||
if (travel.isEmpty())
|
||||
new Travel(10);
|
||||
Collections.shuffle(travel);
|
||||
}
|
||||
|
||||
public void swapCities() {
|
||||
int a = generateRandomIndex();
|
||||
int b = generateRandomIndex();
|
||||
previousTravel = travel;
|
||||
City x = travel.get(a);
|
||||
City y = travel.get(b);
|
||||
travel.set(a, y);
|
||||
travel.set(b, x);
|
||||
}
|
||||
|
||||
public void revertSwap() {
|
||||
travel = previousTravel;
|
||||
}
|
||||
|
||||
private int generateRandomIndex() {
|
||||
return (int) (Math.random() * travel.size());
|
||||
}
|
||||
|
||||
public City getCity(int index) {
|
||||
return travel.get(index);
|
||||
}
|
||||
|
||||
public int getDistance() {
|
||||
int distance = 0;
|
||||
for (int index = 0; index < travel.size(); index++) {
|
||||
City starting = getCity(index);
|
||||
City destination;
|
||||
if (index + 1 < travel.size()) {
|
||||
destination = getCity(index + 1);
|
||||
} else {
|
||||
destination = getCity(0);
|
||||
}
|
||||
distance += starting.distanceToCity(destination);
|
||||
}
|
||||
return distance;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
### Relevant Articles:
|
||||
- [A Guide To UDP In Java](http://www.baeldung.com/udp-in-java)
|
||||
- [A Guide To HTTP Cookies In Java](http://www.baeldung.com/cookies-java)
|
||||
- [A Guide to the Java URL](http://www.baeldung.com/java-url)
|
||||
- [Working with Network Interfaces in Java](http://www.baeldung.com/java-network-interfaces)
|
|
@ -0,0 +1,2 @@
|
|||
###Relevant Articles:
|
||||
- [Introduction to the Java NIO Selector](http://www.baeldung.com/java-nio-selector)
|
|
@ -0,0 +1,2 @@
|
|||
### Relevant Articles:
|
||||
- [How to Print Screen in Java](http://www.baeldung.com/print-screen-in-java)
|
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung.scripting;
|
||||
|
||||
import javax.script.Bindings;
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
public class Nashorn {
|
||||
public static void main(String[] args) throws ScriptException, NoSuchMethodException {
|
||||
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
|
||||
|
||||
Object result = engine.eval(
|
||||
"var greeting='hello world';" +
|
||||
"print(greeting);" +
|
||||
"greeting");
|
||||
|
||||
System.out.println(result);
|
||||
|
||||
Bindings bindings = engine.createBindings();
|
||||
bindings.put("count", 3);
|
||||
bindings.put("name", "baeldung");
|
||||
|
||||
String script = "var greeting='Hello ';" +
|
||||
"for(var i=count;i>0;i--) { " +
|
||||
"greeting+=name + ' '" +
|
||||
"}" +
|
||||
"greeting";
|
||||
|
||||
Object bindingsResult = engine.eval(script, bindings);
|
||||
System.out.println(bindingsResult);
|
||||
|
||||
engine.eval("function composeGreeting(name) {" +
|
||||
"return 'Hello ' + name" +
|
||||
"}");
|
||||
Invocable invocable = (Invocable) engine;
|
||||
|
||||
Object funcResult = invocable.invokeFunction("composeGreeting", "baeldung");
|
||||
System.out.println(funcResult);
|
||||
|
||||
Object map = engine.eval("var HashMap = Java.type('java.util.HashMap');" +
|
||||
"var map = new HashMap();" +
|
||||
"map.put('hello', 'world');" +
|
||||
"map");
|
||||
|
||||
System.out.println(map);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.algorithms;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SimulatedAnnealingTest {
|
||||
|
||||
@Test
|
||||
public void testSimulateAnnealing() {
|
||||
Assert.assertTrue(SimulatedAnnealing.simulateAnnealing(10, 1000, 0.9) > 0);
|
||||
}
|
||||
|
||||
}
|
|
@ -22,7 +22,6 @@ public class EncoderDecoderUnitTest {
|
|||
private static final String testUrl = "http://www.baeldung.com?key1=value+1&key2=value%40%21%242&key3=value%253";
|
||||
private static final String testUrlWithPath = "http://www.baeldung.com/path+1?key1=value+1&key2=value%40%21%242&key3=value%253";
|
||||
|
||||
|
||||
private String encodeValue(String value) {
|
||||
String encoded = null;
|
||||
try {
|
||||
|
@ -59,9 +58,7 @@ public class EncoderDecoderUnitTest {
|
|||
requestParams.put("key2", "value@!$2");
|
||||
requestParams.put("key3", "value%3");
|
||||
|
||||
String encodedURL = requestParams.keySet().stream()
|
||||
.map(key -> key + "=" + encodeValue(requestParams.get(key)))
|
||||
.collect(joining("&", "http://www.baeldung.com?", ""));
|
||||
String encodedURL = requestParams.keySet().stream().map(key -> key + "=" + encodeValue(requestParams.get(key))).collect(joining("&", "http://www.baeldung.com?", ""));
|
||||
|
||||
Assert.assertThat(testUrl, is(encodedURL));
|
||||
}
|
||||
|
@ -103,12 +100,9 @@ public class EncoderDecoderUnitTest {
|
|||
|
||||
String path = "path+1";
|
||||
|
||||
String encodedURL = requestParams.keySet().stream()
|
||||
.map(key -> key + "=" + encodeValue(requestParams.get(key)))
|
||||
.collect(joining("&", "http://www.baeldung.com/" + encodePath(path) + "?", ""));
|
||||
String encodedURL = requestParams.keySet().stream().map(key -> key + "=" + encodeValue(requestParams.get(key))).collect(joining("&", "http://www.baeldung.com/" + encodePath(path) + "?", ""));
|
||||
|
||||
Assert.assertThat(testUrlWithPath, is(encodedURL));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package com.baeldung.grep;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.unix4j.Unix4j;
|
||||
import static org.unix4j.Unix4j.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.unix4j.line.Line;
|
||||
import static org.unix4j.unix.Grep.*;
|
||||
import static org.unix4j.unix.cut.CutOption.*;
|
||||
|
||||
public class GrepWithUnix4JTest {
|
||||
|
||||
private File fileToGrep;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
final String separator = File.separator;
|
||||
final String filePath = String.join(separator, new String[] { "src", "test", "resources", "dictionary.in" });
|
||||
fileToGrep = new File(filePath);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGrepWithSimpleString_thenCorrect() {
|
||||
int expectedLineCount = 4;
|
||||
|
||||
// grep "NINETEEN" dictionary.txt
|
||||
List<Line> lines = Unix4j.grep("NINETEEN", fileToGrep).toLineList();
|
||||
|
||||
assertEquals(expectedLineCount, lines.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInverseGrepWithSimpleString_thenCorrect() {
|
||||
int expectedLineCount = 178687;
|
||||
|
||||
// grep -v "NINETEEN" dictionary.txt
|
||||
List<Line> lines = grep(Options.v, "NINETEEN", fileToGrep).toLineList();
|
||||
|
||||
assertEquals(expectedLineCount, lines.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGrepWithRegex_thenCorrect() {
|
||||
int expectedLineCount = 151;
|
||||
|
||||
// grep -c ".*?NINE.*?" dictionary.txt
|
||||
String patternCount = grep(Options.c, ".*?NINE.*?", fileToGrep).cut(fields, ":", 1).toStringResult();
|
||||
|
||||
assertEquals(expectedLineCount, Integer.parseInt(patternCount));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
### Relevant Articles:
|
||||
- [Convert Hex to ASCII in Java](http://www.baeldung.com/java-convert-hex-to-ascii)
|
|
@ -0,0 +1,2 @@
|
|||
Relevant Articles:
|
||||
- [Java String Conversions](http://www.baeldung.com/java-string-conversions)
|
|
@ -0,0 +1,3 @@
|
|||
### Relevant Articles:
|
||||
- [Introduction to the Java NIO2 File API](http://www.baeldung.com/java-nio-2-file-api)
|
||||
- [Java NIO2 Path API](http://www.baeldung.com/java-nio-2-path)
|
|
@ -17,9 +17,7 @@ public class Java8CollectionCleanupUnitTest {
|
|||
@Test
|
||||
public void givenListContainsNulls_whenFilteringParallel_thenCorrect() {
|
||||
final List<Integer> list = Lists.newArrayList(null, 1, 2, null, 3, null);
|
||||
final List<Integer> listWithoutNulls = list.parallelStream()
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
final List<Integer> listWithoutNulls = list.parallelStream().filter(Objects::nonNull).collect(Collectors.toList());
|
||||
|
||||
assertThat(listWithoutNulls, hasSize(3));
|
||||
}
|
||||
|
@ -27,9 +25,7 @@ public class Java8CollectionCleanupUnitTest {
|
|||
@Test
|
||||
public void givenListContainsNulls_whenFilteringSerial_thenCorrect() {
|
||||
final List<Integer> list = Lists.newArrayList(null, 1, 2, null, 3, null);
|
||||
final List<Integer> listWithoutNulls = list.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
final List<Integer> listWithoutNulls = list.stream().filter(Objects::nonNull).collect(Collectors.toList());
|
||||
|
||||
assertThat(listWithoutNulls, hasSize(3));
|
||||
}
|
||||
|
@ -45,9 +41,7 @@ public class Java8CollectionCleanupUnitTest {
|
|||
@Test
|
||||
public void givenListContainsDuplicates_whenRemovingDuplicatesWithJava8_thenCorrect() {
|
||||
final List<Integer> listWithDuplicates = Lists.newArrayList(1, 1, 2, 2, 3, 3);
|
||||
final List<Integer> listWithoutDuplicates = listWithDuplicates.parallelStream()
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
final List<Integer> listWithoutDuplicates = listWithDuplicates.parallelStream().distinct().collect(Collectors.toList());
|
||||
|
||||
assertThat(listWithoutDuplicates, hasSize(3));
|
||||
}
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
package com.baeldung.java8;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JavaFileSizeUnitTest {
|
||||
private static final long EXPECTED_FILE_SIZE_IN_BYTES = 11;
|
||||
private String filePath;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
final String separator = File.separator;
|
||||
filePath = String.join(separator, new String[] { "src", "test", "resources", "testFolder", "sample_file_1.in" });
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetFileSize_thenCorrect() {
|
||||
final File file = new File(filePath);
|
||||
|
||||
final long size = getFileSize(file);
|
||||
|
||||
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, size);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetFileSizeUsingNioApi_thenCorrect() throws IOException {
|
||||
final Path path = Paths.get(this.filePath);
|
||||
final FileChannel fileChannel = FileChannel.open(path);
|
||||
|
||||
final long fileSize = fileChannel.size();
|
||||
|
||||
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, fileSize);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetFileSizeUsingApacheCommonsIO_thenCorrect() {
|
||||
final File file = new File(filePath);
|
||||
|
||||
final long size = FileUtils.sizeOf(file);
|
||||
|
||||
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, size);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetReadableFileSize_thenCorrect() {
|
||||
final File file = new File(filePath);
|
||||
|
||||
final long size = getFileSize(file);
|
||||
|
||||
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES + " bytes", FileUtils.byteCountToDisplaySize(size));
|
||||
}
|
||||
|
||||
private long getFileSize(final File file) {
|
||||
final long length = file.length();
|
||||
return length;
|
||||
}
|
||||
}
|
|
@ -93,6 +93,7 @@ public class OptionalTest {
|
|||
boolean is2017 = yearOptional.filter(y -> y == 2017).isPresent();
|
||||
assertFalse(is2017);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFiltersWithoutOptional_thenCorrect() {
|
||||
assertTrue(priceIsInRange1(new Modem(10.0)));
|
||||
|
@ -121,12 +122,9 @@ public class OptionalTest {
|
|||
}
|
||||
|
||||
public boolean priceIsInRange2(Modem modem2) {
|
||||
return Optional.ofNullable(modem2)
|
||||
.map(Modem::getPrice)
|
||||
.filter(p -> p >= 10)
|
||||
.filter(p -> p <= 15)
|
||||
.isPresent();
|
||||
return Optional.ofNullable(modem2).map(Modem::getPrice).filter(p -> p >= 10).filter(p -> p <= 15).isPresent();
|
||||
}
|
||||
|
||||
// Transforming Value With map()
|
||||
@Test
|
||||
public void givenOptional_whenMapWorks_thenCorrect() {
|
||||
|
|
|
@ -2,6 +2,9 @@ package org.baeldung.java.collections;
|
|||
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.collections4.IterableUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -107,4 +110,26 @@ public class CollectionsConcatenateUnitTest {
|
|||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingApacheCommons_whenConcatenatingUsingUnion_thenCorrect() {
|
||||
Collection<String> collectionA = asList("S", "T");
|
||||
Collection<String> collectionB = asList("U", "V");
|
||||
|
||||
Iterable<String> combinedIterables = CollectionUtils.union(collectionA, collectionB);
|
||||
Collection<String> collectionCombined = Lists.newArrayList(combinedIterables);
|
||||
|
||||
Assert.assertEquals(asList("S", "T", "U", "V"), collectionCombined);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingApacheCommons_whenConcatenatingUsingChainedIterable_thenCorrect() {
|
||||
Collection<String> collectionA = asList("S", "T");
|
||||
Collection<String> collectionB = asList("U", "V");
|
||||
|
||||
Iterable<String> combinedIterables = IterableUtils.chainedIterable(collectionA, collectionB);
|
||||
Collection<String> collectionCombined = Lists.newArrayList(combinedIterables);
|
||||
|
||||
Assert.assertEquals(asList("S", "T", "U", "V"), collectionCombined);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,8 +15,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||
public void whenJoiningTwoArrays_thenJoined() {
|
||||
String[] animals1 = new String[] { "Dog", "Cat" };
|
||||
String[] animals2 = new String[] { "Bird", "Cow" };
|
||||
String[] result = Stream.concat(
|
||||
Arrays.stream(animals1), Arrays.stream(animals2)).toArray(String[]::new);
|
||||
String[] result = Stream.concat(Arrays.stream(animals1), Arrays.stream(animals2)).toArray(String[]::new);
|
||||
|
||||
assertArrayEquals(result, new String[] { "Dog", "Cat", "Bird", "Cow" });
|
||||
}
|
||||
|
@ -25,9 +24,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||
public void whenJoiningTwoCollections_thenJoined() {
|
||||
Collection<String> collection1 = Arrays.asList("Dog", "Cat");
|
||||
Collection<String> collection2 = Arrays.asList("Bird", "Cow", "Moose");
|
||||
Collection<String> result = Stream.concat(
|
||||
collection1.stream(), collection2.stream())
|
||||
.collect(Collectors.toList());
|
||||
Collection<String> result = Stream.concat(collection1.stream(), collection2.stream()).collect(Collectors.toList());
|
||||
|
||||
assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Bird", "Cow", "Moose")));
|
||||
}
|
||||
|
@ -36,10 +33,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||
public void whenJoiningTwoCollectionsWithFilter_thenJoined() {
|
||||
Collection<String> collection1 = Arrays.asList("Dog", "Cat");
|
||||
Collection<String> collection2 = Arrays.asList("Bird", "Cow", "Moose");
|
||||
Collection<String> result = Stream.concat(
|
||||
collection1.stream(), collection2.stream())
|
||||
.filter(e -> e.length() == 3)
|
||||
.collect(Collectors.toList());
|
||||
Collection<String> result = Stream.concat(collection1.stream(), collection2.stream()).filter(e -> e.length() == 3).collect(Collectors.toList());
|
||||
|
||||
assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Cow")));
|
||||
}
|
||||
|
@ -67,9 +61,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||
animals.put(2, "Cat");
|
||||
animals.put(3, "Cow");
|
||||
|
||||
String result = animals.entrySet().stream()
|
||||
.map(entry -> entry.getKey() + " = " + entry.getValue())
|
||||
.collect(Collectors.joining(", "));
|
||||
String result = animals.entrySet().stream().map(entry -> entry.getKey() + " = " + entry.getValue()).collect(Collectors.joining(", "));
|
||||
|
||||
assertEquals(result, "1 = Dog, 2 = Cat, 3 = Cow");
|
||||
}
|
||||
|
@ -80,10 +72,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||
nested.add(Arrays.asList("Dog", "Cat"));
|
||||
nested.add(Arrays.asList("Cow", "Pig"));
|
||||
|
||||
String result = nested.stream().map(
|
||||
nextList -> nextList.stream()
|
||||
.collect(Collectors.joining("-")))
|
||||
.collect(Collectors.joining("; "));
|
||||
String result = nested.stream().map(nextList -> nextList.stream().collect(Collectors.joining("-"))).collect(Collectors.joining("; "));
|
||||
|
||||
assertEquals(result, "Dog-Cat; Cow-Pig");
|
||||
}
|
||||
|
@ -91,17 +80,14 @@ public class JoinSplitCollectionsUnitTest {
|
|||
@Test
|
||||
public void whenConvertCollectionToStringAndSkipNull_thenConverted() {
|
||||
Collection<String> animals = Arrays.asList("Dog", "Cat", null, "Moose");
|
||||
String result = animals.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.joining(", "));
|
||||
String result = animals.stream().filter(Objects::nonNull).collect(Collectors.joining(", "));
|
||||
|
||||
assertEquals(result, "Dog, Cat, Moose");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSplitCollectionHalf_thenConverted() {
|
||||
Collection<String> animals = Arrays.asList(
|
||||
"Dog", "Cat", "Cow", "Bird", "Moose", "Pig");
|
||||
Collection<String> animals = Arrays.asList("Dog", "Cat", "Cow", "Bird", "Moose", "Pig");
|
||||
Collection<String> result1 = new ArrayList<>();
|
||||
Collection<String> result2 = new ArrayList<>();
|
||||
AtomicInteger count = new AtomicInteger();
|
||||
|
@ -122,9 +108,8 @@ public class JoinSplitCollectionsUnitTest {
|
|||
|
||||
@Test
|
||||
public void whenSplitArrayByWordLength_thenConverted() {
|
||||
String[] animals = new String[] { "Dog", "Cat", "Bird", "Cow", "Pig", "Moose"};
|
||||
Map<Integer, List<String>> result = Arrays.stream(animals)
|
||||
.collect(Collectors.groupingBy(String::length));
|
||||
String[] animals = new String[] { "Dog", "Cat", "Bird", "Cow", "Pig", "Moose" };
|
||||
Map<Integer, List<String>> result = Arrays.stream(animals).collect(Collectors.groupingBy(String::length));
|
||||
|
||||
assertTrue(result.get(3).equals(Arrays.asList("Dog", "Cat", "Cow", "Pig")));
|
||||
assertTrue(result.get(4).equals(Arrays.asList("Bird")));
|
||||
|
@ -151,9 +136,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||
public void whenConvertStringToMap_thenConverted() {
|
||||
String animals = "1 = Dog, 2 = Cat, 3 = Bird";
|
||||
|
||||
Map<Integer, String> result = Arrays.stream(
|
||||
animals.split(", ")).map(next -> next.split(" = "))
|
||||
.collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1]));
|
||||
Map<Integer, String> result = Arrays.stream(animals.split(", ")).map(next -> next.split(" = ")).collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1]));
|
||||
|
||||
assertEquals(result.get(1), "Dog");
|
||||
assertEquals(result.get(2), "Cat");
|
||||
|
@ -164,10 +147,7 @@ public class JoinSplitCollectionsUnitTest {
|
|||
public void whenConvertCollectionToStringMultipleSeparators_thenConverted() {
|
||||
String animals = "Dog. , Cat, Bird. Cow";
|
||||
|
||||
Collection<String> result = Arrays.stream(animals.split("[,|.]"))
|
||||
.map(String::trim)
|
||||
.filter(next -> !next.isEmpty())
|
||||
.collect(Collectors.toList());
|
||||
Collection<String> result = Arrays.stream(animals.split("[,|.]")).map(String::trim).filter(next -> !next.isEmpty()).collect(Collectors.toList());
|
||||
|
||||
assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Bird", "Cow")));
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -45,8 +45,7 @@ public class EJBClient {
|
|||
final String distinctName = "";
|
||||
final String beanName = "HelloWorld";
|
||||
final String viewClassName = HelloWorld.class.getName();
|
||||
final String toLookup = "ejb:" + appName + "/" + moduleName
|
||||
+ "/" + distinctName + "/" + beanName + "!" + viewClassName;
|
||||
final String toLookup = String.format("ejb:%s/%s/%s/%s!%s", appName, moduleName, distinctName, beanName, viewClassName);
|
||||
return (HelloWorld) context.lookup(toLookup);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
### Relevant Articles:
|
||||
- [Guide to Hazelcast with Java](http://www.baeldung.com/java-hazelcast)
|
|
@ -0,0 +1,19 @@
|
|||
<?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>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>imageprocessing</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.imagej</groupId>
|
||||
<artifactId>ij</artifactId>
|
||||
<version>1.51h</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,22 @@
|
|||
package imagej;
|
||||
|
||||
import ij.IJ;
|
||||
import ij.ImagePlus;
|
||||
import ij.process.ImageProcessor;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawRect {
|
||||
public static void main(String[] args) {
|
||||
ImagePlus imp = IJ.openImage(DrawRect.class.getClassLoader().getResource("lena.jpg").getPath());
|
||||
drawRect(imp);
|
||||
imp.show();
|
||||
}
|
||||
|
||||
private static void drawRect(ImagePlus imp) {
|
||||
ImageProcessor ip = imp.getProcessor();
|
||||
ip.setColor(Color.BLUE);
|
||||
ip.setLineWidth(4);
|
||||
ip.drawRect(10, 10, imp.getWidth() - 20, imp.getHeight() - 20);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package swing;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class DrawRect {
|
||||
public static void main(String[] args) throws IOException {
|
||||
BufferedImage image = loadImage();
|
||||
drawRectangle(image);
|
||||
displayImage(image);
|
||||
}
|
||||
|
||||
private static BufferedImage loadImage() throws IOException {
|
||||
String imagePath = DrawRect.class.getClassLoader().getResource("lena.jpg").getPath();
|
||||
return ImageIO.read(new File(imagePath));
|
||||
}
|
||||
|
||||
private static void drawRectangle(BufferedImage image) {
|
||||
Graphics2D g = (Graphics2D) image.getGraphics();
|
||||
g.setStroke(new BasicStroke(3));
|
||||
g.setColor(Color.BLUE);
|
||||
g.drawRect(10, 10, image.getWidth() - 20, image.getHeight() - 20);
|
||||
}
|
||||
|
||||
private static void displayImage(BufferedImage image) {
|
||||
JLabel picLabel = new JLabel(new ImageIcon(image));
|
||||
|
||||
JPanel jPanel = new JPanel();
|
||||
jPanel.add(picLabel);
|
||||
|
||||
JFrame f = new JFrame();
|
||||
f.setSize(new Dimension(image.getWidth(), image.getHeight()));
|
||||
f.add(jPanel);
|
||||
f.setVisible(true);
|
||||
}
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 93 KiB |
|
@ -0,0 +1,38 @@
|
|||
<code_scheme name="updated-formatter">
|
||||
<option name="RIGHT_MARGIN" value="260" />
|
||||
<option name="ENABLE_JAVADOC_FORMATTING" value="false" />
|
||||
<option name="FORMATTER_TAGS_ENABLED" value="true" />
|
||||
<JavaCodeStyleSettings>
|
||||
<option name="SPACE_AFTER_CLOSING_ANGLE_BRACKET_IN_TYPE_ARGUMENT" value="true" />
|
||||
<option name="DO_NOT_WRAP_AFTER_SINGLE_ANNOTATION" value="true" />
|
||||
<option name="ALIGN_MULTILINE_ANNOTATION_PARAMETERS" value="true" />
|
||||
</JavaCodeStyleSettings>
|
||||
<codeStyleSettings language="JAVA">
|
||||
<option name="KEEP_LINE_BREAKS" value="false" />
|
||||
<option name="KEEP_FIRST_COLUMN_COMMENT" value="false" />
|
||||
<option name="KEEP_BLANK_LINES_IN_DECLARATIONS" value="1" />
|
||||
<option name="KEEP_BLANK_LINES_IN_CODE" value="1" />
|
||||
<option name="KEEP_BLANK_LINES_BEFORE_RBRACE" value="1" />
|
||||
<option name="INDENT_CASE_FROM_SWITCH" value="false" />
|
||||
<option name="ALIGN_MULTILINE_PARAMETERS" value="false" />
|
||||
<option name="ALIGN_MULTILINE_RESOURCES" value="false" />
|
||||
<option name="SPACE_WITHIN_ARRAY_INITIALIZER_BRACES" value="true" />
|
||||
<option name="SPACE_BEFORE_ARRAY_INITIALIZER_LBRACE" value="true" />
|
||||
<option name="CALL_PARAMETERS_WRAP" value="1" />
|
||||
<option name="METHOD_PARAMETERS_WRAP" value="1" />
|
||||
<option name="RESOURCE_LIST_WRAP" value="5" />
|
||||
<option name="EXTENDS_LIST_WRAP" value="1" />
|
||||
<option name="THROWS_LIST_WRAP" value="1" />
|
||||
<option name="EXTENDS_KEYWORD_WRAP" value="1" />
|
||||
<option name="THROWS_KEYWORD_WRAP" value="1" />
|
||||
<option name="METHOD_CALL_CHAIN_WRAP" value="1" />
|
||||
<option name="BINARY_OPERATION_WRAP" value="1" />
|
||||
<option name="BINARY_OPERATION_SIGN_ON_NEXT_LINE" value="true" />
|
||||
<option name="TERNARY_OPERATION_WRAP" value="5" />
|
||||
<option name="ARRAY_INITIALIZER_WRAP" value="1" />
|
||||
<indentOptions>
|
||||
<option name="CONTINUATION_INDENT_SIZE" value="2" />
|
||||
<option name="TAB_SIZE" value="8" />
|
||||
</indentOptions>
|
||||
</codeStyleSettings>
|
||||
</code_scheme>
|
|
@ -0,0 +1,2 @@
|
|||
### Relevant Articles:
|
||||
- [A Guide to Cassandra with Java](http://www.baeldung.com/cassandra-with-java)
|
|
@ -1,2 +1,3 @@
|
|||
### Relevant Articles:
|
||||
- [The Basics of JUnit 5 – A Preview](http://www.baeldung.com/junit-5-preview)
|
||||
- [A Guide to JUnit 5](http://www.baeldung.com/junit-5)
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
### Relevant Articles:
|
||||
- TBD
|
||||
- [Improved Java Logging with Mapped Diagnostic Context (MDC)](http://www.baeldung.com/mdc-in-log4j-2-logback)
|
||||
|
||||
### References
|
||||
|
||||
|
|
|
@ -5,20 +5,37 @@
|
|||
<artifactId>logmdc</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>logmdc</name>
|
||||
<description>tutorial on logging with MDC</description>
|
||||
<packaging>war</packaging>
|
||||
<description>tutorial on logging with MDC and NDC</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${springframework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${springframework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${springframework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${springframework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${springframework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${javax.servlet.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.library}</version>
|
||||
</dependency>
|
||||
|
||||
<!--log4j dependencies -->
|
||||
<dependency>
|
||||
|
@ -31,12 +48,12 @@
|
|||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-api</artifactId>
|
||||
<version>2.7</version>
|
||||
<version>${log4j2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>${log4j-api.version}</version>
|
||||
<version>${log4j2.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--disruptor for log4j2 async logging -->
|
||||
|
@ -52,6 +69,13 @@
|
|||
<artifactId>logback-classic</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- JBoss Logging (bridge) dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.jboss.logging</groupId>
|
||||
<artifactId>jboss-logging</artifactId>
|
||||
<version>${jbosslogging.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
|
@ -59,15 +83,51 @@
|
|||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${springframework.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<springframework.version>4.3.4.RELEASE</springframework.version>
|
||||
<log4j.version>1.2.17</log4j.version>
|
||||
<log4j-api.version>2.7</log4j-api.version>
|
||||
<log4j2.version>2.7</log4j2.version>
|
||||
<disruptor.version>3.3.6</disruptor.version>
|
||||
<logback.version>1.1.7</logback.version>
|
||||
<jbosslogging.version>3.3.0.Final</jbosslogging.version>
|
||||
<javax.servlet.version>3.1.0</javax.servlet.version>
|
||||
<jackson.library>2.8.5</jackson.library>
|
||||
<junit.version>4.12</junit.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.2</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>2.4</version>
|
||||
<configuration>
|
||||
<warSourceDirectory>src/main/webapp</warSourceDirectory>
|
||||
<warName>logging-service</warName>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
<finalName>logging-service</finalName>
|
||||
</build>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = "com.baeldung")
|
||||
public class AppConfiguration extends WebMvcConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
|
||||
configurer.enable();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.config;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
|
||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||
|
||||
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
|
||||
|
||||
@Override
|
||||
public void onStartup(ServletContext servletContext) throws ServletException {
|
||||
super.onStartup(servletContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getRootConfigClasses() {
|
||||
return new Class[] { AppConfiguration.class };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getServletConfigClasses() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getServletMappings() {
|
||||
return new String[] { "/" };
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.ndc;
|
||||
|
||||
public class Investment {
|
||||
private String transactionId;
|
||||
private String owner;
|
||||
private Long amount;
|
||||
|
||||
public Investment() {
|
||||
}
|
||||
|
||||
public Investment(String transactionId, String owner, Long amount) {
|
||||
this.transactionId = transactionId;
|
||||
this.owner = owner;
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public String getTransactionId() {
|
||||
return transactionId;
|
||||
}
|
||||
|
||||
public void setTransactionId(String transactionId) {
|
||||
this.transactionId = transactionId;
|
||||
}
|
||||
|
||||
public String getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public void setOwner(String owner) {
|
||||
this.owner = owner;
|
||||
}
|
||||
|
||||
public Long getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public void setAmount(Long amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.ndc.controller;
|
||||
|
||||
import org.jboss.logging.NDC;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.ndc.Investment;
|
||||
import com.baeldung.ndc.service.InvestmentService;
|
||||
|
||||
|
||||
@RestController
|
||||
public class JBossLoggingController {
|
||||
@Autowired
|
||||
@Qualifier("JBossLoggingInvestmentService")
|
||||
private InvestmentService jbossLoggingBusinessService;
|
||||
|
||||
@RequestMapping(value = "/ndc/jboss-logging", method = RequestMethod.POST)
|
||||
public ResponseEntity<Investment> postPayment(@RequestBody Investment investment) {
|
||||
// Add transactionId and owner to NDC
|
||||
NDC.push("tx.id=" + investment.getTransactionId());
|
||||
NDC.push("tx.owner=" + investment.getOwner());
|
||||
|
||||
try {
|
||||
jbossLoggingBusinessService.transfer(investment.getAmount());
|
||||
} finally {
|
||||
// take out owner from the NDC stack
|
||||
NDC.pop();
|
||||
|
||||
// take out transactionId from the NDC stack
|
||||
NDC.pop();
|
||||
|
||||
NDC.clear();
|
||||
}
|
||||
return new ResponseEntity<Investment>(investment, HttpStatus.OK);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.ndc.controller;
|
||||
|
||||
import org.apache.logging.log4j.ThreadContext;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.ndc.Investment;
|
||||
import com.baeldung.ndc.service.InvestmentService;
|
||||
|
||||
@RestController
|
||||
public class Log4J2Controller {
|
||||
@Autowired
|
||||
@Qualifier("log4j2InvestmentService")
|
||||
private InvestmentService log4j2BusinessService;
|
||||
|
||||
@RequestMapping(value = "/ndc/log4j2", method = RequestMethod.POST)
|
||||
public ResponseEntity<Investment> postPayment(@RequestBody Investment investment) {
|
||||
// Add transactionId and owner to NDC
|
||||
ThreadContext.push("tx.id=" + investment.getTransactionId());
|
||||
ThreadContext.push("tx.owner=" + investment.getOwner());
|
||||
|
||||
try {
|
||||
log4j2BusinessService.transfer(investment.getAmount());
|
||||
} finally {
|
||||
// take out owner from the NDC stack
|
||||
ThreadContext.pop();
|
||||
|
||||
// take out transactionId from the NDC stack
|
||||
ThreadContext.pop();
|
||||
|
||||
ThreadContext.clearAll();
|
||||
}
|
||||
return new ResponseEntity<Investment>(investment, HttpStatus.OK);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.ndc.controller;
|
||||
|
||||
import org.apache.log4j.NDC;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.ndc.Investment;
|
||||
import com.baeldung.ndc.service.InvestmentService;
|
||||
|
||||
@RestController
|
||||
public class Log4JController {
|
||||
@Autowired
|
||||
@Qualifier("log4jInvestmentService")
|
||||
private InvestmentService log4jBusinessService;
|
||||
|
||||
@RequestMapping(value = "/ndc/log4j", method = RequestMethod.POST)
|
||||
public ResponseEntity<Investment> postPayment(@RequestBody Investment investment) {
|
||||
// Add transactionId and owner to NDC
|
||||
NDC.push("tx.id=" + investment.getTransactionId());
|
||||
NDC.push("tx.owner=" + investment.getOwner());
|
||||
|
||||
try {
|
||||
log4jBusinessService.transfer(investment.getAmount());
|
||||
} finally {
|
||||
// take out owner from the NDC stack
|
||||
NDC.pop();
|
||||
|
||||
// take out transactionId from the NDC stack
|
||||
NDC.pop();
|
||||
|
||||
NDC.remove();
|
||||
}
|
||||
return new ResponseEntity<Investment>(investment, HttpStatus.OK);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.ndc.service;
|
||||
|
||||
/**
|
||||
* A fake investment service.
|
||||
*/
|
||||
public interface InvestmentService {
|
||||
|
||||
/**
|
||||
* Sample service transferring a given amount of money.
|
||||
* @param amount
|
||||
* @return {@code true} when the transfer complete successfully, {@code false} otherwise.
|
||||
*/
|
||||
default public boolean transfer(long amount) {
|
||||
beforeTransfer(amount);
|
||||
// exchange messages with a remote system to transfer the money
|
||||
try {
|
||||
// let's pause randomly to properly simulate an actual system.
|
||||
Thread.sleep((long) (500 + Math.random() * 500));
|
||||
} catch (InterruptedException e) {
|
||||
// should never happen
|
||||
}
|
||||
// let's simulate both failing and successful transfers
|
||||
boolean outcome = Math.random() >= 0.25;
|
||||
afterTransfer(amount, outcome);
|
||||
return outcome;
|
||||
}
|
||||
|
||||
void beforeTransfer(long amount);
|
||||
|
||||
void afterTransfer(long amount, boolean outcome);
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.ndc.service;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@Qualifier("JBossLoggingInvestmentService")
|
||||
public class JBossLoggingInvestmentService implements InvestmentService {
|
||||
private static final Logger logger = Logger.getLogger(JBossLoggingInvestmentService.class);
|
||||
|
||||
@Override
|
||||
public void beforeTransfer(long amount) {
|
||||
logger.infov("Preparing to transfer {0}$.", amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTransfer(long amount, boolean outcome) {
|
||||
logger.infov("Has transfer of {0}$ completed successfully ? {1}.", amount, outcome);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.ndc.service;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@Qualifier("log4j2InvestmentService")
|
||||
public class Log4J2InvestmentService implements InvestmentService {
|
||||
private static final Logger logger = LogManager.getLogger();
|
||||
|
||||
@Override
|
||||
public void beforeTransfer(long amount) {
|
||||
logger.info("Preparing to transfer {}$.", amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTransfer(long amount, boolean outcome) {
|
||||
logger.info("Has transfer of {}$ completed successfully ? {}.", amount, outcome);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.ndc.service;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@Qualifier("log4jInvestmentService")
|
||||
public class Log4JInvestmentService implements InvestmentService {
|
||||
private Logger logger = Logger.getLogger(Log4JInvestmentService.class);
|
||||
|
||||
@Override
|
||||
public void beforeTransfer(long amount) {
|
||||
logger.info("Preparing to transfer " + amount + "$.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTransfer(long amount, boolean outcome) {
|
||||
logger.info("Has transfer of " + amount + "$ completed successfully ? " + outcome + ".");
|
||||
}
|
||||
}
|
|
@ -3,6 +3,10 @@ log4j.appender.consoleAppender.layout=org.apache.log4j.PatternLayout
|
|||
|
||||
#note the %X{userName} - this is how you fetch data from Mapped Diagnostic Context (MDC)
|
||||
#log4j.appender.consoleAppender.layout.ConversionPattern=%-4r [%t] %5p %c{1} %x - %m%n
|
||||
# %x is used to fetch data from NDC. So below setting uses both MDC and NDC
|
||||
log4j.appender.consoleAppender.layout.ConversionPattern=%-4r [%t] %5p %c{1} %x - %m - tx.id=%X{transaction.id} tx.owner=%X{transaction.owner}%n
|
||||
|
||||
log4j.rootLogger = TRACE, consoleAppender
|
||||
# NDC only setting - %x is used to fetch data from NDC
|
||||
#log4j.appender.consoleAppender.layout.ConversionPattern=%-4r [%t] %5p %c{1} - %m - [%x]%n
|
||||
|
||||
log4j.rootLogger = INFO, consoleAppender
|
|
@ -2,8 +2,15 @@
|
|||
<Configuration status="INFO">
|
||||
<Appenders>
|
||||
<Console name="stdout" target="SYSTEM_OUT">
|
||||
<!-- MDC and NDC -->
|
||||
<PatternLayout
|
||||
pattern="%-4r [%t] %5p %c{1} - %m - tx.id=%X{transaction.id} tx.owner=%X{transaction.owner}%n" />
|
||||
pattern="%-4r [%t] %5p %c{1} - %m - %x - tx.id=%X{transaction.id} tx.owner=%X{transaction.owner}%n" />
|
||||
<!-- MDC Only -->
|
||||
<!-- <PatternLayout
|
||||
pattern="%-4r [%t] %5p %c{1} - %m - tx.id=%X{transaction.id} tx.owner=%X{transaction.owner}%n" /> -->
|
||||
<!-- NDC Only -->
|
||||
<!-- <PatternLayout
|
||||
pattern="%-4r [%t] %5p %c{1} - %m - %x%n" /> -->
|
||||
</Console>
|
||||
</Appenders>
|
||||
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
package com.baeldung.ndc;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import com.baeldung.config.AppConfiguration;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = AppConfiguration.class)
|
||||
@WebAppConfiguration
|
||||
public class NDCLogTest {
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webApplicationContext;
|
||||
|
||||
private Investment investment;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
|
||||
|
||||
investment = new Investment();
|
||||
investment.setTransactionId("123");
|
||||
investment.setOwner("Mark");
|
||||
investment.setAmount(1000L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLog4jLogger_whenNDCAdded_thenResponseOkAndNDCInLog() throws Exception {
|
||||
mockMvc.perform(post("/ndc/log4j", investment).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(investment))).andExpect(status().is2xxSuccessful());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLog4j2Logger_whenNDCAdded_thenResponseOkAndNDCInLog() throws Exception {
|
||||
mockMvc.perform(post("/ndc/log4j2", investment).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(investment))).andExpect(status().is2xxSuccessful());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJBossLoggerBridge_whenNDCAdded_thenResponseOkAndNDCInLog() throws Exception {
|
||||
mockMvc.perform(post("/ndc/jboss-logging", investment).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(investment))).andExpect(status().is2xxSuccessful());
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,2 +1,6 @@
|
|||
### Relevant Articles:
|
||||
- [Introduction to Java Logging](http://www.baeldung.com/java-logging-intro)
|
||||
- [Introduction to SLF4J](http://www.baeldung.com/slf4j-with-log4j2-logback)
|
||||
- [Generate equals() and hashCode() with Eclipse](http://www.baeldung.com/java-eclipse-equals-and-hashcode)
|
||||
- [A Guide To Java Regular Expressions API](http://www.baeldung.com/regular-expressions-java)
|
||||
- [Introduction to SLF4J](http://www.baeldung.com/slf4j-with-log4j2-logback)
|
||||
|
|
|
@ -8,13 +8,24 @@
|
|||
<artifactId>log4j</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<!--log4j dependencies-->
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<dependencies>
|
||||
<!--log4j dependencies -->
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>apache-log4j-extras</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>apache-log4j-extras</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--log4j2 dependencies-->
|
||||
<dependency>
|
||||
|
@ -58,14 +69,14 @@
|
|||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
<properties>
|
||||
<log4j.version>1.2.17</log4j.version>
|
||||
<log4j-api.version>2.7</log4j-api.version>
|
||||
<log4j-core.version>2.7</log4j-core.version>
|
||||
<disruptor.version>3.3.6</disruptor.version>
|
||||
<logback.version>1.1.7</logback.version>
|
||||
|
||||
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.log4j;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
public class Log4jRollingExample {
|
||||
|
||||
private final static Logger logger = Logger.getLogger(Log4jRollingExample.class);
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
for(int i = 0; i<2000; i++){
|
||||
logger.info("This is the " + i + " time I say 'Hello World'.");
|
||||
Thread.sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.log4j2;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class Log4j2RollingExample {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(Log4j2RollingExample.class);
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
for(int i = 0; i<2000; i++){
|
||||
logger.info("This is the {} time I say 'Hello World'.", i);
|
||||
Thread.sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.logback;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class LogbackRollingExample {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(LogbackRollingExample.class);
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
for(int i = 0; i<2000; i++){
|
||||
logger.info("This is the {} time I say 'Hello World'.", i);
|
||||
Thread.sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.slf4j;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class Slf4jRollingExample {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(Slf4jRollingExample.class);
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
for(int i = 0; i<2000; i++){
|
||||
logger.info("This is the {} time I say 'Hello World'.", i);
|
||||
Thread.sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,28 +1,95 @@
|
|||
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
|
||||
<log4j:configuration debug="false">
|
||||
|
||||
<!--Console appender-->
|
||||
<!--Console appender -->
|
||||
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %p %m%n"/>
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<!-- File appender-->
|
||||
<!-- File appender -->
|
||||
<appender name="fout" class="org.apache.log4j.FileAppender">
|
||||
<param name="file" value="log4j/target/baeldung-log4j.log"/>
|
||||
<param name="append" value="false"/>
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n"/>
|
||||
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %p %m%n"/>
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<!--Override log level for specified package-->
|
||||
<!-- Rolling appenders -->
|
||||
<appender name="roll-by-size" class="org.apache.log4j.RollingFileAppender">
|
||||
<param name="file" value="target/log4j/roll-by-size/app.log"/>
|
||||
<param name="MaxFileSize" value="5KB"/>
|
||||
<param name="MaxBackupIndex" value="2"/> <!-- It's one by default. -->
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n"/>
|
||||
</layout>
|
||||
</appender>
|
||||
<appender name="roll-by-size-2" class="org.apache.log4j.RollingFileAppender">
|
||||
<param name="file" value="target/log4j/roll-by-size-2/app.log"/>
|
||||
<param name="MaxFileSize" value="5KB"/>
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n"/>
|
||||
</layout>
|
||||
</appender>
|
||||
<appender name="roll-by-window"
|
||||
class="org.apache.log4j.rolling.RollingFileAppender">
|
||||
<rollingPolicy
|
||||
class="org.apache.log4j.rolling.FixedWindowRollingPolicy">
|
||||
<param name="ActiveFileName" value="target/log4j/roll-by-window/app.log"/>
|
||||
<param name="FileNamePattern"
|
||||
value="target/log4j/roll-by-window/app.%i.log.gz"/>
|
||||
<param name="MinIndex" value="7"/>
|
||||
<param name="MaxIndex" value="17"/>
|
||||
</rollingPolicy>
|
||||
<triggeringPolicy
|
||||
class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
|
||||
<param name="MaxFileSize" value="50000"/>
|
||||
</triggeringPolicy>
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n"/>
|
||||
</layout>
|
||||
</appender>
|
||||
<appender name="roll-by-time"
|
||||
class="org.apache.log4j.rolling.RollingFileAppender">
|
||||
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
|
||||
<param name="FileNamePattern"
|
||||
value="target/log4j/roll-by-time/app.%d{HH-mm}.log.gz"/>
|
||||
</rollingPolicy>
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n"/>
|
||||
</layout>
|
||||
</appender>
|
||||
<appender name="roll-by-time-and-size"
|
||||
class="org.apache.log4j.rolling.RollingFileAppender">
|
||||
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
|
||||
<param name="FileNamePattern"
|
||||
value="target/log4j/roll-by-time-and-size/app.%d{HH-mm}.%i.log.gz"/>
|
||||
</rollingPolicy>
|
||||
<triggeringPolicy
|
||||
class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
|
||||
<param name="MaxFileSize" value="50000"/>
|
||||
</triggeringPolicy>
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n"/>
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<!--Override log level for specified package -->
|
||||
<category name="com.baeldung.log4j">
|
||||
<priority value="TRACE"/>
|
||||
</category>
|
||||
|
||||
<category name="com.baeldung.log4j.Log4jRollingExample">
|
||||
<priority value="TRACE"/>
|
||||
<appender-ref ref="roll-by-size"/>
|
||||
<appender-ref ref="roll-by-size-2"/>
|
||||
<appender-ref ref="roll-by-window"/>
|
||||
<appender-ref ref="roll-by-time"/>
|
||||
<appender-ref ref="roll-by-time-and-size"/>
|
||||
</category>
|
||||
|
||||
<root>
|
||||
<level value="DEBUG"/>
|
||||
<appender-ref ref="stdout"/>
|
||||
|
|
|
@ -8,16 +8,70 @@
|
|||
</Console>
|
||||
|
||||
# File appender
|
||||
<File name="fout" fileName="log4j/target/baeldung-log4j2.log" immediateFlush="false" append="false">
|
||||
<File name="fout" fileName="log4j/target/baeldung-log4j2.log"
|
||||
immediateFlush="false" append="false">
|
||||
# Pattern of log message for file appender
|
||||
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %p %m%n"/>
|
||||
</File>
|
||||
|
||||
# Rolling appender
|
||||
<RollingFile name="roll-by-size"
|
||||
fileName="target/log4j2/roll-by-size/app.log" filePattern="target/log4j2/roll-by-size/app.%i.log.gz"
|
||||
ignoreExceptions="false">
|
||||
<PatternLayout>
|
||||
<Pattern>%d{yyyy-MM-dd HH:mm:ss} %p %m%n</Pattern>
|
||||
</PatternLayout>
|
||||
<Policies>
|
||||
<OnStartupTriggeringPolicy/>
|
||||
<SizeBasedTriggeringPolicy
|
||||
size="5 KB"/>
|
||||
</Policies>
|
||||
</RollingFile>
|
||||
|
||||
<RollingFile name="roll-by-time"
|
||||
fileName="target/log4j2/roll-by-time/app.log"
|
||||
filePattern="target/log4j2/roll-by-time/app.%d{MM-dd-yyyy-HH-mm}.log.gz"
|
||||
ignoreExceptions="false">
|
||||
<PatternLayout>
|
||||
<Pattern>%d{yyyy-MM-dd HH:mm:ss} %p %m%n</Pattern>
|
||||
</PatternLayout>
|
||||
<TimeBasedTriggeringPolicy/>
|
||||
</RollingFile>
|
||||
|
||||
<RollingFile name="roll-by-time-and-size"
|
||||
fileName="target/log4j2/roll-by-time-and-size/app.log"
|
||||
filePattern="target/log4j2/roll-by-time-and-size/app.%d{MM-dd-yyyy-HH-mm}.%i.log.gz"
|
||||
ignoreExceptions="false">
|
||||
<PatternLayout>
|
||||
<Pattern>%d{yyyy-MM-dd HH:mm:ss} %p %m%n</Pattern>
|
||||
</PatternLayout>
|
||||
<Policies>
|
||||
<OnStartupTriggeringPolicy/>
|
||||
<SizeBasedTriggeringPolicy
|
||||
size="5 KB"/>
|
||||
<TimeBasedTriggeringPolicy/>
|
||||
</Policies>
|
||||
<DefaultRolloverStrategy>
|
||||
<Delete basePath="${baseDir}" maxDepth="2">
|
||||
<IfFileName
|
||||
glob="target/log4j2/roll-by-time-and-size/app.*.log.gz"/>
|
||||
<IfLastModified age="20s"/>
|
||||
</Delete>
|
||||
</DefaultRolloverStrategy>
|
||||
</RollingFile>
|
||||
</Appenders>
|
||||
|
||||
<Loggers>
|
||||
# Override log level for specified package
|
||||
<Logger name="com.baeldung.log4j2" level="TRACE"/>
|
||||
|
||||
<Logger name="com.baeldung.log4j2.Log4j2RollingExample"
|
||||
level="TRACE">
|
||||
<AppenderRef ref="roll-by-size"/>
|
||||
<AppenderRef ref="roll-by-time"/>
|
||||
<AppenderRef ref="roll-by-time-and-size"/>
|
||||
</Logger>
|
||||
|
||||
<AsyncRoot level="DEBUG">
|
||||
<AppenderRef ref="stdout"/>
|
||||
<AppenderRef ref="fout"/>
|
||||
|
|
|
@ -18,8 +18,65 @@
|
|||
</encoder>
|
||||
</appender>
|
||||
|
||||
# Rolling appenders
|
||||
<appender name="roll-by-size"
|
||||
class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>target/slf4j/roll-by-size/app.log</file>
|
||||
<rollingPolicy
|
||||
class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
|
||||
<fileNamePattern>target/slf4j/roll-by-size/app.%i.log.zip
|
||||
</fileNamePattern>
|
||||
<minIndex>1</minIndex>
|
||||
<maxIndex>3</maxIndex>
|
||||
<totalSizeCap>1MB</totalSizeCap>
|
||||
</rollingPolicy>
|
||||
<triggeringPolicy
|
||||
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
|
||||
<maxFileSize>5KB</maxFileSize>
|
||||
</triggeringPolicy>
|
||||
<encoder>
|
||||
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="roll-by-time"
|
||||
class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>target/slf4j/roll-by-time/app.log</file>
|
||||
<rollingPolicy
|
||||
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<fileNamePattern>target/slf4j/roll-by-time/app.%d{yyyy-MM-dd-HH-mm}.log.zip
|
||||
</fileNamePattern>
|
||||
<maxHistory>20</maxHistory>
|
||||
<totalSizeCap>1MB</totalSizeCap>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss} %p %m%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="roll-by-time-and-size"
|
||||
class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>target/slf4j/roll-by-time-and-size/app.log</file>
|
||||
<rollingPolicy
|
||||
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
|
||||
<fileNamePattern>target/slf4j/roll-by-time-and-size/app.%d{yyyy-MM-dd-mm}.%i.log.zip
|
||||
</fileNamePattern>
|
||||
<maxFileSize>5KB</maxFileSize>
|
||||
<maxHistory>20</maxHistory>
|
||||
<totalSizeCap>1MB</totalSizeCap>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss} %p %m%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
# Override log level for specified package
|
||||
<logger name="com.baeldung.logback" level="TRACE"/>
|
||||
<logger name="com.baeldung.slf4j.Slf4jRollingExample" level="TRACE">
|
||||
<appender-ref ref="roll-by-size"/>
|
||||
<appender-ref ref="roll-by-time"/>
|
||||
<appender-ref ref="roll-by-time-and-size"/>
|
||||
</logger>
|
||||
|
||||
<root level="DEBUG">
|
||||
<appender-ref ref="stdout"/>
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
###Relevant Articles:
|
||||
- [Quick Guide to MapStruct](http://www.baeldung.com/mapstruct)
|
|
@ -94,6 +94,9 @@
|
|||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<testFailureIgnore>true</testFailureIgnore>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
###Relevant Articles:
|
||||
- [A Guide to the Front Controller Pattern in Java](http://www.baeldung.com/java-front-controller-pattern)
|
||||
- [Introduction to Intercepting Filter Pattern in Java](http://www.baeldung.com/intercepting-filter-pattern-in-java)
|
|
@ -0,0 +1,2 @@
|
|||
### Relevant Articles:
|
||||
- [PDF Conversions in Java](http://www.baeldung.com/pdf-conversions-java)
|
|
@ -0,0 +1,4 @@
|
|||
###Relevant Articles:
|
||||
- [REST API with Play Framework in Java](http://www.baeldung.com/rest-api-with-play)
|
||||
- [Routing In Play Applications in Java](http://www.baeldung.com/routing-in-play)
|
||||
- [Introduction To Play In Java](http://www.baeldung.com/java-intro-to-the-play-framework)
|
8
pom.xml
8
pom.xml
|
@ -1,5 +1,6 @@
|
|||
<?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">
|
||||
<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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
|
@ -117,6 +118,7 @@
|
|||
<module>spring-mvc-velocity</module>
|
||||
<module>spring-mvc-web-vs-initializer</module>
|
||||
<module>spring-mvc-xml</module>
|
||||
<module>spring-mvc-simple</module>
|
||||
<module>spring-openid</module>
|
||||
<module>spring-protobuf</module>
|
||||
<module>spring-quartz</module>
|
||||
|
@ -150,7 +152,7 @@
|
|||
<module>xml</module>
|
||||
<module>xmlunit2</module>
|
||||
<module>xstream</module>
|
||||
<module>pdf</module>
|
||||
</modules>
|
||||
<module>pdf</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
|
@ -1,7 +1,6 @@
|
|||
<?xml version="1.0"?>
|
||||
<project
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
@ -44,16 +43,29 @@
|
|||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*LongRunningUnitTest.java</exclude>
|
||||
<exclude>**/*ManualTest.java</exclude>
|
||||
</excludes>
|
||||
<testFailureIgnore>true</testFailureIgnore>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
||||
|
||||
<jedis.version>2.9.0</jedis.version>
|
||||
<embedded-redis.version>0.6</embedded-redis.version>
|
||||
|
||||
|
||||
<!-- testing -->
|
||||
<junit.version>4.12</junit.version>
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ This article will demonstrate how to configure and use Apache Camel with Spring
|
|||
<ul>
|
||||
<li><a href="http://camel.apache.org/">Apache Camel</a></li>
|
||||
<li><a href="http://www.enterpriseintegrationpatterns.com/patterns/messaging/toc.html">Enterprise Integration Patterns</a></li>
|
||||
<li><a href="http://www.baeldung.com/apache-camel-intro">Introduction To Apache Camel</a></li>
|
||||
</ul>
|
||||
|
||||
<b><h2>Framework Versions:</h2></b>
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
<properties>
|
||||
<env.camel.version>2.16.1</env.camel.version>
|
||||
<env.spring.version>4.3.4.RELEASE</env.spring.version>
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
<java.version>1.8</java.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
</properties>
|
||||
|
@ -48,7 +48,11 @@
|
|||
<artifactId>spring-context</artifactId>
|
||||
<version>${env.spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<version>1.7.21</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package com.baeldung.camel.file;
|
||||
|
||||
import org.apache.camel.Exchange;
|
||||
import org.apache.camel.LoggingLevel;
|
||||
import org.apache.camel.Processor;
|
||||
import org.apache.camel.builder.RouteBuilder;
|
||||
|
||||
public class DeadLetterChannelFileRouter extends RouteBuilder {
|
||||
|
@ -10,13 +8,11 @@ public class DeadLetterChannelFileRouter extends RouteBuilder {
|
|||
|
||||
@Override
|
||||
public void configure() throws Exception {
|
||||
errorHandler(deadLetterChannel("log:dead?level=ERROR").maximumRedeliveries(3).redeliveryDelay(1000).retryAttemptedLogLevel(LoggingLevel.ERROR));
|
||||
errorHandler(deadLetterChannel("log:dead?level=ERROR").maximumRedeliveries(3)
|
||||
.redeliveryDelay(1000).retryAttemptedLogLevel(LoggingLevel.ERROR));
|
||||
|
||||
from("file://" + SOURCE_FOLDER + "?delete=true").process(new Processor() {
|
||||
@Override
|
||||
public void process(Exchange exchange) throws Exception {
|
||||
throw new IllegalArgumentException("Exception thrown!");
|
||||
}
|
||||
from("file://" + SOURCE_FOLDER + "?delete=true").process((exchange) -> {
|
||||
throw new IllegalArgumentException("Exception thrown!");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
|
||||
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
|
||||
|
||||
<bean id="contentBasedFileRouter" class="org.baeldung.camel.file.ContentBasedFileRouter" />
|
||||
<bean id="contentBasedFileRouter" class="com.baeldung.camel.file.ContentBasedFileRouter" />
|
||||
|
||||
<camelContext xmlns="http://camel.apache.org/schema/spring">
|
||||
<routeBuilder ref="contentBasedFileRouter" />
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
|
||||
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
|
||||
|
||||
<bean id="deadLetterChannelFileRouter" class="org.baeldung.camel.file.DeadLetterChannelFileRouter" />
|
||||
<bean id="deadLetterChannelFileRouter" class="com.baeldung.camel.file.DeadLetterChannelFileRouter" />
|
||||
|
||||
<camelContext xmlns="http://camel.apache.org/schema/spring">
|
||||
<routeBuilder ref="deadLetterChannelFileRouter" />
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
|
||||
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
|
||||
|
||||
<bean id="messageTranslatorFileRouter" class="org.baeldung.camel.file.MessageTranslatorFileRouter" />
|
||||
<bean id="messageTranslatorFileRouter" class="com.baeldung.camel.file.MessageTranslatorFileRouter" />
|
||||
|
||||
<camelContext xmlns="http://camel.apache.org/schema/spring">
|
||||
<routeBuilder ref="messageTranslatorFileRouter" />
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
|
||||
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
|
||||
|
||||
<bean id="multicastFileRouter" class="org.baeldung.camel.file.MulticastFileRouter" />
|
||||
<bean id="multicastFileRouter" class="com.baeldung.camel.file.MulticastFileRouter" />
|
||||
|
||||
<camelContext xmlns="http://camel.apache.org/schema/spring">
|
||||
<routeBuilder ref="multicastFileRouter" />
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
|
||||
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
|
||||
|
||||
<bean id="splitterFileRouter" class="org.baeldung.camel.file.SplitterFileRouter" />
|
||||
<bean id="splitterFileRouter" class="com.baeldung.camel.file.SplitterFileRouter" />
|
||||
|
||||
<camelContext xmlns="http://camel.apache.org/schema/spring">
|
||||
<routeBuilder ref="splitterFileRouter" />
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package org.apache.camel.file.processor;
|
||||
package com.apache.camel.file.processor;
|
||||
|
||||
import java.io.File;
|
||||
|
|
@ -1,9 +1,8 @@
|
|||
package org.apache.camel.file.processor;
|
||||
package com.apache.camel.file.processor;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.apache.camel.file.processor;
|
||||
package com.apache.camel.file.processor;
|
||||
|
||||
import java.io.File;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.apache.camel.file.processor;
|
||||
package com.apache.camel.file.processor;
|
||||
|
||||
import java.io.File;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.apache.camel.file.processor;
|
||||
package com.apache.camel.file.processor;
|
||||
|
||||
import java.io.File;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.apache.camel.file.processor;
|
||||
package com.apache.camel.file.processor;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
|
@ -1,4 +1,4 @@
|
|||
package org.apache.camel.main;
|
||||
package com.apache.camel.main;
|
||||
|
||||
import com.baeldung.camel.main.App;
|
||||
import junit.framework.TestCase;
|
|
@ -1,68 +0,0 @@
|
|||
package org.apache.camel.file.processor;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.apache.camel.CamelContext;
|
||||
import org.apache.camel.builder.RouteBuilder;
|
||||
import org.apache.camel.impl.DefaultCamelContext;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
import com.baeldung.camel.file.FileProcessor;
|
||||
|
||||
|
||||
public class FileProcessorTest {
|
||||
|
||||
private static final long DURATION_MILIS = 10000;
|
||||
private static final String SOURCE_FOLDER = "src/test/source-folder";
|
||||
private static final String DESTINATION_FOLDER = "src/test/destination-folder";
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
File sourceFolder = new File(SOURCE_FOLDER);
|
||||
File destinationFolder = new File(DESTINATION_FOLDER);
|
||||
|
||||
cleanFolder(sourceFolder);
|
||||
cleanFolder(destinationFolder);
|
||||
|
||||
sourceFolder.mkdirs();
|
||||
File file1 = new File(SOURCE_FOLDER + "/File1.txt");
|
||||
File file2 = new File(SOURCE_FOLDER + "/File2.txt");
|
||||
file1.createNewFile();
|
||||
file2.createNewFile();
|
||||
}
|
||||
|
||||
private void cleanFolder(File folder) {
|
||||
File[] files = folder.listFiles();
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
if (file.isFile()) {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void moveFolderContentJavaDSLTest() throws Exception {
|
||||
final CamelContext camelContext = new DefaultCamelContext();
|
||||
camelContext.addRoutes(new RouteBuilder() {
|
||||
@Override
|
||||
public void configure() throws Exception {
|
||||
from("file://" + SOURCE_FOLDER + "?delete=true").process(new FileProcessor()).to("file://" + DESTINATION_FOLDER);
|
||||
}
|
||||
});
|
||||
camelContext.start();
|
||||
Thread.sleep(DURATION_MILIS);
|
||||
camelContext.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void moveFolderContentSpringDSLTest() throws InterruptedException {
|
||||
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context-test.xml");
|
||||
Thread.sleep(DURATION_MILIS);
|
||||
applicationContext.close();
|
||||
|
||||
}
|
||||
}
|
|
@ -5,3 +5,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||
- [Quick Guide to @RestClientTest in Spring Boot](http://www.baeldung.com/restclienttest-in-spring-boot)
|
||||
- [Intro to Spring Boot Starters](http://www.baeldung.com/spring-boot-starters)
|
||||
- [A Guide to Spring in Eclipse STS](http://www.baeldung.com/eclipse-sts-spring)
|
||||
- [Introduction to WebJars](http://www.baeldung.com/maven-webjars)
|
||||
|
|
|
@ -112,23 +112,4 @@
|
|||
</profile>
|
||||
</profiles>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -77,23 +77,4 @@
|
|||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -1,15 +1,18 @@
|
|||
<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>
|
||||
<groupId>org.baeldung.spring.cloud</groupId>
|
||||
<artifactId>spring-cloud-data-flow</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>data-flow-server</module>
|
||||
<module>data-flow-shell</module>
|
||||
<module>time-source</module>
|
||||
<module>time-processor</module>
|
||||
<module>log-sink</module>
|
||||
<module>batch-job</module>
|
||||
</modules>
|
||||
<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>
|
||||
<groupId>org.baeldung.spring.cloud</groupId>
|
||||
<artifactId>spring-cloud-data-flow</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>data-flow-server</module>
|
||||
<!-- <module>data-flow-shell</module> -->
|
||||
<module>time-source</module>
|
||||
<module>time-processor</module>
|
||||
<module>log-sink</module>
|
||||
<module>batch-job</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -16,4 +16,5 @@
|
|||
- [Dockerizing a Spring Boot Application](http://www.baeldung.com/dockerizing-spring-boot-application)
|
||||
- [Introduction to Spring Cloud Rest Client with Netflix Ribbon](http://www.baeldung.com/spring-cloud-rest-client-with-netflix-ribbon)
|
||||
|
||||
|
||||
### Relevant Articles:
|
||||
- [Introduction to Spring Cloud Rest Client with Netflix Ribbon](http://www.baeldung.com/spring-cloud-rest-client-with-netflix-ribbon)
|
||||
|
|
|
@ -1,80 +1,80 @@
|
|||
<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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-cloud-ribbon</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>spring-cloud-ribbon-client</name>
|
||||
<description>Introduction to Spring Cloud Rest Client with Netflix Ribbon</description>
|
||||
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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-cloud-ribbon</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>spring-cloud-ribbon-client</name>
|
||||
<description>Introduction to Spring Cloud Rest Client with Netflix Ribbon</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.4.2.RELEASE</version>
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.4.2.RELEASE</version>
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<spring-cloud-dependencies.version>Brixton.SR7</spring-cloud-dependencies.version>
|
||||
</properties>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<spring-cloud-dependencies.version>Brixton.SR7</spring-cloud-dependencies.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-ribbon</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-ribbon</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring-cloud-dependencies.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring-cloud-dependencies.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
</project>
|
|
@ -20,7 +20,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||
@SuppressWarnings("unused")
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = ServerLocationApp.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public class ServerLocationAppTests {
|
||||
public class ServerLocationAppIntegrationTest {
|
||||
ConfigurableApplicationContext application2;
|
||||
ConfigurableApplicationContext application3;
|
||||
|
|
@ -2,4 +2,4 @@
|
|||
- [Wiring in Spring: @Autowired, @Resource and @Inject](http://www.baeldung.com/spring-annotations-resource-inject-autowire)
|
||||
- [Exploring the Spring BeanFactory API](http://www.baeldung.com/spring-beanfactory)
|
||||
- [How to use the Spring FactoryBean?](http://www.baeldung.com/spring-factorybean)
|
||||
|
||||
- [Constructor Dependency Injection in Spring](http://www.baeldung.com/constructor-injection-in-spring)
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue