Class InMemoryConnector
- java.lang.Object
-
- io.smallrye.reactive.messaging.connectors.InMemoryConnector
-
- All Implemented Interfaces:
org.eclipse.microprofile.reactive.messaging.spi.ConnectorFactory
,org.eclipse.microprofile.reactive.messaging.spi.IncomingConnectorFactory
,org.eclipse.microprofile.reactive.messaging.spi.OutgoingConnectorFactory
@ApplicationScoped public class InMemoryConnector extends Object implements org.eclipse.microprofile.reactive.messaging.spi.IncomingConnectorFactory, org.eclipse.microprofile.reactive.messaging.spi.OutgoingConnectorFactory
An implementation of connector used for testing applications without having to use external broker. The idea is to substitute the `connector` of a specific channel to use `smallrye-in-memory`. Then, your test can send message and checked the received messages.
-
-
Constructor Summary
Constructors Constructor Description InMemoryConnector()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
clear()
Switch back the channel to their original connector.org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder<? extends org.eclipse.microprofile.reactive.messaging.Message<?>>
getPublisherBuilder(org.eclipse.microprofile.config.Config config)
org.eclipse.microprofile.reactive.streams.operators.SubscriberBuilder<? extends org.eclipse.microprofile.reactive.messaging.Message<?>,Void>
getSubscriberBuilder(org.eclipse.microprofile.config.Config config)
<T> InMemorySink<T>
sink(String channel)
Retrieves anInMemorySink
associated to the channel namedchannel
.<T> InMemorySource<T>
source(String channel)
Retrieves anInMemorySource
associated to the channel namedchannel
.static Map<String,String>
switchIncomingChannelsToInMemory(String... channels)
Switch the given incoming channel to in-memory.static Map<String,String>
switchOutgoingChannelsToInMemory(String... channels)
Switch the given outgoing channel to in-memory.
-
-
-
Field Detail
-
CONNECTOR
public static final String CONNECTOR
- See Also:
- Constant Field Values
-
-
Method Detail
-
switchIncomingChannelsToInMemory
public static Map<String,String> switchIncomingChannelsToInMemory(String... channels)
Switch the given incoming channel to in-memory. It replaces the previously used connector with the in-memory connector.This method is generally used before tests to avoid using an external broker for a specific channel. You can then retrieve the
InMemorySource
using:@Inject @Any InMemoryConnector connector; //... @Before public void setup() { InMemoryConnector.switchIncomingChannelsToInMemory("my-channel"); } // .. InMemorySource<Integer> channel = connector.source("my-channel"); channel.send(1); channel.send(2);
- Parameters:
channels
- the channels to switch, must not benull
, must not containnull
, must not contain a blank value- Returns:
- The map of properties that have been defined. The method sets the system properties, but give you this map to pass the properties around if needed.
-
switchOutgoingChannelsToInMemory
public static Map<String,String> switchOutgoingChannelsToInMemory(String... channels)
Switch the given outgoing channel to in-memory. It replaces the previously used connector with the in-memory connector.This method is generally used before tests to avoid using an external broker for a specific channel. You can then retrieve the
InMemorySink
using:@Inject @Any InMemoryConnector connector; //... @Before public void setup() { InMemoryConnector.switchOutgoingChannelsToInMemory("my-channel"); } // .. InMemorySink<Integer> channel = connector.sink("my-channel"); assertThat(channel.received()).hasSize(3).extracting(Message::getPayload).containsExactly(1, 2);
- Parameters:
channels
- the channels to switch, must not benull
, must not containnull
, must not contain a blank value- Returns:
- The map of properties that have been defined. The method sets the system properties, but give you this map to pass these properties around if needed.
-
clear
public static void clear()
Switch back the channel to their original connector.This method is generally used after tests to reset the original configuration.
-
getPublisherBuilder
public org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder<? extends org.eclipse.microprofile.reactive.messaging.Message<?>> getPublisherBuilder(org.eclipse.microprofile.config.Config config)
- Specified by:
getPublisherBuilder
in interfaceorg.eclipse.microprofile.reactive.messaging.spi.IncomingConnectorFactory
-
getSubscriberBuilder
public org.eclipse.microprofile.reactive.streams.operators.SubscriberBuilder<? extends org.eclipse.microprofile.reactive.messaging.Message<?>,Void> getSubscriberBuilder(org.eclipse.microprofile.config.Config config)
- Specified by:
getSubscriberBuilder
in interfaceorg.eclipse.microprofile.reactive.messaging.spi.OutgoingConnectorFactory
-
source
public <T> InMemorySource<T> source(String channel)
Retrieves anInMemorySource
associated to the channel namedchannel
. This channel must use the in-memory connected.The returned
InMemorySource
lets you send messages or payloads to the channel, mocking the real interactions.- Type Parameters:
T
- the type of message or payload sent to the channel- Parameters:
channel
- the name of the channel, must not benull
- Returns:
- the source
- Throws:
IllegalArgumentException
- if the channel name isnull
or if the channel is not associated with the in-memory connector.- See Also:
switchIncomingChannelsToInMemory(String...)
-
sink
public <T> InMemorySink<T> sink(String channel)
Retrieves anInMemorySink
associated to the channel namedchannel
. This channel must use the in-memory connected.The returned
InMemorySink
lets you checks the messages sent to the channel.- Type Parameters:
T
- the type of payload received by the channel- Parameters:
channel
- the name of the channel, must not benull
- Returns:
- the sink
- Throws:
IllegalArgumentException
- if the channel name isnull
or if the channel is not associated with the in-memory connector.- See Also:
switchOutgoingChannelsToInMemory(String...)
-
-