Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save annemarte/8eb780b3fe54047d2977f64704a642fa to your computer and use it in GitHub Desktop.
Save annemarte/8eb780b3fe54047d2977f64704a642fa to your computer and use it in GitHub Desktop.
prerequisite:
Spring boot application
Locally running NServiceBus with RabbitMQ (will add remote config)
//Same as usual, but
//remember to configure JSON Serializer:
var settings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
};
var serialization = endpointConfiguration.UseSerialization<NewtonsoftSerializer>();
package com.example.domain;
import lombok.Data; //or just create getters and setters manually
import java.io.Serializable;
@Data
public final class MyMessage implements Serializable {
private final String greeting;
private final int guid;
}
package com.example.service;
import com.example.domain.MyMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.Random;
@Service
public class NServiceBusPublisher {
private static final Logger log = LoggerFactory.getLogger(NServiceBusPublisher.class);
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private Queue queue;
@Scheduled(fixedDelay = 30000L, initialDelay = 500L)
public void sendMessage() {
int guid = new Random().nextInt(50);
final MyMessage myMessage = new MyMessage("Hello there!", guid);
CorrelationData cd = new CorrelationData(Integer.toString(guid));
MessageProperties messageProperties = new MessageProperties();
messageProperties.setHeader("NServiceBus.EnclosedMessageTypes", "MyMessage");
Message message = rabbitTemplate.getMessageConverter().toMessage(myMessage, messageProperties);
rabbitTemplate.convertAndSend(queue.getName(), message, cd);
log.info("Message sent: " + message.toString());
}
}
....
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
</dependency>
...
package com.example.config;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitConfiguration {
private static final String QUEUE_NAME = "HBT.callstatus";
@Autowired
ConnectionFactory connectionFactory;
@Bean
public Queue callStatus(){
return new Queue(QUEUE_NAME);
}
@Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate rt = new RabbitTemplate(connectionFactory);
rt.setMessageConverter(producerJackson2MessageConverter());
return rt;
}
@Bean
public Jackson2JsonMessageConverter producerJackson2MessageConverter() {
Jackson2JsonMessageConverter jsonConv = new Jackson2JsonMessageConverter();
jsonConv.setCreateMessageIds(true);
return jsonConv;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment