Skip to content

Instantly share code, notes, and snippets.

@Tcharl
Created March 26, 2016 15:11
Show Gist options
  • Select an option

  • Save Tcharl/28376989d989815ee135 to your computer and use it in GitHub Desktop.

Select an option

Save Tcharl/28376989d989815ee135 to your computer and use it in GitHub Desktop.
@Slf4j
@ApplicationScoped
@ContextName
public class HelloServiceJMS extends RouteBuilder implements HelloService {
private final transient DataFormat helloObjectJSonFormat = new JacksonDataFormat(
HelloEntity.class, Hellos.class);
/**
* The repository.
*/
@Inject
@OsgiService
private transient HelloRepository helloObjectRepository;
/**
* JMS producer.
*/
@Inject
@Uri("jms:topic:helloServiceQueueOut")
private transient ProducerTemplate producer;
/**
* saves element
*
* @param helloObject
* element to save
*/
@Override
public void persistHello(@NotNull @Valid HelloEntity helloObject) {
log.info("****************** Save on JMS Service **********************");
log.info("persisting new message with jms: " + helloObject.getHelloMessage());
this.helloObjectRepository.save(helloObject);
ObjectMapper mapper = new ObjectMapper();
try {
this.producer.sendBody( mapper.writeValueAsString(getHellos()));
} catch (CamelExecutionException | JsonProcessingException e) {
log.error("exception marshalling messages", e);
}
}
/**
* Returns all elements.
*
* @return all elements
*/
@Override
public Hellos getHellos() {
final Collection<HelloEntity> helloObjects = this.helloObjectRepository.findAll();
if (helloObjects.isEmpty()) {
throw new UnsupportedOperationException("You could not call this method when the list is empty");
}
return Hellos.builder()
.helloCollection(helloObjects.stream().map(x -> x.getHelloMessage()).collect(Collectors.toList())).build();
}
/**
* Deletes all elements.
*/
@Override
public void deleteHellos() {
this.helloObjectRepository.deleteAll();
}
/**
* receives JMS message.
*
* @throws Exception
* Persistence error
*/
@Override
public void configure() throws Exception {
from("properties:{{helloApp.inQueueJMS}}"/*"jms:queue:helloServiceQueueIn"*/)
.log(LoggingLevel.INFO, "received jms message: ${body}").unmarshal(this.helloObjectJSonFormat).process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
HelloServiceJMS.this.persistHello((HelloEntity) exchange.getIn().getBody());
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment