-
Notifications
You must be signed in to change notification settings - Fork 179
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 #2572 from dankristensen/feature/jms_reconnect
Implemented reconnect and downstream propagation when reading messages
- Loading branch information
Showing
7 changed files
with
380 additions
and
108 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
103 changes: 103 additions & 0 deletions
103
...ive-messaging-jms/src/main/java/io/smallrye/reactive/messaging/jms/JmsResourceHolder.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,103 @@ | ||
package io.smallrye.reactive.messaging.jms; | ||
|
||
import static io.smallrye.reactive.messaging.jms.i18n.JmsLogging.log; | ||
|
||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
import jakarta.jms.Destination; | ||
import jakarta.jms.ExceptionListener; | ||
import jakarta.jms.JMSContext; | ||
import jakarta.jms.JMSException; | ||
|
||
public class JmsResourceHolder<T> implements ExceptionListener { | ||
|
||
private final String channel; | ||
private final Supplier<JMSContext> contextCreator; | ||
private Function<JmsResourceHolder<T>, Destination> destinationCreator; | ||
private Function<JmsResourceHolder<T>, T> clientCreator; | ||
private volatile JMSContext context; | ||
private volatile Destination destination; | ||
private volatile T client; | ||
|
||
private volatile boolean closed = false; | ||
|
||
public JmsResourceHolder(String channel, Supplier<JMSContext> contextCreator) { | ||
this.channel = channel; | ||
this.contextCreator = contextCreator; | ||
} | ||
|
||
public JmsResourceHolder<T> configure(Function<JmsResourceHolder<T>, Destination> destinationCreator, | ||
Function<JmsResourceHolder<T>, T> clientCreator) { | ||
this.destinationCreator = destinationCreator; | ||
this.clientCreator = clientCreator; | ||
return this; | ||
} | ||
|
||
public Destination getDestination() { | ||
if (destination == null) { | ||
synchronized (this) { | ||
if (destination == null) { | ||
destination = destinationCreator.apply(this); | ||
} | ||
} | ||
} | ||
return destination; | ||
} | ||
|
||
public T getClient() { | ||
if (client == null) { | ||
synchronized (this) { | ||
if (client == null) { | ||
client = clientCreator.apply(this); | ||
} | ||
} | ||
} | ||
return client; | ||
} | ||
|
||
public JMSContext getContext() { | ||
if (context == null) { | ||
synchronized (this) { | ||
if (context == null) { | ||
context = contextCreator.get(); | ||
closed = false; | ||
context.setExceptionListener(this); | ||
} | ||
} | ||
} | ||
return context; | ||
} | ||
|
||
@Override | ||
public void onException(JMSException exception) { | ||
synchronized (this) { | ||
if (closed) { | ||
return; | ||
} | ||
log.jmsException(channel, exception); | ||
close(); | ||
} | ||
} | ||
|
||
void close() { | ||
synchronized (this) { | ||
if (context != null) { | ||
context.close(); | ||
context = null; | ||
} | ||
destination = null; | ||
if (client != null) { | ||
if (client instanceof AutoCloseable) { | ||
try { | ||
((AutoCloseable) client).close(); | ||
} catch (Exception e) { | ||
log.infof(e, "Error closing client for channel %s", channel); | ||
} | ||
} | ||
client = null; | ||
} | ||
closed = true; | ||
} | ||
} | ||
} |
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.