Skip to content

Commit

Permalink
Merge pull request #84 from Patlenlix/jms-issue-63
Browse files Browse the repository at this point in the history
JMS issue 63 - Basic Version
  • Loading branch information
LordRekishi committed Mar 25, 2022
2 parents b0b7892 + 5b58e09 commit a49be01
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 17 deletions.
38 changes: 27 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ different degree of access throughout the application.
* Database relations
* Dockerfile + Container
* Custom exceptions
* ![GitHub milestone](https://img.shields.io/github/milestones/progress-percent/Patlenlix/CrimeDatabase/1)
* ![GitHub milestone](https://img.shields.io/github/milestones/progress-percent/Patlenlix/CrimeDatabase/2)
* ![GitHub milestone](https://img.shields.io/github/milestones/progress-percent/Patlenlix/CrimeDatabase/5)

### Planned features

* ![GitHub milestone](https://img.shields.io/github/milestones/progress-percent/Patlenlix/CrimeDatabase/1)
* ![GitHub milestone](https://img.shields.io/github/milestones/progress-percent/Patlenlix/CrimeDatabase/2)
* ![GitHub milestone](https://img.shields.io/github/milestones/progress-percent/Patlenlix/CrimeDatabase/3)
* ![GitHub milestone](https://img.shields.io/github/milestones/progress-percent/Patlenlix/CrimeDatabase/4)
* ![GitHub milestone](https://img.shields.io/github/milestones/progress-percent/Patlenlix/CrimeDatabase/5)
* MySQL database


Expand All @@ -39,14 +39,16 @@ different degree of access throughout the application.

1. Clone/Fork this repo in your favorite IDE
2. Install Docker Desktop
3. Create and run MySQL docker image/container:
1. Run command in
Console: `docker run --name mysql -e MYSQL_ROOT_PASSWORD=my_secret_password -e 'MYSQL_ROOT_HOST=%' -e MYSQL_DATABASE=crime -e MYSQL_USER=user -e MYSQL_PASSWORD=password -p 3309:3306 mysql:latest`
4. Create a .jar file: Go to the folder of the application and run the following from your
Console: `./mvnw clean package`
5. Build the image: Go to the folder of the application and run the following from your Console:
`docker image build -t crimedb .`
6. Run the application: `docker container run crimedb`
3. Create and run RabbitMQ message broker image/container:
- Run command in
Console: `docker run -d --hostname my-rabbit --name some-rabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management`
- You can access management console at: `http://localhost:15672/`
- Username: guest
- Password: guest
4. Build the image:
- Go to the folder of the application and run the following from your Console:
`docker image build -t crimedb .`
5. Run the application: `docker container run crimedb`

---

Expand Down Expand Up @@ -150,3 +152,17 @@ POST and PUT needs a Body with a JSON object. Example of body for POST (PUT also
"time": "2022-03-18 15:48"
}
```

#### Publish RabbitMQ Messaging Service:

| HTTP-verb | URL | Authorization | Info |
|-----------|----------|-------------------------|--------------------------------------|
| POST | /publish | All authenticated users | Sends message internally to Listener |

POST needs a Body with a JSON object. Example of body for POST:

```json
{
"message": "Sample message"
}
```
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand All @@ -43,10 +42,21 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/se/iths/crimedatabase/messaging/CustomMessage.java
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 src/main/java/se/iths/crimedatabase/messaging/MQConfig.java
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 src/main/java/se/iths/crimedatabase/messaging/MessageListener.java
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);
}
}
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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

private final String[] urls = {"/addresses", "/categories", "/crimes", "/criminals", "/users", "/victims"};
private final String[] urls = {"/addresses", "/categories", "/crimes", "/criminals", "/users", "/victims", "/publish"};

//Used to authorize requests
@Override
protected void configure(HttpSecurity http) throws Exception {
http
Expand All @@ -28,13 +27,10 @@ protected void configure(HttpSecurity http) throws Exception {
.antMatchers("/victims").hasRole("ADMIN")
.antMatchers("/users").hasRole("ADMIN")
.anyRequest().authenticated();

}


@Bean
public PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}

}

0 comments on commit a49be01

Please sign in to comment.