Merge remote-tracking branch 'eugenp/master'

This commit is contained in:
DOHA 2017-10-02 13:09:00 +02:00
commit dfc0a979d3
22 changed files with 557 additions and 47 deletions

View File

@ -34,6 +34,11 @@
<artifactId>jenetics</artifactId> <artifactId>jenetics</artifactId>
<version>3.7.0</version> <version>3.7.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-core</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

1
atomix/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

46
atomix/pom.xml Normal file
View File

@ -0,0 +1,46 @@
<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.atomix.io</groupId>
<artifactId>atomix</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>io.atomix</groupId>
<artifactId>atomix-all</artifactId>
<version>1.0.0-rc9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,27 @@
package com.atomix.example;
import io.atomix.AtomixReplica;
import io.atomix.catalyst.transport.Address;
import io.atomix.catalyst.transport.netty.NettyTransport;
import io.atomix.copycat.server.storage.Storage;
import io.atomix.copycat.server.storage.StorageLevel;
import java.io.File;
import java.util.concurrent.CompletableFuture;
public class BootstrapingCluster {
public static void main(String[] args) {
Storage storage = Storage.builder()
.withDirectory(new File("log"))
.withStorageLevel(StorageLevel.DISK)
.build();
AtomixReplica replica = AtomixReplica.builder(new Address("localhost", 8700))
.withStorage(storage)
.withTransport(new NettyTransport())
.build();
CompletableFuture<AtomixReplica> completableFuture = replica.bootstrap();
completableFuture.join();
}
}

View File

@ -0,0 +1,71 @@
package com.atomix.example;
import io.atomix.AtomixReplica;
import io.atomix.catalyst.transport.Address;
import io.atomix.catalyst.transport.netty.NettyTransport;
import io.atomix.concurrent.DistributedLock;
import io.atomix.copycat.server.storage.Storage;
import io.atomix.copycat.server.storage.StorageLevel;
import java.io.File;
import java.util.Arrays;
import java.util.List;
public class OtherNodes {
public static void main(String[] args) throws InterruptedException {
List<Address> cluster = Arrays
.asList(
new Address("localhost", 8700),
new Address("localhost", 8701),
new Address("localhost", 8702));
Storage storage = Storage.builder()
.withDirectory(new File("log"))
.withStorageLevel(StorageLevel.DISK)
.build();
AtomixReplica replica2 = AtomixReplica.builder(new Address("localhost", 8701))
.withStorage(storage)
.withTransport(new NettyTransport())
.build();
WorkerThread WT1 = new WorkerThread(replica2, cluster);
WT1.run();
AtomixReplica replica3 = AtomixReplica.builder(new Address("localhost", 8702))
.withStorage(storage)
.withTransport(new NettyTransport())
.build();
WorkerThread WT2 = new WorkerThread(replica3, cluster);
WT2.run();
Thread.sleep(6000);
DistributedLock lock = replica2.getLock("my-lock")
.join();
lock.lock()
.thenRun(() -> System.out.println("Acquired a lock"));
replica2.getMap("map")
.thenCompose(m -> m.put("bar", "Hello world!"))
.thenRun(() -> System.out.println("Value is set in Distributed Map"))
.join();
}
private static class WorkerThread extends Thread {
private AtomixReplica replica;
private List<Address> cluster;
WorkerThread(AtomixReplica replica, List<Address> cluster) {
this.replica = replica;
this.cluster = cluster;
}
public void run() {
replica.join(cluster)
.join();
}
}
}

View File

@ -0,0 +1,35 @@
package com.atomix.exampletest;
import io.atomix.AtomixClient;
import io.atomix.catalyst.transport.Address;
import io.atomix.catalyst.transport.netty.NettyTransport;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import static org.junit.Assert.assertEquals;
public class AtomixClientLiveTest {
private final AtomixClient client = AtomixClient.builder()
.withTransport(new NettyTransport())
.build();
@Test
public void whenBootstrap_thenShouldGet() throws InterruptedException, ExecutionException {
List<Address> cluster = Arrays.asList(
new Address("localhost", 8700),
new Address("localhsot", 8701));
String value = client.connect(cluster)
.thenRun(() -> System.out.println("Client Connected"))
.thenCompose(c -> client.getMap("map"))
.thenCompose(m -> m.get("bar"))
.thenApply(a -> (String) a)
.get();
assertEquals("Hello world!", value);
}
}

View File

@ -585,11 +585,6 @@
<artifactId>fugue</artifactId> <artifactId>fugue</artifactId>
<version>3.0.0-m007</version> <version>3.0.0-m007</version>
</dependency> </dependency>
<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-core</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies> </dependencies>
<repositories> <repositories>
<repository> <repository>

View File

@ -0,0 +1,72 @@
package com.baeldung.jdo.xml;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jdo.annotations.Element;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.PrimaryKey;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
@PersistenceCapable(
schema="/myproduct/people",
table="person"
)
public class AnnotadedPerson {
@XmlAttribute
private long personNum;
@PrimaryKey
private String firstName;
private String lastName;
@XmlElementWrapper(name="phone-numbers")
@XmlElement(name="phone-number")
@Element(types=String.class)
private List phoneNumbers = new ArrayList();
public AnnotadedPerson(long personNum, String firstName, String lastName) {
super();
this.personNum = personNum;
this.firstName = firstName;
this.lastName = lastName;
}
public long getPersonNum() {
return personNum;
}
public void setPersonNum(long personNum) {
this.personNum = personNum;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public List getPhoneNumbers() {
return phoneNumbers;
}
public void setPhoneNumbers(List phoneNumbers) {
this.phoneNumbers = phoneNumbers;
}
}

View File

@ -0,0 +1,105 @@
package com.baeldung.jdo.xml;
import java.util.List;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.jdo.Transaction;
import org.datanucleus.api.jdo.JDOPersistenceManagerFactory;
import org.datanucleus.metadata.PersistenceUnitMetaData;
public class MyApp {
private static PersistenceUnitMetaData pumd;
private static PersistenceManagerFactory pmf;
private static PersistenceManager pm;
public static void main( String[] args ) {
//persist product object using dynamic persistence unit
defineDynamicPersistentUnit();
Product product = new Product("id1","Sony Discman", "A standard discman from Sony", 49.99);
persistObject(product);
closePersistenceManager();
//persist AnnotatedPerson object using named pmf
defineNamedPersistenceManagerFactory("XmlDatastore");
AnnotadedPerson annotatedPerson = new AnnotadedPerson(654320,"annotated","person");
annotatedPerson.getPhoneNumbers().add("999999999");
annotatedPerson.getPhoneNumbers().add("000000000");
persistObject(annotatedPerson);
queryAnnotatedPersonsInXML();
closePersistenceManager();
//persist Person object using PMF created by properties file
definePersistenceManagerFactoryUsingPropertiesFile("META-INF\\datanucleus.properties");
Person person = new Person(654321,"bealdung","author");
person.getPhoneNumbers().add("123456789");
person.getPhoneNumbers().add("987654321");
persistObject(person);
queryPersonsInXML();
closePersistenceManager();
}
public static void defineDynamicPersistentUnit(){
PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
pumd.addProperty("javax.jdo.option.ConnectionURL", "xml:file:myfile_dynamicPMF.xml");
pumd.addProperty("datanucleus.schema.autoCreateAll", "true");
pumd.addProperty("datanucleus.xml.indentSize", "4");
pmf = new JDOPersistenceManagerFactory(pumd, null);
pm = pmf.getPersistenceManager();
}
public static void defineNamedPersistenceManagerFactory(String pmfName){
pmf = JDOHelper.getPersistenceManagerFactory("XmlDatastore");
pm = pmf.getPersistenceManager();
}
public static void definePersistenceManagerFactoryUsingPropertiesFile(String filePath){
pmf = JDOHelper.getPersistenceManagerFactory(filePath);
pm = pmf.getPersistenceManager();
}
public static void closePersistenceManager(){
if(pm!=null && !pm.isClosed()){
pm.close();
}
}
public static void persistObject(Object obj){
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(obj);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
}
public static void queryPersonsInXML(){
Query<Person> query = pm.newQuery(Person.class);
List<Person> result = query.executeList();
System.out.println("name: "+result.get(0).getFirstName());
}
public static void queryAnnotatedPersonsInXML(){
Query<AnnotadedPerson> query = pm.newQuery(AnnotadedPerson.class);
List<AnnotadedPerson> result = query.executeList();
System.out.println("name: "+result.get(0).getFirstName());
}
}

View File

@ -0,0 +1,65 @@
package com.baeldung.jdo.xml;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jdo.annotations.Element;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.PrimaryKey;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
@PersistenceCapable
public class Person {
private long personNum;
@PrimaryKey
private String firstName;
private String lastName;
private List phoneNumbers = new ArrayList();
public Person(long personNum, String firstName, String lastName) {
super();
this.personNum = personNum;
this.firstName = firstName;
this.lastName = lastName;
}
public long getPersonNum() {
return personNum;
}
public void setPersonNum(long personNum) {
this.personNum = personNum;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public List getPhoneNumbers() {
return phoneNumbers;
}
public void setPhoneNumbers(List phoneNumbers) {
this.phoneNumbers = phoneNumbers;
}
}

View File

@ -0,0 +1,58 @@
package com.baeldung.jdo.xml;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceAware;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
@PersistenceCapable
public class Product {
@PrimaryKey
String id;
String name;
String description;
double price;
public Product(){
}
public Product(String id,String name,String description,double price){
this.id = id;
this.name=name;
this.description = description;
this.price = price;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}

View File

@ -0,0 +1,4 @@
javax.jdo.PersistenceManagerFactoryClass=org.datanucleus.api.jdo.JDOPersistenceManagerFactory
javax.jdo.option.ConnectionURL= xml:file:myfile-ds.xml
datanucleus.xml.indentSize=6
datanucleus.schema.autoCreateAll=true

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<jdoconfig xmlns="http://xmlns.jcp.org/xml/ns/jdo/jdoconfig"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/jdo/jdoconfig
http://xmlns.jcp.org/xml/ns/jdo/jdoconfig_3_2.xsd"
version="3.2">
<!-- Datastore Txn PMF -->
<persistence-manager-factory name="XmlDatastore">
<property name="javax.jdo.PersistenceManagerFactoryClass"
value="org.datanucleus.api.jdo.JDOPersistenceManagerFactory" />
<property name="javax.jdo.option.ConnectionURL" value="xml:file:namedPMF-ds.xml" />
<property name="datanucleus.xml.indentSize" value="6" />
<property name="datanucleus.schema.autoCreateAll"
value="true" />
</persistence-manager-factory>
</jdoconfig>

View File

@ -0,0 +1,21 @@
<jdo xmlns="http://xmlns.jcp.org/xml/ns/jdo/jdo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/jdo/jdo http://xmlns.jcp.org/xml/ns/jdo/jdo_3_1.xsd"
version="3.1">
<package name="com.baeldung.jdo.xml">
<class name="Person" detachable="true" schema="/myproduct/people"
table="person">
<field name="personNum">
<extension vendor-name="datanucleus" key="XmlAttribute"
value="true" />
</field>
<field name="firstName" primary-key="true" /> <!-- PK since JAXB requires String -->
<field name="lastName" />
<field name="phoneNumbers">
<collection element-type="java.lang.String" />
<element column="phoneNumber" />
</field>
</class>
</package>
</jdo>

View File

@ -28,6 +28,7 @@
</properties> </properties>
<modules> <modules>
<module>atomix</module>
<module>apache-cayenne</module> <module>apache-cayenne</module>
<module>aws</module> <module>aws</module>
<module>akka-streams</module> <module>akka-streams</module>

View File

@ -10,6 +10,7 @@ import javax.json.bind.annotation.JsonbTransient;
public class Person { public class Person {
private int id;
@JsonbProperty("person-name") @JsonbProperty("person-name")
private String name; private String name;
@JsonbProperty(nillable = true) @JsonbProperty(nillable = true)
@ -23,8 +24,9 @@ public class Person {
public Person() { public Person() {
} }
public Person(String name, String email, int age, LocalDate registeredDate, BigDecimal salary) { public Person(int id, String name, String email, int age, LocalDate registeredDate, BigDecimal salary) {
super(); super();
this.id = id;
this.name = name; this.name = name;
this.email = email; this.email = email;
this.age = age; this.age = age;
@ -32,6 +34,14 @@ public class Person {
this.salary = salary; this.salary = salary;
} }
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() { public int getAge() {
return age; return age;
} }
@ -76,7 +86,9 @@ public class Person {
@Override @Override
public String toString() { public String toString() {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.append("Person [name="); builder.append("Person [id=");
builder.append(id);
builder.append(", name=");
builder.append(name); builder.append(name);
builder.append(", email="); builder.append(", email=");
builder.append(email); builder.append(email);
@ -94,11 +106,7 @@ public class Person {
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
result = prime * result + age; result = prime * result + id;
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((registeredDate == null) ? 0 : registeredDate.hashCode());
result = prime * result + ((salary == null) ? 0 : salary.hashCode());
return result; return result;
} }
@ -111,27 +119,7 @@ public class Person {
if (getClass() != obj.getClass()) if (getClass() != obj.getClass())
return false; return false;
Person other = (Person) obj; Person other = (Person) obj;
if (age != other.age) if (id != other.id)
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (registeredDate == null) {
if (other.registeredDate != null)
return false;
} else if (!registeredDate.equals(other.registeredDate))
return false;
if (salary == null) {
if (other.salary != null)
return false;
} else if (!salary.equals(other.salary))
return false; return false;
return true; return true;
} }

View File

@ -26,12 +26,12 @@ public class PersonController {
public void init() { public void init() {
// @formatter:off // @formatter:off
personRepository = new ArrayList<>(Arrays.asList( personRepository = new ArrayList<>(Arrays.asList(
new Person("Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)), new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
new Person("Jhon", "jhon1@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)), new Person(2, "Jhon", "jhon1@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)),
new Person("Jhon", null, 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)), new Person(3, "Jhon", null, 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
new Person("Tom", "tom@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)), new Person(4, "Tom", "tom@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)),
new Person("Mark", "mark@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1200)), new Person(5, "Mark", "mark@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1200)),
new Person("Julia", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)))); new Person(6, "Julia", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000))));
// @formatter:on // @formatter:on
} }

View File

@ -30,15 +30,14 @@ public class JsonbIntegrationTest {
ResponseEntity<Person> response = template.withBasicAuth(username, password) ResponseEntity<Person> response = template.withBasicAuth(username, password)
.getForEntity("/person/1", Person.class); .getForEntity("/person/1", Person.class);
Person person = response.getBody(); Person person = response.getBody();
assertTrue(person.equals(new Person("Jhon", "jhon1@test.com", 0, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500.0)))); assertTrue(person.equals(new Person(2, "Jhon", "jhon1@test.com", 0, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500.0))));
} }
@Test @Test
public void whenSendPostAPerson_thenGetOkStatus() { public void whenSendPostAPerson_thenGetOkStatus() {
ResponseEntity<Boolean> response = template.withBasicAuth(username, password) ResponseEntity<Boolean> response = template.withBasicAuth(username, password)
.postForEntity("/person", "{\"birthDate\":\"07-09-2017\",\"email\":\"jhon1@test.com\",\"person-name\":\"Jhon\"}", Boolean.class); .postForEntity("/person", "{\"birthDate\":\"07-09-2017\",\"email\":\"jhon1@test.com\",\"person-name\":\"Jhon\",\"id\":10}", Boolean.class);
boolean value = response.getBody(); assertTrue(response.getBody());
assertTrue(true == value);
} }
} }

View File

@ -22,15 +22,15 @@ public class JsonbTest {
@Test @Test
public void givenPersonObject_whenSerializeWithJsonb_thenGetPersonJson() { public void givenPersonObject_whenSerializeWithJsonb_thenGetPersonJson() {
Person person = new Person("Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000)); Person person = new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000));
String jsonPerson = jsonb.toJson(person); String jsonPerson = jsonb.toJson(person);
assertTrue("{\"email\":\"jhon@test.com\",\"person-name\":\"Jhon\",\"registeredDate\":\"07-09-2019\",\"salary\":\"1000.0\"}".equals(jsonPerson)); assertTrue("{\"email\":\"jhon@test.com\",\"id\":1,\"person-name\":\"Jhon\",\"registeredDate\":\"07-09-2019\",\"salary\":\"1000.0\"}".equals(jsonPerson));
} }
@Test @Test
public void givenPersonJson_whenDeserializeWithJsonb_thenGetPersonObject() { public void givenPersonJson_whenDeserializeWithJsonb_thenGetPersonObject() {
Person person = new Person("Jhon", "jhon@test.com", 0, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000.0)); Person person = new Person(1, "Jhon", "jhon@test.com", 0, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000.0));
String jsonPerson = "{\"email\":\"jhon@test.com\",\"person-name\":\"Jhon\",\"registeredDate\":\"07-09-2019\",\"salary\":\"1000.0\"}"; String jsonPerson = "{\"email\":\"jhon@test.com\",\"id\":1,\"person-name\":\"Jhon\",\"registeredDate\":\"07-09-2019\",\"salary\":\"1000.0\"}";
assertTrue(jsonb.fromJson(jsonPerson, Person.class) assertTrue(jsonb.fromJson(jsonPerson, Person.class)
.equals(person)); .equals(person));
} }