Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
abd0123 committed May 19, 2024
1 parent ad61f2f commit 113339f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
15 changes: 15 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ services:
image: rabbitmq:3.13-management
ports:
- "5672:5672" # hacky method.. dont ever do this :(
- "15672:15672" # RabbitMQ management UI port
healthcheck:
test: rabbitmq-diagnostics -q ping
interval: 30s
timeout: 30s
retries: 3
networks:
- default
- frontend

service_mq-1:
image: rabbitmq:3.13-management
ports:
- "5671:5672" # hacky method.. dont ever do this :(
- "15671:15672" # RabbitMQ management UI port
healthcheck:
test: rabbitmq-diagnostics -q ping
interval: 30s
Expand Down
30 changes: 24 additions & 6 deletions controller/src/main/java/com/workup/controller/CLIHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
import java.util.Map;
import javassist.*;
import org.apache.logging.log4j.Level;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

public class CLIHandler {
@Autowired AmqpTemplate rabbitTemplate;
@Autowired RabbitTemplate rabbitTemplate;
private static final Map<String, String> appQueueMap = new HashMap<>();

static {
Expand Down Expand Up @@ -118,10 +119,10 @@ public String updateCommand(String app, String commandName, String className) th

private byte[] getByteCode(String commandName, String className)
throws InstantiationException,
IllegalAccessException,
NotFoundException,
IOException,
CannotCompileException {
IllegalAccessException,
NotFoundException,
IOException,
CannotCompileException {
ClassPool pool = ClassPool.getDefault();
pool.insertClassPath(new ClassClassPath(ControllerApplication.class));
CtClass ctClass = pool.get(className);
Expand All @@ -139,4 +140,21 @@ public String deleteCommand(String app, String commandName) {
appQueueMap.get(app), "", DeleteCommandRequest.builder().commandName(commandName).build());
return "Command sent";
}

@Command(description = "Change host")
public String setMQ(String host, int port) {
rabbitTemplate.convertAndSend(
ControllerQueueNames.JOBS,
"",
SetMessageQueueRequest.builder().withHost(host).withPort(port).build());
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host);
connectionFactory.setPort(port);
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
rabbitTemplate.setConnectionFactory(connectionFactory);
// for(Map.Entry<String, String> entry : appQueueMap.entrySet()){
// rabbitTemplate.convertAndSend(entry.getValue(), "", SetMessageQueueRequest.builder().withHost(host).withPort(port).build());
// }
return "Host changed";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.apache.logging.log4j.core.config.Configurator;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -113,6 +114,22 @@ public void receive(DeleteCommandRequest in) throws Exception {
}
}

@RabbitHandler
public void receive(SetMessageQueueRequest in) throws Exception {
try {
System.out.println("Setting message queue to: " + in.getHost() + ":" + in.getPort());
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(in.getHost());
connectionFactory.setPort(in.getPort());
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");

System.out.println("Message queue set to: " + in.getHost() + ":" + in.getPort());
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}

static class MyClassLoader extends ClassLoader {
public MyClassLoader(ClassLoader classLoader) {
super(classLoader);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.workup.shared.commands.controller;

import com.workup.shared.enums.ServiceQueueNames;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
import lombok.extern.jackson.Jacksonized;
Expand All @@ -10,5 +9,6 @@
@SuperBuilder(setterPrefix = "with")
@Jacksonized
public class SetMessageQueueRequest {
ServiceQueueNames messageQueue;
String host;
int port;
}

0 comments on commit 113339f

Please sign in to comment.