Skip to content

Feature/observability #317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.atomhopper.adapter.jpa.PersistedEntry;
import org.atomhopper.adapter.jpa.PersistedFeed;
import org.atomhopper.hibernate.query.CategoryCriteriaGenerator;
import org.hibernate.Session;

import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -33,4 +34,14 @@ List<PersistedEntry> getFeedPage(String feedName, PersistedEntry markerEntry, Pa
Set<PersistedCategory> updateCategories(Set<PersistedCategory> categories);

PersistedEntry getNextMarker(PersistedEntry persistedEntry, String feedName, CategoryCriteriaGenerator criteriaGenerator);

void addObserver(Observer observer);

void removeObserver(Observer observer);

interface Observer {
void beforeAction(String name, Session session);
void afterAction(String name, Session session);
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.atomhopper.hibernate;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;

import org.atomhopper.adapter.jpa.PersistedCategory;
import org.atomhopper.adapter.jpa.PersistedEntry;
Expand All @@ -31,17 +33,20 @@ public class HibernateFeedRepository implements FeedRepository {
private final HibernateSessionManager sessionManager;
private static final String DATE_LAST_UPDATED = "dateLastUpdated";
private static final String FEED_NAME = "feed.name";
private final List<Observer> observers = Collections.synchronizedList(new ArrayList<Observer>());

public HibernateFeedRepository(Map<String, String> parameters) {
sessionManager = new HibernateSessionManager(parameters);
}

public void performSimpleAction(SimpleSessionAction action) {
public void performSimpleAction(String description, SimpleSessionAction action) {
final long begin = System.currentTimeMillis();
LOG.debug("~!$: Simple Action Session begin: " + begin);

final Session session = sessionManager.getSession();

beforeAction(description, session);

Transaction tx = null;

try {
Expand All @@ -57,16 +62,18 @@ public void performSimpleAction(SimpleSessionAction action) {

throw new AtomDatabaseException("Failure performing hibernate action: " + action.toString(), ex);
} finally {
afterAction(description, session);
LOG.debug("~!$: Closing session. Elapsed time: " + (System.currentTimeMillis() - begin));
session.close();
}
}

public <T> T performComplexAction(ComplexSessionAction<T> action) {
public <T> T performComplexAction(String description, ComplexSessionAction<T> action) {
final long begin = System.currentTimeMillis();
LOG.debug("~!$: Complex Action Session begin: " + begin);

final Session session = sessionManager.getSession();
beforeAction(description, session);

T returnable = null;
Transaction tx = null;
Expand All @@ -86,30 +93,34 @@ public <T> T performComplexAction(ComplexSessionAction<T> action) {

throw new AtomDatabaseException("Failure performing hibernate action: " + action.toString(), ex);
} finally {
afterAction(description, session);
LOG.debug("~!$: Closing session. Elapsed time: " + (System.currentTimeMillis() - begin));
session.close();
}
}

public <T> T performComplexActionNonTransactionable(ComplexSessionAction<T> action) {
public <T> T performComplexActionNonTransactionable(String description, ComplexSessionAction<T> action) {
final Session session = sessionManager.getSession();

T returnable = null;

beforeAction(description, session);

try {
returnable = action.perform(session);

return returnable;
} catch (Exception ex) {
throw new AtomDatabaseException("Failure performing hibernate action: " + action.toString(), ex);
} finally {
afterAction(description, session);
session.close();
}
}

@Override
public Set<PersistedCategory> getCategoriesForFeed(final String feedName) {
return performComplexAction(new ComplexSessionAction<Set<PersistedCategory>>() {
return performComplexAction("getCategoriesForFeed/" + feedName, new ComplexSessionAction<Set<PersistedCategory>>() {

@Override
public Set<PersistedCategory> perform(Session liveSession) {
Expand All @@ -129,7 +140,7 @@ public Set<PersistedCategory> perform(Session liveSession) {

@Override
public List<PersistedEntry> getFeedHead(final String feedName, final CategoryCriteriaGenerator criteriaGenerator, final int pageSize) {
return performComplexActionNonTransactionable(new ComplexSessionAction<List<PersistedEntry>>() {
return performComplexActionNonTransactionable("getFeedHead/" + feedName, new ComplexSessionAction<List<PersistedEntry>>() {

@Override
public List<PersistedEntry> perform(Session liveSession) {
Expand All @@ -150,7 +161,7 @@ public List<PersistedEntry> perform(Session liveSession) {
@Override
public List<PersistedEntry> getFeedPage(final String feedName, final PersistedEntry markerEntry, final PageDirection direction,
final CategoryCriteriaGenerator criteriaGenerator, final int pageSize) {
return performComplexActionNonTransactionable(new ComplexSessionAction<List<PersistedEntry>>() {
return performComplexActionNonTransactionable("getFeedPage/" + feedName, new ComplexSessionAction<List<PersistedEntry>>() {

@Override
public List<PersistedEntry> perform(Session liveSession) {
Expand Down Expand Up @@ -180,7 +191,7 @@ public List<PersistedEntry> perform(Session liveSession) {

@Override
public void saveFeed(final PersistedFeed feed) {
performSimpleAction(new SimpleSessionAction() {
performSimpleAction("saveFeed", new SimpleSessionAction() {

@Override
public void perform(Session liveSession) {
Expand All @@ -207,7 +218,7 @@ public Set<PersistedCategory> updateCategories(final Set<PersistedCategory> cate
AtomDatabaseException firstException = null;
while (attemptsLeft-- > 0) {
try {
return performComplexAction(new ComplexSessionAction<Set<PersistedCategory>>() {
return performComplexAction("updateCategories", new ComplexSessionAction<Set<PersistedCategory>>() {

@Override
public Set<PersistedCategory> perform(Session liveSession) {
Expand Down Expand Up @@ -245,7 +256,7 @@ public void saveEntry(final PersistedEntry entry) {
AtomDatabaseException firstException = null;
while (attemptsLeft-- > 0) {
try {
performSimpleAction(new SimpleSessionAction() {
performSimpleAction("saveEntry", new SimpleSessionAction() {

@Override
public void perform(Session liveSession) {
Expand All @@ -272,7 +283,7 @@ public void perform(Session liveSession) {

@Override
public Collection<PersistedFeed> getAllFeeds() {
return performComplexActionNonTransactionable(new ComplexSessionAction<Collection<PersistedFeed>>() {
return performComplexActionNonTransactionable("getAllFeeds", new ComplexSessionAction<Collection<PersistedFeed>>() {

@Override
public Collection<PersistedFeed> perform(Session liveSession) {
Expand All @@ -283,7 +294,7 @@ public Collection<PersistedFeed> perform(Session liveSession) {

@Override
public PersistedEntry getEntry(final String entryId, final String feedName) {
return performComplexActionNonTransactionable(new ComplexSessionAction<PersistedEntry>() {
return performComplexActionNonTransactionable("getEntry/" + feedName, new ComplexSessionAction<PersistedEntry>() {

@Override
public PersistedEntry perform(Session liveSession) {
Expand All @@ -295,7 +306,7 @@ public PersistedEntry perform(Session liveSession) {

@Override
public PersistedFeed getFeed(final String name) {
return performComplexActionNonTransactionable(new ComplexSessionAction<PersistedFeed>() {
return performComplexActionNonTransactionable("getFeed/" + name, new ComplexSessionAction<PersistedFeed>() {

@Override
public PersistedFeed perform(Session liveSession) {
Expand All @@ -307,7 +318,7 @@ public PersistedFeed perform(Session liveSession) {
@Override
public List<PersistedEntry> getLastPage(final String feedName, final int pageSize, final CategoryCriteriaGenerator criteriaGenerator) {

return performComplexActionNonTransactionable(new ComplexSessionAction<List<PersistedEntry>>() {
return performComplexActionNonTransactionable("getLastPage/" + feedName, new ComplexSessionAction<List<PersistedEntry>>() {

@Override
public List<PersistedEntry> perform(Session liveSession) {
Expand All @@ -328,7 +339,7 @@ public List<PersistedEntry> perform(Session liveSession) {
@Override
public PersistedEntry getNextMarker(final PersistedEntry persistedEntry, final String feedName, final CategoryCriteriaGenerator criteriaGenerator) {

return performComplexActionNonTransactionable(new ComplexSessionAction<PersistedEntry>() {
return performComplexActionNonTransactionable("getNextMarker/" + feedName, new ComplexSessionAction<PersistedEntry>() {

@Override
public PersistedEntry perform(Session liveSession) {
Expand All @@ -348,6 +359,30 @@ public PersistedEntry perform(Session liveSession) {
});
}

@Override
public void addObserver(Observer observer) {
observers.add(observer);
}

@Override
public void removeObserver(Observer observer) {
observers.remove(observer);
}

private void beforeAction(String action, Session session) {
CopyOnWriteArrayList<Observer> observers = new CopyOnWriteArrayList<Observer>(this.observers);
for (FeedRepository.Observer observer : observers) {
observer.beforeAction(action, session);
}
}

private void afterAction(String action, Session session) {
CopyOnWriteArrayList<Observer> observers = new CopyOnWriteArrayList<Observer>(this.observers);
for (int i = observers.size() - 1; i >= 0; i--) {
observers.get(i).beforeAction(action, session);
}
}

private int safeLongToInt(long value) {
if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
throw new IllegalArgumentException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void setup() throws Exception {

@Test(expected=AtomDatabaseException.class)
public void shouldThrowAtomDatabaseException() throws Exception {
feedRepository.performSimpleAction(simpleSessionAction);
feedRepository.performSimpleAction("test", simpleSessionAction);
}
}

Expand All @@ -87,7 +87,7 @@ public void setup() throws Exception {
/*This should throw the error because */
@Test(expected=AtomDatabaseException.class)
public void shouldThrowAtomDatabaseException() throws Exception {
feedRepository.performComplexAction(complexSessionAction);
feedRepository.performComplexAction("test", complexSessionAction);
}
}

Expand Down Expand Up @@ -133,7 +133,7 @@ public void shouldFindEntry() throws Exception {

@Test
public void shouldCreateCategories() {
feedRepository.performSimpleAction(new SimpleSessionAction() {
feedRepository.performSimpleAction("test", new SimpleSessionAction() {
@Override
public void perform(Session liveSession) {
final PersistedEntry entry = (PersistedEntry) liveSession.createCriteria(PersistedEntry.class)
Expand All @@ -147,7 +147,7 @@ public void perform(Session liveSession) {

@Test
public void shouldFindEntryInFeed() throws Exception {
feedRepository.performSimpleAction(new SimpleSessionAction() {
feedRepository.performSimpleAction("test", new SimpleSessionAction() {
@Override
public void perform(Session liveSession) {
final List feeds = liveSession.createCriteria(PersistedFeed.class).list();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void main(String[] args) {

feedRepository.saveEntry(entry);

feedRepository.performSimpleAction(new SimpleSessionAction() {
feedRepository.performSimpleAction("testmain", new SimpleSessionAction() {

@Override
public void perform(Session liveSession) {
Expand Down
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,18 @@
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
Expand Down