-
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.
Adds support for `@Broadcast` on `Emitter` to broadcast to all subscribers
- Loading branch information
1 parent
5cf72f6
commit c8e907b
Showing
10 changed files
with
215 additions
and
34 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
47 changes: 47 additions & 0 deletions
47
...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,47 @@ | ||
package io.smallrye.reactive.messaging.extension; | ||
|
||
import org.eclipse.microprofile.reactive.messaging.OnOverflow; | ||
|
||
import io.smallrye.reactive.messaging.annotations.Broadcast; | ||
|
||
public class EmitterConfiguration { | ||
private final String name; | ||
private OnOverflow.Strategy overflowBufferStrategy = null; | ||
private long overflowBufferSize = -1; | ||
private Boolean broadcast = Boolean.FALSE; | ||
private int numberOfSubscriberBeforeConnecting = -1; | ||
|
||
public EmitterConfiguration(String name, OnOverflow onOverflow, Broadcast broadcast) { | ||
this.name = name; | ||
|
||
if (onOverflow != null) { | ||
this.overflowBufferStrategy = onOverflow.value(); | ||
this.overflowBufferSize = onOverflow.bufferSize(); | ||
} | ||
|
||
if (broadcast != null) { | ||
this.broadcast = Boolean.TRUE; | ||
this.numberOfSubscriberBeforeConnecting = broadcast.value(); | ||
} | ||
} | ||
|
||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
public OnOverflow.Strategy getOverflowBufferStrategy() { | ||
return this.overflowBufferStrategy; | ||
} | ||
|
||
public long getOverflowBufferSize() { | ||
return this.overflowBufferSize; | ||
} | ||
|
||
public Boolean isBroadcast() { | ||
return broadcast; | ||
} | ||
|
||
public int getNumberOfSubscriberBeforeConnecting() { | ||
return this.numberOfSubscriberBeforeConnecting; | ||
} | ||
} |
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
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