You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2018/12/18 02:11:21 UTC

[3/5] james-project git commit: MAILBOX-364 In VM implementation for EventBus

MAILBOX-364 In VM implementation for EventBus


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/edf98d45
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/edf98d45
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/edf98d45

Branch: refs/heads/master
Commit: edf98d459d102ca17df4c3d58a4dcf3a8d556d18
Parents: cad536c
Author: Benoit Tellier <bt...@linagora.com>
Authored: Wed Dec 12 12:07:32 2018 +0700
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Tue Dec 18 09:09:58 2018 +0700

----------------------------------------------------------------------
 .../james/mailbox/events/InVMEventBus.java      | 77 ++++++++++++++++++++
 .../james/mailbox/events/InVMEventBusTest.java  | 40 ++++++++++
 2 files changed, 117 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/edf98d45/mailbox/event/event-memory/src/main/java/org/apache/james/mailbox/events/InVMEventBus.java
----------------------------------------------------------------------
diff --git a/mailbox/event/event-memory/src/main/java/org/apache/james/mailbox/events/InVMEventBus.java b/mailbox/event/event-memory/src/main/java/org/apache/james/mailbox/events/InVMEventBus.java
new file mode 100644
index 0000000..fb4aee3
--- /dev/null
+++ b/mailbox/event/event-memory/src/main/java/org/apache/james/mailbox/events/InVMEventBus.java
@@ -0,0 +1,77 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.events;
+
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.james.mailbox.Event;
+import org.apache.james.mailbox.MailboxListener;
+import org.apache.james.mailbox.events.delivery.EventDelivery;
+
+import com.github.steveash.guavate.Guavate;
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Multimaps;
+
+import reactor.core.publisher.Mono;
+
+public class InVMEventBus implements EventBus {
+    private final Multimap<RegistrationKey, MailboxListener> registrations;
+    private final ConcurrentHashMap<Group, MailboxListener> groups;
+    private final EventDelivery eventDelivery;
+
+    InVMEventBus(EventDelivery eventDelivery) {
+        this.eventDelivery = eventDelivery;
+        this.registrations = Multimaps.synchronizedSetMultimap(HashMultimap.create());
+        this.groups = new ConcurrentHashMap<>();
+    }
+
+    @Override
+    public Registration register(MailboxListener listener, RegistrationKey key) {
+        registrations.put(key, listener);
+        return () -> registrations.remove(key, listener);
+    }
+
+    @Override
+    public Registration register(MailboxListener listener, Group group) {
+        MailboxListener previous = groups.putIfAbsent(group, listener);
+        if (previous == null) {
+            return () -> groups.remove(group, listener);
+        }
+        throw new GroupAlreadyRegistered(group);
+    }
+
+    @Override
+    public Mono<Void> dispatch(Event event, Set<RegistrationKey> keys) {
+        return eventDelivery.deliver(registeredListeners(keys), event).synchronousListenerFuture();
+    }
+
+    private Set<MailboxListener> registeredListeners(Set<RegistrationKey> keys) {
+        return ImmutableSet.<MailboxListener>builder()
+            .addAll(groups.values())
+            .addAll(keys.stream()
+                .flatMap(registrationKey -> registrations.get(registrationKey).stream())
+                .collect(Guavate.toImmutableList()))
+            .build();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/edf98d45/mailbox/event/event-memory/src/test/java/org/apache/james/mailbox/events/InVMEventBusTest.java
----------------------------------------------------------------------
diff --git a/mailbox/event/event-memory/src/test/java/org/apache/james/mailbox/events/InVMEventBusTest.java b/mailbox/event/event-memory/src/test/java/org/apache/james/mailbox/events/InVMEventBusTest.java
new file mode 100644
index 0000000..6d278aa
--- /dev/null
+++ b/mailbox/event/event-memory/src/test/java/org/apache/james/mailbox/events/InVMEventBusTest.java
@@ -0,0 +1,40 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.events;
+
+import org.apache.james.mailbox.events.delivery.InVmEventDelivery;
+import org.apache.james.metrics.api.NoopMetricFactory;
+import org.junit.jupiter.api.BeforeEach;
+
+public class InVMEventBusTest implements EventBusContract {
+    private InVMEventBus eventBus;
+
+    @BeforeEach
+    void setUp() {
+        eventBus = new InVMEventBus(
+            new InVmEventDelivery(
+                new NoopMetricFactory()));
+    }
+
+    @Override
+    public EventBus eventBus() {
+        return eventBus;
+    }
+}
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org