diff options
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/notifications/DefaultNotificationManager.java | 16 | ||||
-rw-r--r-- | sonar-core/src/main/java/org/sonar/jpa/entity/NotificationQueueElement.java | 15 |
2 files changed, 24 insertions, 7 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/notifications/DefaultNotificationManager.java b/sonar-core/src/main/java/org/sonar/core/notifications/DefaultNotificationManager.java index f9e2107743f..2db564a2953 100644 --- a/sonar-core/src/main/java/org/sonar/core/notifications/DefaultNotificationManager.java +++ b/sonar-core/src/main/java/org/sonar/core/notifications/DefaultNotificationManager.java @@ -19,9 +19,6 @@ */ package org.sonar.core.notifications; -import java.util.Date; -import java.util.List; - import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.configuration.Property; import org.sonar.api.database.model.User; @@ -30,6 +27,9 @@ import org.sonar.api.notifications.NotificationManager; import org.sonar.jpa.entity.NotificationQueueElement; import org.sonar.jpa.session.DatabaseSessionFactory; +import java.util.Date; +import java.util.List; + /** * @since 2.10 */ @@ -52,15 +52,21 @@ public class DefaultNotificationManager implements NotificationManager { public NotificationQueueElement getFromQueue() { DatabaseSession session = sessionFactory.getSession(); - String hql = "FROM " + NotificationQueueElement.class.getSimpleName() + " ORDER BY createdAt ASC LIMIT 1"; - List<NotificationQueueElement> notifications = session.createQuery(hql).getResultList(); + String hql = "FROM " + NotificationQueueElement.class.getSimpleName() + " ORDER BY createdAt ASC"; + List<NotificationQueueElement> notifications = session.createQuery(hql).setMaxResults(1).getResultList(); if (notifications.isEmpty()) { + // UGLY - waiting for a clean way to manage JDBC connections without Hibernate - myBatis is coming soon + // This code is highly coupled to org.sonar.server.notifications.NotificationService, which periodically executes + // several times the methods getFromQueue() and isEnabled(). The session is closed only at the end of the task - + // when there are no more notifications to process - to ensure "better" performances. + sessionFactory.clear(); return null; } NotificationQueueElement notification = notifications.get(0); session.removeWithoutFlush(notification); session.commit(); return notification; + } public boolean isEnabled(String username, String channelKey, String dispatcherKey) { diff --git a/sonar-core/src/main/java/org/sonar/jpa/entity/NotificationQueueElement.java b/sonar-core/src/main/java/org/sonar/jpa/entity/NotificationQueueElement.java index ac80cec95e6..8428ab9a20e 100644 --- a/sonar-core/src/main/java/org/sonar/jpa/entity/NotificationQueueElement.java +++ b/sonar-core/src/main/java/org/sonar/jpa/entity/NotificationQueueElement.java @@ -19,6 +19,7 @@ */ package org.sonar.jpa.entity; +import org.apache.commons.io.IOUtils; import org.sonar.api.notifications.Notification; import org.sonar.api.utils.SonarException; @@ -59,14 +60,18 @@ public class NotificationQueueElement { } public void setNotification(Notification notification) { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(notification); objectOutputStream.close(); this.data = byteArrayOutputStream.toByteArray(); + } catch (IOException e) { throw new SonarException(e); + + } finally { + IOUtils.closeQuietly(byteArrayOutputStream); } } @@ -74,16 +79,22 @@ public class NotificationQueueElement { if (this.data == null) { return null; } + ByteArrayInputStream byteArrayInputStream = null; try { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(this.data); + byteArrayInputStream = new ByteArrayInputStream(this.data); ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); Object result = objectInputStream.readObject(); objectInputStream.close(); return (Notification) result; + } catch (IOException e) { throw new SonarException(e); + } catch (ClassNotFoundException e) { throw new SonarException(e); + + } finally { + IOUtils.closeQuietly(byteArrayInputStream); } } |