forked from jeffkole/jpile
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding more flexible events processing approach using Guava's EventBus.
- Loading branch information
phillip-michailov
committed
Jan 26, 2016
1 parent
12eeaee
commit 6299106
Showing
14 changed files
with
538 additions
and
45 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
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
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
39 changes: 39 additions & 0 deletions
39
src/main/java/com/opower/persistence/jpile/infile/events/EventBase.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,39 @@ | ||
package com.opower.persistence.jpile.infile.events; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
/** | ||
* Base class for events. | ||
* | ||
* @author phillip-michailov | ||
*/ | ||
public class EventBase { | ||
|
||
private final Object source; | ||
private final EventFirePoint firePoint; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param source object that fired this event. | ||
* @param firePoint point in code path where this event occurred. | ||
*/ | ||
public EventBase(Object source, EventFirePoint firePoint) { | ||
this.source = checkNotNull(source, "Source cannot be null"); | ||
this.firePoint = checkNotNull(firePoint, "Fire point cannot be null"); | ||
} | ||
|
||
/** | ||
* @return object that fired this event. | ||
*/ | ||
public Object getSource() { | ||
return this.source; | ||
} | ||
|
||
/** | ||
* @return point in code path where this event occurred. | ||
*/ | ||
public EventFirePoint getFirePoint() { | ||
return this.firePoint; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/opower/persistence/jpile/infile/events/EventFirePoint.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,11 @@ | ||
package com.opower.persistence.jpile.infile.events; | ||
|
||
/** | ||
* A point in the code path where an event was fired. | ||
* For now it can be either `before` some action or 'after' it. | ||
* | ||
* @author phillip-michailov | ||
*/ | ||
public enum EventFirePoint { | ||
BEFORE, AFTER | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/com/opower/persistence/jpile/infile/events/FlushEvent.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,59 @@ | ||
package com.opower.persistence.jpile.infile.events; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
/** | ||
* This event indicates when infile buffer was flushed. | ||
* | ||
* @author phillip-michailov | ||
*/ | ||
public class FlushEvent extends EventBase { | ||
|
||
private final Class<?> entityClass; | ||
private final String tableName; | ||
private final long timestamp; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param source object that fired this event. | ||
* @param firePoint point in code path where this event occurred. | ||
* @param entityClass class annotated by {@link javax.persistence.Entity}. | ||
* @param tableName name of database table for which this event occurred. | ||
* @param timestamp when this event occurred in nanoseconds. | ||
*/ | ||
public FlushEvent(Object source, EventFirePoint firePoint, Class<?> entityClass, String tableName, long timestamp) { | ||
super(source, firePoint); | ||
this.entityClass = checkNotNull(entityClass, "Entity class cannot be null"); | ||
this.tableName = checkNotNull(tableName, "Table name cannot be null"); | ||
this.timestamp = timestamp; | ||
} | ||
|
||
/** | ||
* Constructs event by capturing current {@link System#nanoTime()} as {@link #timestamp}. | ||
*/ | ||
public FlushEvent(Object source, EventFirePoint firePoint, Class<?> entityClass, String tableName) { | ||
this(source, firePoint, entityClass, tableName, System.nanoTime()); | ||
} | ||
|
||
/** | ||
* @return class annotated by {@link javax.persistence.Entity}. | ||
*/ | ||
public Class<?> getEntityClass() { | ||
return this.entityClass; | ||
} | ||
|
||
/** | ||
* @return when this event occurred in nanoseconds. | ||
*/ | ||
public long getTimestamp() { | ||
return this.timestamp; | ||
} | ||
|
||
/** | ||
* @return name of database table for which this event occurred. | ||
*/ | ||
public String getTableName() { | ||
return this.tableName; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/opower/persistence/jpile/infile/events/SaveEntityEvent.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,32 @@ | ||
package com.opower.persistence.jpile.infile.events; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
/** | ||
* This event indicates that object was saved to the infile buffer. | ||
* | ||
* @author phillip-michailov | ||
*/ | ||
public class SaveEntityEvent extends EventBase { | ||
|
||
private final Object entity; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param source object that fired this event. | ||
* @param firePoint point in code path where this event occurred. | ||
* @param entity entity instance that was saved to buffer. | ||
*/ | ||
public SaveEntityEvent(Object source, EventFirePoint firePoint, Object entity) { | ||
super(source, firePoint); | ||
this.entity = checkNotNull(entity, "Entity cannot be null"); | ||
} | ||
|
||
/** | ||
* @return entity instance that was saved to buffer. | ||
*/ | ||
public Object getEntity() { | ||
return this.entity; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/opower/persistence/jpile/infile/events/SaveEntityEventAdapter.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,42 @@ | ||
package com.opower.persistence.jpile.infile.events; | ||
|
||
import com.google.common.eventbus.Subscribe; | ||
import com.opower.persistence.jpile.loader.HierarchicalInfileObjectLoader; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
/** | ||
* Adapter for {@link HierarchicalInfileObjectLoader.CallBack} for backward compatibility. | ||
* | ||
* @author phillip-michailov | ||
*/ | ||
@SuppressWarnings("deprecation") | ||
public class SaveEntityEventAdapter { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(SaveEntityEventAdapter.class); | ||
|
||
private final HierarchicalInfileObjectLoader.CallBack callBack; | ||
|
||
public SaveEntityEventAdapter(HierarchicalInfileObjectLoader.CallBack callBack) { | ||
this.callBack = checkNotNull(callBack, "callback"); | ||
} | ||
|
||
@Subscribe | ||
@SuppressWarnings("unused") | ||
public void dispatch(SaveEntityEvent event) { | ||
EventFirePoint firePoint = event.getFirePoint(); | ||
Object entity = event.getEntity(); | ||
switch (firePoint) { | ||
case BEFORE: | ||
this.callBack.onBeforeSave(entity); | ||
break; | ||
case AFTER: | ||
this.callBack.onAfterSave(entity); | ||
break; | ||
default: | ||
LOGGER.warn("Unknown fire point: {}", firePoint); | ||
} | ||
} | ||
} |
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
17 changes: 0 additions & 17 deletions
17
src/main/java/com/opower/persistence/jpile/loader/NoOpCallBack.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.