Skip to content

Instantly share code, notes, and snippets.

@AdrianoJS
Created September 28, 2022 18:54
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save AdrianoJS/7d40951104957da5b034408f04bb1f2d to your computer and use it in GitHub Desktop.
public interface DataExporter {
void export(List<DataEventMessage> messages);
default void export(DataEventMessage message) {
if (message != null) {
export(List.of(message));
}
}
}
@Component
public class JmsExporter implements DataExporter {
private final ObjectMapper objectMapper;
private JmsTemplate jmsTemplate;
private String queue;
public JmsExporter() {
this.objectMapper = JacksonUtil.createObjectMapper();
}
@Override
public void export(final List<DataEventMessage> messages) {
for (var message : messages) {
try {
jmsTemplate.convertAndSend(queue, objectMapper.writeValueAsString(message));
} catch (JsonProcessingException jpe) {
throw new IllegalStateException("Unable to convert message to JSON", jpe);
}
}
}
@Autowired
public JmsExporter setJmsTemplate(final JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
return this;
}
@Autowired
public JmsExporter setQueue(@Value("${foo.export.queue}") final String queue) {
this.queue = queue;
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment