-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #563 from kenfinnigan/broadcast-emitter
Fixes #471
- Loading branch information
Showing
13 changed files
with
238 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
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
33 changes: 33 additions & 0 deletions
33
...provider/src/main/java/io/smallrye/reactive/messaging/extension/EmitterConfiguration.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,33 @@ | ||
package io.smallrye.reactive.messaging.extension; | ||
|
||
import org.eclipse.microprofile.reactive.messaging.OnOverflow; | ||
|
||
import io.smallrye.reactive.messaging.annotations.Broadcast; | ||
|
||
public class EmitterConfiguration { | ||
public final String name; | ||
public final OnOverflow.Strategy overflowBufferStrategy; | ||
public final long overflowBufferSize; | ||
public final Boolean broadcast; | ||
public final int numberOfSubscriberBeforeConnecting; | ||
|
||
public EmitterConfiguration(String name, OnOverflow onOverflow, Broadcast broadcast) { | ||
this.name = name; | ||
|
||
if (onOverflow != null) { | ||
this.overflowBufferStrategy = onOverflow.value(); | ||
this.overflowBufferSize = onOverflow.bufferSize(); | ||
} else { | ||
this.overflowBufferStrategy = null; | ||
this.overflowBufferSize = -1; | ||
} | ||
|
||
if (broadcast != null) { | ||
this.broadcast = Boolean.TRUE; | ||
this.numberOfSubscriberBeforeConnecting = broadcast.value(); | ||
} else { | ||
this.broadcast = Boolean.FALSE; | ||
this.numberOfSubscriberBeforeConnecting = -1; | ||
} | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
...saging-provider/src/main/java/io/smallrye/reactive/messaging/helpers/BroadcastHelper.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,36 @@ | ||
package io.smallrye.reactive.messaging.helpers; | ||
|
||
import org.eclipse.microprofile.reactive.messaging.Message; | ||
import org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder; | ||
import org.eclipse.microprofile.reactive.streams.operators.ReactiveStreams; | ||
import org.reactivestreams.Publisher; | ||
|
||
import io.smallrye.mutiny.Multi; | ||
|
||
public class BroadcastHelper { | ||
|
||
private BroadcastHelper() { | ||
// Avoid direct instantiation. | ||
} | ||
|
||
/** | ||
* <p> | ||
* Wraps an existing {@code Publisher} for broadcasting. | ||
* </p> | ||
* | ||
* @param publisher The publisher to be wrapped | ||
* @param numberOfSubscriberBeforeConnecting Number of subscribers that must be present before broadcast occurs. | ||
* A value of 0 means any number of subscribers will trigger the broadcast. | ||
* @return The wrapped {@code Publisher} in a new {@code PublisherBuilder} | ||
*/ | ||
public static PublisherBuilder<? extends Message<?>> broadcastPublisher(Publisher<? extends Message<?>> publisher, | ||
int numberOfSubscriberBeforeConnecting) { | ||
Multi<? extends Message<?>> broadcastPublisher = Multi.createFrom().publisher(publisher); | ||
if (numberOfSubscriberBeforeConnecting != 0) { | ||
return ReactiveStreams.fromPublisher(broadcastPublisher | ||
.broadcast().toAtLeast(numberOfSubscriberBeforeConnecting)); | ||
} else { | ||
return ReactiveStreams.fromPublisher(broadcastPublisher.broadcast().toAllSubscribers()); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...provider/src/test/java/io/smallrye/reactive/messaging/broadcast/BeanEmitterBroadcast.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,40 @@ | ||
package io.smallrye.reactive.messaging.broadcast; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
|
||
import org.eclipse.microprofile.reactive.messaging.Channel; | ||
import org.eclipse.microprofile.reactive.messaging.Emitter; | ||
import org.eclipse.microprofile.reactive.messaging.Incoming; | ||
|
||
import io.smallrye.reactive.messaging.annotations.Broadcast; | ||
|
||
@ApplicationScoped | ||
public class BeanEmitterBroadcast { | ||
@Inject | ||
@Broadcast | ||
@Channel("X") | ||
Emitter<String> emitter; | ||
|
||
private final List<String> list = new CopyOnWriteArrayList<>(); | ||
|
||
public Emitter<String> emitter() { | ||
return emitter; | ||
} | ||
|
||
public List<String> list() { | ||
return list; | ||
} | ||
|
||
@Incoming("X") | ||
public void consume(final String s) { | ||
list.add(s); | ||
} | ||
|
||
public void send(String s) { | ||
emitter.send(s); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...-provider/src/test/java/io/smallrye/reactive/messaging/broadcast/BeanEmitterConsumer.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,22 @@ | ||
package io.smallrye.reactive.messaging.broadcast; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
|
||
import org.eclipse.microprofile.reactive.messaging.Incoming; | ||
|
||
@ApplicationScoped | ||
public class BeanEmitterConsumer { | ||
private final List<String> list = new CopyOnWriteArrayList<>(); | ||
|
||
public List<String> list() { | ||
return list; | ||
} | ||
|
||
@Incoming("X") | ||
public void consume(final String s) { | ||
list.add(s); | ||
} | ||
} |
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.