-
-
Save AdrianoJS/7d40951104957da5b034408f04bb1f2d to your computer and use it in GitHub Desktop.
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
public interface DataExporter { | |
void export(List<DataEventMessage> messages); | |
default void export(DataEventMessage message) { | |
if (message != null) { | |
export(List.of(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
@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