Skip to content

Commit

Permalink
rabbitmq working test queue
Browse files Browse the repository at this point in the history
  • Loading branch information
NiHaiden committed Jan 20, 2024
1 parent 3e235ed commit 61b3c40
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
{
"name": "Aries-Backend DevContainer File for quick dev env setup",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/java:0-17",
"image": "mcr.microsoft.com/devcontainers/java:0-21",
"features": {
"ghcr.io/devcontainers/features/java:1": {
"version": "none",
"installGradle": "true",
"gradleVersion": "8.3",
},
"ghcr.io/devcontainers/features/docker-in-docker:2": {
"dockerDashComposeVersion": "v2",
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id 'org.springframework.boot' version '3.1.0'
id 'org.springframework.boot' version '3.2.1'
id 'io.spring.dependency-management' version '1.1.0'
id 'org.jetbrains.kotlin.jvm' version '1.8.21'
id 'org.jetbrains.kotlin.plugin.spring' version '1.8.21'
Expand Down
2 changes: 1 addition & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
- 'POSTGRES_PASSWORD=secret'
- 'POSTGRES_USER=myuser'
ports:
- '5433:5432'
- '5432:5432'
pgadmin:
image: dpage/pgadmin4
restart: always
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ open class DockerService protected constructor(
@JoinColumn(name = "machineID")
var serviceMachine: DockerMachine,

@OneToOne

) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand Down
43 changes: 43 additions & 0 deletions src/main/kotlin/tech/niklas/ariesbackend/queue/RabbitMQConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package tech.niklas.ariesbackend.queue

import org.springframework.amqp.rabbit.connection.CachingConnectionFactory
import org.springframework.amqp.rabbit.connection.ConnectionFactory
import org.springframework.amqp.rabbit.core.RabbitTemplate
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class RabbitMQConfig {
@Value("\${spring.rabbitmq.host}")
private lateinit var host: String

@Value("\${spring.rabbitmq.port}")
private var port: Int = 0

@Value("\${spring.rabbitmq.username}")
private lateinit var username: String

@Value("\${spring.rabbitmq.password}")
private lateinit var password: String

@Bean
fun connectionFactory(): ConnectionFactory {
val connectionFactory = CachingConnectionFactory()
connectionFactory.setHost(host)
connectionFactory.setPort(port)
connectionFactory.username = username
connectionFactory.setPassword(password)
return connectionFactory
}

@Bean
fun rabbitTemplate(connectionFactory: ConnectionFactory?): RabbitTemplate? {
return connectionFactory?.let { RabbitTemplate(it) }
}

@Bean
fun dockerDeploy(): org.springframework.amqp.core.Queue {
return org.springframework.amqp.core.Queue("docker.deploy", true)
}
}
12 changes: 12 additions & 0 deletions src/main/kotlin/tech/niklas/ariesbackend/queue/RabbitMQListener.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package tech.niklas.ariesbackend.queue

import org.springframework.amqp.rabbit.annotation.RabbitListener
import org.springframework.stereotype.Component

@Component
class RabbitMQListener {
@RabbitListener(queues = arrayOf("docker.deploy"))
fun processMessage(message: String) {
println("Received message: $message")
}
}
21 changes: 21 additions & 0 deletions src/main/kotlin/tech/niklas/ariesbackend/web/RabbitController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tech.niklas.ariesbackend.web

import org.springframework.amqp.core.Queue
import org.springframework.amqp.rabbit.core.RabbitTemplate
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/rabbit")
class RabbitController(@Autowired private val rabbitTemplate: RabbitTemplate,
@Autowired private val queue: Queue) {

@GetMapping("/test")
fun sendMessage(): String {
val message: String = "Hello Rabbit!"
rabbitTemplate.convertAndSend(queue.name, message)
return "Message sent successfully!"
}
}

0 comments on commit 61b3c40

Please sign in to comment.