-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
358 additions
and
35 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
87 changes: 87 additions & 0 deletions
87
hivemq-edge/src/main/java/com/hivemq/edge/impl/events/v2/EventServiceDelegateImpl.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,87 @@ | ||
/* | ||
* Copyright 2019-present HiveMQ GmbH | ||
* | ||
* Licensed 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 com.hivemq.edge.impl.events.v2; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.hivemq.adapter.sdk.api.eventsv2.Event; | ||
import com.hivemq.adapter.sdk.api.eventsv2.EventsService; | ||
import com.hivemq.bootstrap.factories.EventServiceHandling; | ||
import com.hivemq.bootstrap.factories.EventServiceHandlingProvider; | ||
import com.hivemq.bootstrap.factories.EventServiceResult; | ||
import com.hivemq.configuration.service.ConfigurationService; | ||
import com.hivemq.extension.sdk.api.annotations.NotNull; | ||
import com.hivemq.extension.sdk.api.annotations.Nullable; | ||
import com.hivemq.extension.sdk.api.services.publish.Publish; | ||
import com.hivemq.extension.sdk.api.services.publish.PublishService; | ||
import com.hivemq.extensions.services.builder.PublishBuilderImpl; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
import java.util.List; | ||
|
||
/** | ||
* SPI delegate which wraps multiple (chained) implementations and | ||
* manages the event listeners | ||
* | ||
* @author Simon L Johnson | ||
*/ | ||
@Singleton | ||
public class EventServiceDelegateImpl implements EventsService { | ||
|
||
private final @NotNull InMemoryEvent eventStore; | ||
private final @NotNull EventServiceHandlingProvider eventServiceHandlingProvider; | ||
private final @NotNull PublishService publishService; | ||
private final @NotNull ConfigurationService configurationService; | ||
|
||
@Inject | ||
public EventServiceDelegateImpl( | ||
final @NotNull InMemoryEvent eventStore, | ||
final @NotNull EventServiceHandlingProvider eventServiceHandlingProvider, | ||
final @NotNull PublishService publishService, | ||
final @NotNull ConfigurationService configurationService) { | ||
this.eventServiceHandlingProvider = eventServiceHandlingProvider; | ||
this.publishService = publishService; | ||
this.configurationService = configurationService; | ||
Preconditions.checkNotNull(eventStore); | ||
this.eventStore = eventStore; | ||
} | ||
|
||
@Override | ||
public void publish(final @NotNull Event event) { | ||
|
||
final EventServiceHandling eventServiceHandling = eventServiceHandlingProvider.get(); | ||
if (eventServiceHandling != null) { | ||
final EventServiceResult eventServiceResult = eventServiceHandling.apply(event); | ||
if (!eventServiceResult.isPreventPublish()) { | ||
// publish can not be null if preventPublish is false | ||
assert eventServiceResult.getModifiedPublish() != null; | ||
final Publish publish = | ||
new PublishBuilderImpl(configurationService).fromPublish(eventServiceResult.getModifiedPublish()) | ||
.build(); | ||
publishService.publish(publish); | ||
} | ||
} | ||
eventStore.storeEvent(event); | ||
} | ||
|
||
@Override | ||
public @NotNull List<Event> readEvents( | ||
final @Nullable Long sinceTimestamp, final @Nullable Integer limit) { | ||
return eventStore.readEvents(sinceTimestamp, limit); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
hivemq-edge/src/main/java/com/hivemq/edge/impl/events/v2/InMemoryEvent.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,84 @@ | ||
/* | ||
* Copyright 2019-present HiveMQ GmbH | ||
* | ||
* Licensed 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 com.hivemq.edge.impl.events.v2; | ||
|
||
import com.hivemq.adapter.sdk.api.eventsv2.Event; | ||
import com.hivemq.configuration.service.InternalConfigurations; | ||
import com.hivemq.extension.sdk.api.annotations.NotNull; | ||
import com.hivemq.extension.sdk.api.annotations.Nullable; | ||
import com.hivemq.util.RollingList; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReadWriteLock; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Simple rolling list implementation optimized for fast writes but slow reads as requires sort on read. (Handled | ||
* out of lock). | ||
* | ||
* @author Simon L Johnson | ||
*/ | ||
@Singleton | ||
public class InMemoryEvent{ | ||
private final @NotNull RollingList<Event> inMemoryEventList; | ||
private final @NotNull ReadWriteLock lock = new ReentrantReadWriteLock(); | ||
|
||
@Inject | ||
public InMemoryEvent() { | ||
//optimize for quick write slow read (sort) | ||
this(InternalConfigurations.EDGE_RUNTIME_MAX_EVENTS_IN_INMEMORY_LIST.get()); | ||
} | ||
|
||
public InMemoryEvent(final int max) { | ||
this.inMemoryEventList = new RollingList<>(max); | ||
} | ||
|
||
public void storeEvent(final @NotNull Event event) { | ||
Lock writeLock = lock.writeLock(); | ||
try { | ||
writeLock.lock(); | ||
inMemoryEventList.add(event); | ||
} finally { | ||
writeLock.unlock(); | ||
} | ||
} | ||
|
||
public @NotNull List<Event> readEvents(@Nullable Long since, @Nullable Integer limit) { | ||
Lock readLock = lock.writeLock(); | ||
List<Event> events; | ||
try { | ||
readLock.lock(); | ||
events = new ArrayList<>(inMemoryEventList); | ||
} finally { | ||
readLock.unlock(); | ||
} | ||
Stream<Event> stream = events.stream().sorted(Comparator.comparing(Event::getCreated).reversed()); | ||
if (since != null) { | ||
stream = stream.filter(event -> since < event.getCreated().getTime()); | ||
} | ||
if (limit != null) { | ||
stream = stream.limit(limit); | ||
} | ||
return stream.collect(Collectors.toUnmodifiableList()); | ||
} | ||
} |
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
Oops, something went wrong.