patternjavaMinor
Serialize/deserialize objects to and from database concurrently
Viewed 0 times
objectsserializedatabaseandfromdeserializeconcurrently
Problem
I have a class which runs as spring bean in the container and its purpose is data persistence. In the overriden method
I implemented the logic of using workManager which will get instantiated every-time the above method gets called and these workmanagers each instantiates n number of threads which then goes through the iterator created from the list which was created from the original array. Below is my implementation; can you please advice any shortcomings of this method or if there are any better methods.
CustomDataSyncEndPoint.java (class which runs as spring bean and its method is called whenever data is passed for persistence)
```
public class CustomDataSyncEndPoint extends SpaceSynchronizationEndpoint {
private final Logger logger = Logger.getLogger(CustomDataSyncEndPoint.class.getName());
private DataSource dataSource;
private List workManagers;
private Thread workManagerMonitor;
private volatile boolean stopProcessing;
private boolean isTraceLoggingEnabled;
public CustomDataSyncEndPoint(DataSource dataSource) {
this.dataSource = dataSource;
workManagers = new ArrayList();
isTraceLoggingEnabled = logger.isLoggable(Level.FINER);
}
@PostConstruct
private void init() {
stopProcessing = false;
workManagerMonitor = new Thread(new WorkManagerMonitor(), "workManagerMonitor");
workManagerMonitor.setDaemon(true);
workManagerMonitor.start();
}
@P
public void executeDataSyncOperation(DataSyncOperation[] operations) we have option to persists the data in the operations array. But the flow of data coming in is really huge and quick that we wanted to make its execution concurrent so that when fatal exception happen we don't end up with huge backlog of data which did not get persisted. One thing I should mention is that once executeDataSyncOperation method is called operations will not be modified by any other thread. So I came up with following idea;I implemented the logic of using workManager which will get instantiated every-time the above method gets called and these workmanagers each instantiates n number of threads which then goes through the iterator created from the list which was created from the original array. Below is my implementation; can you please advice any shortcomings of this method or if there are any better methods.
CustomDataSyncEndPoint.java (class which runs as spring bean and its method is called whenever data is passed for persistence)
```
public class CustomDataSyncEndPoint extends SpaceSynchronizationEndpoint {
private final Logger logger = Logger.getLogger(CustomDataSyncEndPoint.class.getName());
private DataSource dataSource;
private List workManagers;
private Thread workManagerMonitor;
private volatile boolean stopProcessing;
private boolean isTraceLoggingEnabled;
public CustomDataSyncEndPoint(DataSource dataSource) {
this.dataSource = dataSource;
workManagers = new ArrayList();
isTraceLoggingEnabled = logger.isLoggable(Level.FINER);
}
@PostConstruct
private void init() {
stopProcessing = false;
workManagerMonitor = new Thread(new WorkManagerMonitor(), "workManagerMonitor");
workManagerMonitor.setDaemon(true);
workManagerMonitor.start();
}
@P
Solution
Some simple suggestions here...
For Java 7, you can already use type inference aka diamond operator to simplify the following such assignments:
BTW, you have a spelling typo too: it should be
You have a 'guard clause' around your
While you're at it, why not simply replace the
Finally, your method
For Java 7, you can already use type inference aka diamond operator to simplify the following such assignments:
persistenceRunnableThread = new ArrayList<>();BTW, you have a spelling typo too: it should be
persistence, not persitence in PersistenceWorkManager. Also, datasyncOperation seems to be an unnecessary instance field, as you are operating directly on the Iterator object. Therefore, you can probably just stick with:iterator = Arrays.asList(datasyncOperation).iterator();You have a 'guard clause' around your
finer() logging lines to prevent unnecessary String concatenation, but for consistency I'll suggest that you use braces for the if statement too:if (isTraceLoggingEnabled) {
logger.finer("executing update on " + data.toString());
}While you're at it, why not simply replace the
java.util.Logging classes with a 'modern' logging framework like SLF4J? The methods it offers handles the String concatenation elegantly when logging is not required, so you don't even need the guard clause in the first place.Finally, your method
PersistenceRunnable.existsInDatabase(Data) can be simplified as such:private boolean existsInDatabase(Data data) {
String sql = "select count(*) from data where Id = ?";
return getJdbcTemplate().queryForObject(sql, new Object[] { data.getMessageUID() },
Integer.class) == 1;
}Code Snippets
persistenceRunnableThread = new ArrayList<>();iterator = Arrays.asList(datasyncOperation).iterator();if (isTraceLoggingEnabled) {
logger.finer("executing update on " + data.toString());
}private boolean existsInDatabase(Data data) {
String sql = "select count(*) from data where Id = ?";
return getJdbcTemplate().queryForObject(sql, new Object[] { data.getMessageUID() },
Integer.class) == 1;
}Context
StackExchange Code Review Q#96779, answer score: 2
Revisions (0)
No revisions yet.