Blueriq provides IExternalFlowStore interface which can be used to interact with a generic datastore. A default implementation is already provided in the Blueriq External Flow Component. If other datastore implementation is desired the following have to be done:
- Add the
blueriq-component-api
to the project dependencies.
<project ...>
...
<dependencies>
...
<dependency>
<groupId>com.blueriq</groupId>
<artifactId>blueriq-component-api</artifactId>
<version>${blueriq.version}</version>
</dependency>
...
</dependencies>
...
</project>
- Define the custom implementation like below:
package ...;
import com.blueriq.component.api.store.externalflow.IExternalFlowStore;
// other imports ...
@Component
public class CustomExternalFlowStore implements IExternalFlowStore {
@Override
public void delete(String key)
throws ExternalFlowStoreException, IllegalArgumentException {
// add implementation here
}
@Override
public ObjectModelRetrieval T get(String key)
throws ExternalFlowStoreException, IllegalArgumentException {
// add implementation here
return null;
}
@Override
public boolean hasKey(String key)
throws ExternalFlowStoreException, IllegalArgumentException {
// add implementation here
return false;
}
@Override
public void set(String key, ObjectModelStorage value)
throws ExternalFlowStoreException, IllegalArgumentException {
// add implementation here
}
}
Example Hazelcast Datastore
Here is an example Hazelcast implementation of the datastore.
Adding the dependencies
The following dependencies are required in the pom.xml
<project ...>
...
<dependencies>
...
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-all</artifactId>
<version>${hazelcast.version}</version>
</dependency>
<dependency>
<groupId>com.blueriq</groupId>
<artifactId>blueriq-component-api</artifactId>
<version>${blueriq.version}</version>
</dependency>
...
<dependencies>
...
</project>
Implementing the IExternalFlowStore Interface
package ...;
import com.blueriq.component.api.store.externalflow.IExternalFlowStore;
// other imports ...
@Component
public class HazelcastExternalFlowStore implements IExternalFlowStore {
private final IMap<String, byte[]> map;
public HazelcastExternalFlowStore() {
ClientConfig config = new ClientConfig();
// configure Hazelcast properties here
HazelcastInstance hazelcastInstance
= HazelcastClient.newHazelcastClient(config);
map = hazelcastInstance.getMap("blueriq");
}
@Override
public void delete(String key)
throws ExternalFlowStoreException, IllegalArgumentException {
if (isInvalid(key)) {
throw new IllegalArgumentException("Invalid key.");
}
try {
map.delete(key);
} catch (Exception e) {
throw new ExternalFlowStoreException("Something went wrong", e);
}
}
@Override
public ObjectModelRetrieval T get(String key)
throws ExternalFlowStoreException, IllegalArgumentException {
if (isInvalid(key)) {
throw new IllegalArgumentException("Invalid key.");
}
try {
byte[] storedValue = map.get(key);
if (storedValue == null) {
return null;
}
return new ObjectModel(storedValue);
} catch (Exception e) {
throw new ExternalFlowStoreException("Something went wrong", e);
}
}
@Override
public boolean hasKey(String key)
throws ExternalFlowStoreException, IllegalArgumentException {
if (isInvalid(key)) {
throw new IllegalArgumentException("Invalid key.");
}
try {
return map.get(key) != null;
} catch (Exception e) {
throw new ExternalFlowStoreException("Something went wrong", e);
}
}
@Override
public void set(String key, ObjectModelStorage value)
throws ExternalFlowStoreException, IllegalArgumentException {
if (isInvalid(key)) {
throw new IllegalArgumentException("Invalid key.");
}
if (model == null) {
throw new IllegalArgumentException("external flow model must not be null");
}
try {
map.set(key, value.toBytes(), 1, TimeUnit.MINUTES);
} catch (Exception e) {
throw new ExternalFlowStoreException("Something went wrong", e);
}
}
private boolean isInvalid(String key) {
// define what an invalid key means
return false;
}
}