-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
creepid
committed
Mar 27, 2018
0 parents
commit 11ac7b3
Showing
24 changed files
with
855 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
**/target | ||
.idea | ||
*.iml | ||
nbactions.xml | ||
nb-configuration.xml | ||
nbactions-development.xml | ||
/test/nbproject/private/ | ||
/test/build/ | ||
/ui/i18n/target/ | ||
/ui/webui/target/ | ||
/scripting/nbproject/ | ||
/Main/nbproject/private/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?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>net.easysmarthouse</groupId> | ||
<artifactId>esh-client</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>esh-client</name> | ||
|
||
<parent> | ||
<groupId>net.easysmarthouse</groupId> | ||
<artifactId>esh-distribution</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.hazelcast</groupId> | ||
<artifactId>hazelcast-all</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>net.easysmarthouse</groupId> | ||
<artifactId>esh-shared</artifactId> | ||
</dependency> | ||
|
||
<!-- Test dependencies --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
<version>4.12</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>net.easysmarthouse</groupId> | ||
<artifactId>esh-storage-node</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hsqldb</groupId> | ||
<artifactId>hsqldb</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
Binary file not shown.
21 changes: 21 additions & 0 deletions
21
esh-client/src/main/java/net/easysmarthouse/distribution/client/ClientApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package net.easysmarthouse.distribution.client; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@SpringBootApplication | ||
@Configuration | ||
@ComponentScan | ||
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) | ||
public class ClientApplication { | ||
|
||
public static void main(String... args) { | ||
SpringApplication.run(ClientApplication.class, args); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...ient/src/main/java/net/easysmarthouse/distribution/client/config/ClientConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package net.easysmarthouse.distribution.client.config; | ||
|
||
import com.hazelcast.client.HazelcastClient; | ||
import com.hazelcast.client.config.ClientConfig; | ||
import com.hazelcast.core.HazelcastInstance; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) | ||
public class ClientConfiguration { | ||
|
||
@Bean(name = "HazelcastClientConfig") | ||
public ClientConfig clientConfig() { | ||
ClientConfig clientConfig = new ClientConfig(); | ||
clientConfig.getNetworkConfig().setConnectionAttemptLimit(0); | ||
return clientConfig; | ||
} | ||
|
||
@Bean(name = "ClientInstance", destroyMethod = "shutdown") | ||
public HazelcastInstance clientInstance(ClientConfig clientConfig) throws Exception { | ||
return HazelcastClient.newHazelcastClient(clientConfig); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
...t/src/main/java/net/easysmarthouse/distribution/client/service/devices/DeviceService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package net.easysmarthouse.distribution.client.service.devices; | ||
|
||
import com.hazelcast.core.HazelcastInstance; | ||
import com.hazelcast.core.IMap; | ||
import net.easysmarthouse.distribution.shared.Device; | ||
import net.easysmarthouse.distribution.shared.MapNames; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Service | ||
public class DeviceService implements MapNames { | ||
|
||
private HazelcastInstance hazelcastInstance; | ||
private IMap<Long, Device> devicesMap; | ||
|
||
@Autowired | ||
public DeviceService(@Qualifier("ClientInstance") | ||
HazelcastInstance hazelcastInstance) { | ||
this.hazelcastInstance = hazelcastInstance; | ||
} | ||
|
||
@PostConstruct | ||
public void init() { | ||
devicesMap = hazelcastInstance.getMap(DEVICES_MAP); | ||
} | ||
|
||
public void addDevice(Device device) { | ||
devicesMap.put(device.getId(), device); | ||
} | ||
|
||
public void addDevices(Collection<Device> devices) { | ||
Map<Long, Device> devicesLocalMap = new HashMap<>(); | ||
devices.forEach(device -> | ||
devicesLocalMap.put(device.getId(), device) | ||
); | ||
devicesMap.putAll(devicesLocalMap); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
___ _ _ _ | ||
/ __| (_)___ _ _| |_ | ||
| (__| | / -_) ' \ _| | ||
\___|_|_\___|_||_\__| |
27 changes: 27 additions & 0 deletions
27
...rc/test/java/net/easysmarthouse/distribution/client/HazelcastClientTestConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package net.easysmarthouse.distribution.client; | ||
|
||
import com.hazelcast.client.HazelcastClient; | ||
import com.hazelcast.client.config.ClientConfig; | ||
import com.hazelcast.core.HazelcastInstance; | ||
import net.easysmarthouse.distribution.client.helper.StorageNodeFactory; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@ComponentScan(basePackages = { | ||
"net.easysmarthouse.distribution.shared", | ||
"net.easysmarthouse.distribution.client", | ||
"net.easysmarthouse.distribution.storage", | ||
}) | ||
public class HazelcastClientTestConfiguration { | ||
|
||
@Bean(name = "ClientInstance") | ||
public HazelcastInstance clientInstance(StorageNodeFactory storageNodeFactory, | ||
ClientConfig config) throws Exception { | ||
//Ensure there is at least 1 running instance(); | ||
storageNodeFactory.ensureClusterSize(1); | ||
return HazelcastClient.newHazelcastClient(config); | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
...lient/src/test/java/net/easysmarthouse/distribution/client/helper/StorageNodeFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package net.easysmarthouse.distribution.client.helper; | ||
|
||
import com.hazelcast.core.Hazelcast; | ||
import com.hazelcast.core.HazelcastInstance; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.stereotype.Service; | ||
import com.hazelcast.config.Config; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.*; | ||
|
||
@Service | ||
public class StorageNodeFactory { | ||
|
||
private Config config; | ||
private List<HazelcastInstance> instances = Collections.synchronizedList(new ArrayList<>()); | ||
private ExecutorService executorService = Executors.newCachedThreadPool(); | ||
|
||
@Autowired | ||
public StorageNodeFactory(@Qualifier("StorageNodeConfig") Config config) { | ||
this.config = config; | ||
} | ||
|
||
public void ensureClusterSize(int size) throws InterruptedException { | ||
if (instances.size() < size) { | ||
int diff = size - instances.size(); | ||
CountDownLatch latch = new CountDownLatch(diff); | ||
for (int x = 0; x < diff; x++) { | ||
executorService.submit(new CreateHazelcastInstanceCallable(latch, config)); | ||
} | ||
latch.await(10, TimeUnit.SECONDS); | ||
} else if (instances.size() > size) { | ||
for (int x = instances.size() - 1; x >= size; x--) { | ||
HazelcastInstance instance = instances.remove(x); | ||
instance.shutdown(); | ||
} | ||
} | ||
} | ||
|
||
public final class CreateHazelcastInstanceCallable implements Callable<HazelcastInstance> { | ||
|
||
private Config config; | ||
private CountDownLatch latch; | ||
|
||
public CreateHazelcastInstanceCallable(CountDownLatch latch, Config config) { | ||
this.config = config; | ||
this.latch = latch; | ||
} | ||
|
||
@Override | ||
public HazelcastInstance call() throws Exception { | ||
HazelcastInstance instance = Hazelcast.newHazelcastInstance(config); | ||
instances.add(instance); | ||
if (latch != null) { | ||
latch.countDown(); | ||
} | ||
return instance; | ||
} | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...c/test/java/net/easysmarthouse/distribution/client/service/devices/DeviceServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package net.easysmarthouse.distribution.client.service.devices; | ||
|
||
import com.hazelcast.core.HazelcastInstance; | ||
import com.hazelcast.core.IMap; | ||
import net.easysmarthouse.distribution.client.HazelcastClientTestConfiguration; | ||
import net.easysmarthouse.distribution.shared.Device; | ||
import net.easysmarthouse.distribution.shared.DeviceType; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@SpringBootTest(classes = {HazelcastClientTestConfiguration.class}) | ||
public class DeviceServiceTest { | ||
|
||
@Autowired | ||
private DeviceService deviceService; | ||
|
||
@Autowired | ||
@Qualifier("ClientInstance") | ||
HazelcastInstance hazelcastInstance; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
hazelcastInstance.getMap(DeviceService.DEVICES_MAP).clear(); | ||
} | ||
|
||
@Test | ||
public void addDevice() { | ||
System.out.println("***** addDevice *****"); | ||
Device device = new Device(2134l, "Simple switch", "DF4534563456FF", DeviceType.Switch, "Device description"); | ||
deviceService.addDevice(device); | ||
|
||
IMap<Long, Device> devicesMap = hazelcastInstance.getMap(DeviceService.DEVICES_MAP); | ||
assertEquals(1, devicesMap.size()); | ||
assertEquals(device, devicesMap.get(2134l)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>net.easysmarthouse</groupId> | ||
<artifactId>esh-shared</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>esh-shared</name> | ||
<description>Shared library between storage nodes and main application</description> | ||
|
||
<parent> | ||
<groupId>net.easysmarthouse</groupId> | ||
<artifactId>esh-distribution</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jpa</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.hazelcast</groupId> | ||
<artifactId>hazelcast-all</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
|
||
</project> |
Oops, something went wrong.