Class InMemoryConnector
- java.lang.Object
-
- io.smallrye.reactive.messaging.connectors.InMemoryConnector
-
- All Implemented Interfaces:
ConnectorFactory
,IncomingConnectorFactory
,OutgoingConnectorFactory
@ApplicationScoped public class InMemoryConnector extends Object implements IncomingConnectorFactory, 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.
-
-
Field Summary
Fields Modifier and Type Field Description static String
CONNECTOR
-
Fields inherited from interface org.eclipse.microprofile.reactive.messaging.spi.ConnectorFactory
CHANNEL_NAME_ATTRIBUTE, CONNECTOR_ATTRIBUTE, CONNECTOR_PREFIX, INCOMING_PREFIX, OUTGOING_PREFIX
-
-
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 Message<?>>
getPublisherBuilder(org.eclipse.microprofile.config.Config config)
Creates a channel for the given configuration.org.eclipse.microprofile.reactive.streams.operators.SubscriberBuilder<? extends Message<?>,Void>
getSubscriberBuilder(org.eclipse.microprofile.config.Config config)
Creates a channel for the given configuration.<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 void
switchChannelToInMemory(String... channels)
Switch the given channel to in-memory.
-
-
-
Field Detail
-
CONNECTOR
public static final String CONNECTOR
- See Also:
- Constant Field Values
-
-
Method Detail
-
switchChannelToInMemory
public static void switchChannelToInMemory(String... channels)
Switch the given channel to in-memory. It replaces the previously used connector with the in-memory connector. It substitutes the connector for both the incoming and outgoing channel.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; //... InMemorySource<Integer> channel = connector.source("my-channel"); channel.send(1); channel.send(2);
With the
InMemorySource
, you can send messages to the channel, mocking the incoming messages. You can also retrieve anInMemorySink
using:@Inject @Any InMemoryConnector connector; //... InMemorySink<Integer> channel = connector.sink("my-channel"); assertThat(channel.received()).hasSize(3).extracting(Message::getPayload).containsExactly(1, 2);
With the
InMemorySink
, you can checked the messages received by the channel, to verify that the expected messages have been received.- Parameters:
channels
- the channels to switch, must not benull
, must not containnull
, must not contain a blank value
-
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 Message<?>> getPublisherBuilder(org.eclipse.microprofile.config.Config config)
Description copied from interface:IncomingConnectorFactory
Creates a channel for the given configuration. The channel's configuration is associated with a specificconnector
, using theConnector
qualifier's parameter indicating a key to whichIncomingConnectorFactory
to use.Note that the connection to the transport or broker is generally postponed until the subscription occurs.
- Specified by:
getPublisherBuilder
in interfaceIncomingConnectorFactory
- Parameters:
config
- the configuration, must not benull
, must contain theConnectorFactory.CHANNEL_NAME_ATTRIBUTE
attribute.- Returns:
- the created
PublisherBuilder
, will not benull
.
-
getSubscriberBuilder
public org.eclipse.microprofile.reactive.streams.operators.SubscriberBuilder<? extends Message<?>,Void> getSubscriberBuilder(org.eclipse.microprofile.config.Config config)
Description copied from interface:OutgoingConnectorFactory
Creates a channel for the given configuration. The channel's configuration is associated with a specificconnector
, using theConnector
qualifier's parameter indicating a key to whichOutgoing
to use.Note that the connection to the transport or broker is generally postponed until the subscription.
- Specified by:
getSubscriberBuilder
in interfaceOutgoingConnectorFactory
- Parameters:
config
- the configuration, nevernull
, must contain theConnectorFactory.CHANNEL_NAME_ATTRIBUTE
attribute.- Returns:
- the created
SubscriberBuilder
, must not benull
.
-
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:
switchChannelToInMemory(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:
switchChannelToInMemory(String...)
-
-