-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from Patlenlix/jms-issue-63
JMS issue 63 - Basic Version
- Loading branch information
Showing
7 changed files
with
176 additions
and
17 deletions.
There are no files selected for viewing
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
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
50 changes: 50 additions & 0 deletions
50
src/main/java/se/iths/crimedatabase/messaging/CustomMessage.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package se.iths.crimedatabase.messaging; | ||
|
||
import java.util.Date; | ||
|
||
public class CustomMessage { | ||
private String messageID; | ||
private String message; | ||
private Date messageDate; | ||
|
||
public CustomMessage(String messageID, String message, Date messageDate) { | ||
this.messageID = messageID; | ||
this.message = message; | ||
this.messageDate = messageDate; | ||
} | ||
|
||
public CustomMessage() {} | ||
|
||
public String getMessageID() { | ||
return messageID; | ||
} | ||
|
||
public void setMessageID(String messageID) { | ||
this.messageID = messageID; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public Date getMessageDate() { | ||
return messageDate; | ||
} | ||
|
||
public void setMessageDate(Date messageDate) { | ||
this.messageDate = messageDate; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "CustomMessage{" + | ||
"messageID='" + messageID + '\'' + | ||
", message='" + message + '\'' + | ||
", messageDate=" + messageDate + | ||
'}'; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/se/iths/crimedatabase/messaging/MQConfig.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package se.iths.crimedatabase.messaging; | ||
|
||
import org.springframework.amqp.core.*; | ||
import org.springframework.amqp.rabbit.connection.ConnectionFactory; | ||
import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; | ||
import org.springframework.amqp.support.converter.MessageConverter; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class MQConfig { | ||
public static final String QUEUE = "message_queue"; | ||
public static final String EXCHANGE = "message_exchange"; | ||
public static final String ROUTING_KEY = "message_routingKey"; | ||
|
||
@Bean | ||
public Queue queue() { | ||
return new Queue(QUEUE); | ||
} | ||
|
||
@Bean | ||
public TopicExchange exchange() { | ||
return new TopicExchange(EXCHANGE); | ||
} | ||
|
||
@Bean | ||
public Binding binding(Queue queue, TopicExchange exchange) { | ||
return BindingBuilder | ||
.bind(queue) | ||
.to(exchange) | ||
.with(ROUTING_KEY); | ||
} | ||
|
||
@Bean | ||
public MessageConverter messageConverter() { | ||
return new Jackson2JsonMessageConverter(); | ||
} | ||
|
||
@Bean | ||
public AmqpTemplate template(ConnectionFactory connectionFactory) { | ||
RabbitTemplate template = new RabbitTemplate(connectionFactory); | ||
template.setMessageConverter(messageConverter()); | ||
return template; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/se/iths/crimedatabase/messaging/MessageListener.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package se.iths.crimedatabase.messaging; | ||
|
||
import org.springframework.amqp.rabbit.annotation.RabbitListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class MessageListener { | ||
|
||
@RabbitListener(queues = MQConfig.QUEUE) | ||
public void listener(CustomMessage message) { | ||
System.out.println(message); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/se/iths/crimedatabase/messaging/MessagePublisher.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package se.iths.crimedatabase.messaging; | ||
|
||
import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.Date; | ||
import java.util.UUID; | ||
|
||
@RestController | ||
public class MessagePublisher { | ||
|
||
private final RabbitTemplate template; | ||
|
||
public MessagePublisher(RabbitTemplate template) { | ||
this.template = template; | ||
} | ||
|
||
@PostMapping("/publish") | ||
public String publishMessage(@RequestBody CustomMessage message) { | ||
message.setMessageID(UUID.randomUUID().toString()); | ||
message.setMessageDate(new Date()); | ||
template.convertAndSend(MQConfig.EXCHANGE, MQConfig.ROUTING_KEY, message); | ||
|
||
return "Message Published"; | ||
} | ||
} |
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