Persisting the current message ID in a configuration table

This commit is contained in:
Kai S. K. Engelbart 2020-01-28 20:46:27 +01:00
parent 1a83b5ca50
commit f1a8606825
5 changed files with 117 additions and 9 deletions

View File

@ -28,7 +28,7 @@
<dependency>
<groupId>com.github.informatik-ag-ngl</groupId>
<artifactId>envoy-common</artifactId>
<version>f~login_or_registration-SNAPSHOT</version>
<version>develop-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.informatik-ag-ngl</groupId>

View File

@ -6,6 +6,7 @@ import java.util.Set;
import com.jenkov.nioserver.Server;
import envoy.server.data.ConfigItem;
import envoy.server.database.PersistenceManager;
import envoy.server.net.ObjectMessageProcessor;
import envoy.server.net.ObjectMessageReader;
@ -38,10 +39,14 @@ public class Startup {
processors.add(new IdGeneratorRequestProcessor());
Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors));
// TODO: Prevent lazy DB initialization
PersistenceManager.getPersistenceManager();
initializeCurrentMessageId();
server.start();
server.getSocketProcessor().registerSocketIdListener(ConnectionManager.getInstance());
}
private static void initializeCurrentMessageId() {
PersistenceManager persMan = PersistenceManager.getPersistenceManager();
if (persMan.getConfigItemById("currentMessageId") == null) persMan.addConfigItem(new ConfigItem("currentMessageId", "0"));
}
}

View File

@ -0,0 +1,65 @@
package envoy.server.data;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>ConfigItem.java</strong><br>
* Created: <strong>28 Jan 2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Server Standalone v0.1-alpha
*/
@Entity
@Table(name = "configuration")
public class ConfigItem {
@Id
private String key;
private String value;
/**
* Creates an instance of @link{ConfigItem}.
*
* @since Envoy Server Standalone v0.1-alpha
*/
public ConfigItem() {}
/**
* Creates an instance of @link{ConfigItem}.
*
* @param key
* @param value
* @since Envoy Server Standalone v0.1-alpha
*/
public ConfigItem(String key, String value) {
this.key = key;
this.value = value;
}
/**
* @return the key
* @since Envoy Server Standalone v0.1-alpha
*/
public String getKey() { return key; }
/**
* @param key the key to set
* @since Envoy Server Standalone v0.1-alpha
*/
public void setKey(String key) { this.key = key; }
/**
* @return the value
* @since Envoy Server Standalone v0.1-alpha
*/
public String getValue() { return value; }
/**
* @param value the value to set
* @since Envoy Server Standalone v0.1-alpha
*/
public void setValue(String value) { this.value = value; }
}

View File

@ -5,6 +5,7 @@ import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import envoy.server.data.ConfigItem;
import envoy.server.data.Message;
import envoy.server.data.User;
@ -59,13 +60,29 @@ public class PersistenceManager {
entityManager.getTransaction().commit();
}
/**
* Adds a {@link ConfigItem} to the database.
*
* @param configItem the {@link ConfigItem} to add to the database
* @since Envoy Server Standalone v0.1-alpha
*/
public void addConfigItem(ConfigItem configItem) {
entityManager.getTransaction().begin();
entityManager.persist(configItem);
entityManager.getTransaction().commit();
}
/**
* Updates a {@link User} in the database
*
* @param user the {@link User} to add to the database
* @since Envoy Server Standalone v0.1-alpha
*/
public void updateUser(User user) { entityManager.merge(user); }
public void updateUser(User user) {
entityManager.getTransaction().begin();
entityManager.merge(user);
entityManager.getTransaction().commit();
}
/**
* Updates a {@link Message} in the database.
@ -73,7 +90,23 @@ public class PersistenceManager {
* @param message the message to update
* @since Envoy Server Standalone v0.1-alpha
*/
public void updateMessage(Message message) { entityManager.merge(message); }
public void updateMessage(Message message) {
entityManager.getTransaction().begin();
entityManager.merge(message);
entityManager.getTransaction().commit();
}
/**
* Updates a {@link ConfigItem} in the database.
*
* @param configItem the configItem to update
* @since Envoy Server Standalone v0.1-alpha
*/
public void updateConfigItem(ConfigItem configItem) {
entityManager.getTransaction().begin();
entityManager.merge(configItem);
entityManager.getTransaction().commit();
}
/**
* Searches for a {@link User} with a specific id.
@ -104,6 +137,8 @@ public class PersistenceManager {
*/
public Message getMessageById(long id) { return entityManager.find(Message.class, id); }
public ConfigItem getConfigItemById(String key) { return entityManager.find(ConfigItem.class, key); }
/**
* Returns all messages received while being offline.
*

View File

@ -5,6 +5,8 @@ import java.io.IOException;
import envoy.data.IdGenerator;
import envoy.event.IdGeneratorRequest;
import envoy.server.ObjectProcessor;
import envoy.server.data.ConfigItem;
import envoy.server.database.PersistenceManager;
import envoy.server.net.ObjectWriteProxy;
/**
@ -17,8 +19,7 @@ import envoy.server.net.ObjectWriteProxy;
*/
public class IdGeneratorRequestProcessor implements ObjectProcessor<IdGeneratorRequest> {
private static long currentId = 0;
private static final long ID_RANGE = 2;
private static final long ID_RANGE = 2;
@Override
public Class<IdGeneratorRequest> getInputClass() { return IdGeneratorRequest.class; }
@ -27,8 +28,10 @@ public class IdGeneratorRequestProcessor implements ObjectProcessor<IdGeneratorR
public void process(IdGeneratorRequest input, long socketId, ObjectWriteProxy writeProxy) throws IOException {
System.out.println("Received id generation request.");
IdGenerator generator = new IdGenerator(currentId, ID_RANGE);
currentId += ID_RANGE;
ConfigItem currentId = PersistenceManager.getPersistenceManager().getConfigItemById("currentMessageId");
IdGenerator generator = new IdGenerator(Integer.parseInt(currentId.getValue()), ID_RANGE);
currentId.setValue(String.valueOf(Integer.parseInt(currentId.getValue()) + ID_RANGE));
PersistenceManager.getPersistenceManager().updateConfigItem(currentId);
System.out.println("Sending new id generator " + generator);
writeProxy.write(socketId, generator);