Last active
March 17, 2024 19:01
-
-
Save annemarte/8eb780b3fe54047d2977f64704a642fa 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
prerequisite: | |
Spring boot application | |
Locally running NServiceBus with RabbitMQ (will add remote config) |
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
//Same as usual, but | |
//remember to configure JSON Serializer: | |
var settings = new JsonSerializerSettings | |
{ | |
Formatting = Formatting.Indented, | |
}; | |
var serialization = endpointConfiguration.UseSerialization<NewtonsoftSerializer>(); |
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
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; | |
} |
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
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()); | |
} | |
} |
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
.... | |
<dependency> | |
<groupId>org.springframework.amqp</groupId> | |
<artifactId>spring-rabbit</artifactId> | |
</dependency> | |
... |
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
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