This repository has been archived by the owner on Aug 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
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 #11 from CJSCommonPlatform/core-jms-adapter
Core jms adapter
- Loading branch information
Showing
35 changed files
with
710 additions
and
278 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
9 changes: 9 additions & 0 deletions
9
...in/java/uk/gov/justice/services/core/annotation/exception/MissingAnnotationException.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,9 @@ | ||
package uk.gov.justice.services.core.annotation.exception; | ||
|
||
public class MissingAnnotationException extends RuntimeException { | ||
|
||
public MissingAnnotationException(final String message) { | ||
super(message); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
core/src/main/java/uk/gov/justice/services/core/context/ContextName.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,21 @@ | ||
package uk.gov.justice.services.core.context; | ||
|
||
import uk.gov.justice.services.core.exception.InvalidNameException; | ||
|
||
public final class ContextName { | ||
private ContextName() { | ||
} | ||
|
||
/** | ||
* Extracts context name from the logical action or event name. | ||
* | ||
* @param name logical name of the action or event. | ||
* @return context name | ||
*/ | ||
public static String fromName(final String name) { | ||
if (!name.contains(".")) { | ||
throw new InvalidNameException("Invalid action or event name " + name); | ||
} else return name.split("\\.")[0]; | ||
} | ||
|
||
} |
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
9 changes: 9 additions & 0 deletions
9
core/src/main/java/uk/gov/justice/services/core/exception/InvalidNameException.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,9 @@ | ||
package uk.gov.justice.services.core.exception; | ||
|
||
public class InvalidNameException extends RuntimeException { | ||
|
||
public InvalidNameException(final String message) { | ||
super(message); | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...src/main/java/uk/gov/justice/services/core/handler/exception/MissingHandlerException.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,9 @@ | ||
package uk.gov.justice.services.core.handler.exception; | ||
|
||
public class MissingHandlerException extends RuntimeException { | ||
|
||
public MissingHandlerException(final String message) { | ||
super(message); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
core/src/main/java/uk/gov/justice/services/core/jms/AbstractJMSListener.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,38 @@ | ||
package uk.gov.justice.services.core.jms; | ||
|
||
|
||
import uk.gov.justice.services.core.dispatcher.Dispatcher; | ||
import uk.gov.justice.services.core.jms.converter.EnvelopeConverter; | ||
import uk.gov.justice.services.core.jms.exception.InvalildJmsMessageTypeException; | ||
|
||
import javax.inject.Inject; | ||
import javax.jms.JMSException; | ||
import javax.jms.Message; | ||
import javax.jms.MessageListener; | ||
import javax.jms.TextMessage; | ||
|
||
/** | ||
* Abstract JMS Listener. Dispatches the message to the correct handler. | ||
*/ | ||
public abstract class AbstractJMSListener implements MessageListener { | ||
|
||
@Inject | ||
EnvelopeConverter envelopeConverter; | ||
|
||
protected abstract Dispatcher getDispatcher(); | ||
|
||
@Override | ||
public void onMessage(final Message message) { | ||
if (!(message instanceof TextMessage)) { | ||
try { | ||
throw new InvalildJmsMessageTypeException(String.format("Message is not an instance of TextMessage %s", message.getJMSMessageID())); | ||
} catch (JMSException e) { | ||
throw new InvalildJmsMessageTypeException(String.format("Message is not an instance of TextMessage. Failed to retrieve messageId %s", | ||
message), e); | ||
} | ||
} | ||
|
||
getDispatcher().dispatch(envelopeConverter.fromMessage((TextMessage) message)); | ||
} | ||
|
||
} |
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
35 changes: 35 additions & 0 deletions
35
core/src/main/java/uk/gov/justice/services/core/jms/JmsSenderFactory.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,35 @@ | ||
package uk.gov.justice.services.core.jms; | ||
|
||
import uk.gov.justice.services.core.annotation.Component; | ||
import uk.gov.justice.services.core.jms.converter.EnvelopeConverter; | ||
|
||
import javax.annotation.Resource; | ||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
import javax.jms.QueueConnectionFactory; | ||
|
||
/** | ||
* Factory to create {@link JmsSender}. | ||
*/ | ||
@ApplicationScoped | ||
public class JmsSenderFactory { | ||
|
||
@Inject | ||
JmsEndpoints jmsEndpoints; | ||
|
||
@Inject | ||
EnvelopeConverter envelopeConverter; | ||
|
||
@Resource(mappedName = "java:comp/DefaultJMSConnectionFactory") | ||
QueueConnectionFactory queueConnectionFactory; | ||
|
||
/** | ||
* Creates a {@link JmsSender} based on the componentDestination. | ||
* | ||
* @param componentDestination message destination component. | ||
* @return a new JmsSender instance. | ||
*/ | ||
public JmsSender createJmsSender(final Component componentDestination) { | ||
return new JmsSender(componentDestination, envelopeConverter, jmsEndpoints, queueConnectionFactory); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
core/src/main/java/uk/gov/justice/services/core/jms/converter/EnvelopeConverter.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,57 @@ | ||
package uk.gov.justice.services.core.jms.converter; | ||
|
||
import uk.gov.justice.services.core.jms.exception.JmsConverterException; | ||
import uk.gov.justice.services.core.util.JsonObjectConverter; | ||
import uk.gov.justice.services.messaging.Envelope; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
import javax.jms.JMSException; | ||
import javax.jms.Session; | ||
import javax.jms.TextMessage; | ||
|
||
/** | ||
* Implementation of {@link MessageConverter} for {@link uk.gov.justice.services.messaging.Envelope} | ||
*/ | ||
@ApplicationScoped | ||
public class EnvelopeConverter implements MessageConverter<Envelope, TextMessage> { | ||
|
||
static final String JMS_HEADER_CPPNAME = "CPPNAME"; | ||
|
||
@Inject | ||
JsonObjectConverter jsonObjectConverter; | ||
|
||
@Override | ||
public Envelope fromMessage(final TextMessage message) { | ||
String messageAsString = null; | ||
|
||
try { | ||
messageAsString = message.getText(); | ||
return jsonObjectConverter.asEnvelope(jsonObjectConverter.fromString(messageAsString)); | ||
} catch (JMSException e) { | ||
throw createJmsConverterException(message, e); | ||
} | ||
} | ||
|
||
@Override | ||
public TextMessage toMessage(final Envelope envelope, final Session session) { | ||
final String envelopeAsString = jsonObjectConverter.asString(jsonObjectConverter.fromEnvelope(envelope)); | ||
|
||
try { | ||
final TextMessage textMessage = session.createTextMessage(envelopeAsString); | ||
textMessage.setStringProperty(JMS_HEADER_CPPNAME, envelope.metadata().name()); | ||
return textMessage; | ||
} catch (JMSException e) { | ||
throw new JmsConverterException(String.format("Exception while creating message from envelope %s", envelopeAsString), e); | ||
} | ||
} | ||
|
||
private JmsConverterException createJmsConverterException(final TextMessage message, final Throwable e) { | ||
try { | ||
return new JmsConverterException(String.format("Exception while creating envelope from message %s", message.getJMSMessageID()), e); | ||
} catch (JMSException e1) { | ||
return new JmsConverterException(String.format("Exception while creating envelope from message. Failed to retrieve messageId from %s", message), e1); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.