Skip to content

Instantly share code, notes, and snippets.

@calvincodes
Created June 27, 2017 04:37
Show Gist options
  • Save calvincodes/39b7a79fc58c863256706dab0f96cd6f to your computer and use it in GitHub Desktop.
Save calvincodes/39b7a79fc58c863256706dab0f96cd6f to your computer and use it in GitHub Desktop.
public class ChannelCachedMessagePublisher {
private static CachingConnectionFactory CACHING_CONNECTION_FACTORY = new CachingConnectionFactory("localhost");
private static RabbitTemplate RABBIT_TEMPLATE;
private void init() {
if (!INITIALIZED) {
synchronized (ConnectionCachedMessagePublisher.class) {
CACHING_CONNECTION_FACTORY.setCacheMode(CachingConnectionFactory.CacheMode.CHANNEL);
CACHING_CONNECTION_FACTORY.setChannelCacheSize(100);
CACHING_CONNECTION_FACTORY.setVirtualHost("testVHost");
CACHING_CONNECTION_FACTORY.setConnectionNameStrategy(CACHING_CONNECTION_FACTORY -> "arpit-cached-connection");
CACHING_CONNECTION_FACTORY.setPublisherConfirms(false);
RABBIT_TEMPLATE = new RabbitTemplate(CACHING_CONNECTION_FACTORY);
}
INITIALIZED = true;
}
}
public void publishMessage(String exchange, String routingKey, String message) {
RABBIT_TEMPLATE.convertAndSend(exchange, routingKey, message, new MessagePostProcessor() {
@Override
public Message postProcessMessage(Message message) throws AmqpException {
MessageProperties properties = message.getMessageProperties();
properties.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
return new org.springframework.amqp.core.Message(message.getBody(),properties);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment