diff --git a/controller/pom.xml b/controller/pom.xml index f5ccdbdc..ae52daef 100644 --- a/controller/pom.xml +++ b/controller/pom.xml @@ -33,16 +33,41 @@ ${project.version} - org.testcontainers - cassandra + * + * + + + + com.workup + contracts + ${project.version} + - org.springframework.boot - spring-boot-starter-data-cassandra + * + * + + + + + com.workup + payments + ${project.version} + + + * + * + + + + com.workup + users + ${project.version} + - org.springframework.boot - spring-boot-starter-web + * + * diff --git a/controller/src/main/java/com/workup/controller/CLIHandler.java b/controller/src/main/java/com/workup/controller/CLIHandler.java index 9e0200fe..e0ee7dd7 100644 --- a/controller/src/main/java/com/workup/controller/CLIHandler.java +++ b/controller/src/main/java/com/workup/controller/CLIHandler.java @@ -3,9 +3,7 @@ import asg.cliche.CLIException; import asg.cliche.Command; import com.workup.shared.commands.controller.*; -import com.workup.shared.commands.jobs.requests.CreateJobRequest; import com.workup.shared.enums.ControllerQueueNames; -import com.workup.shared.enums.ServiceQueueNames; import java.io.IOException; import java.util.HashMap; import java.util.Map; @@ -57,6 +55,11 @@ public String maxdb(String app, int maxDBConn) { return "Command Sent!"; } + @Command(description = "Adds a new command") + public String addCommand(String app, String commandName, String className) throws Exception { + return updateCommand(app, commandName, className); + } + @Command(description = "starts a specific app") public String start(String app) { app = app.toLowerCase(); @@ -78,12 +81,7 @@ public String freeze(String app) { return "Command sent"; } - @Command(description = "stops a specific app") - public String setmq(String app, int appNum) { - return "setmq"; - } - - @Command(description = "stops a specific app") + @Command(description = "sets a logging level") public String setLoggingLevel(String app, String level) { app = app.toLowerCase(); if (!appQueueMap.containsKey(app)) { @@ -96,17 +94,6 @@ public String setLoggingLevel(String app, String level) { return "Command sent!!"; } - @Command(description = "test") - public void test() { - CreateJobRequest request = CreateJobRequest.builder().withTitle("Ziko").build(); - rabbitTemplate.convertSendAndReceive(ServiceQueueNames.JOBS, request); - } - - @Command(description = "Creates a new command") - public String addcommand(String app, String commandName, String className) { - return "Add command"; - } - @Command(description = "Updates an existing command") public String updateCommand(String app, String commandName, String className) throws Exception { app = app.toLowerCase(); @@ -120,6 +107,7 @@ public String updateCommand(String app, String commandName, String className) th "", UpdateCommandRequest.builder() .withCommandName(commandName) + .withClassName(className) .withByteCode(byteArray) .build()); } catch (Exception ex) { @@ -142,12 +130,13 @@ private byte[] getByteCode(String commandName, String className) } @Command(description = "Deletes an existing command") - public String deletecommand(String app, String commandName, String className) { - return "Delete command"; - } - - @Command(description = "stops a specific app") - public String updateClass(String app, int appNum) { - return "update class"; + public String deleteCommand(String app, String commandName) { + app = app.toLowerCase(); + if (!appQueueMap.containsKey(app)) { + return "Error: app can only be jobs, users, contracts or payments!"; + } + rabbitTemplate.convertAndSend( + appQueueMap.get(app), "", DeleteCommandRequest.builder().commandName(commandName).build()); + return "Command sent"; } } diff --git a/services/contracts/src/main/java/com/workup/contracts/ContractsApplication.java b/services/contracts/src/main/java/com/workup/contracts/ContractsApplication.java index 0af6686a..87d54e85 100644 --- a/services/contracts/src/main/java/com/workup/contracts/ContractsApplication.java +++ b/services/contracts/src/main/java/com/workup/contracts/ContractsApplication.java @@ -2,6 +2,7 @@ import com.workup.shared.enums.ControllerQueueNames; import com.workup.shared.enums.ServiceQueueNames; +import com.workup.shared.enums.ThreadPoolSize; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.amqp.core.AnonymousQueue; import org.springframework.amqp.core.Binding; @@ -79,8 +80,9 @@ public Binding fanoutBinding(FanoutExchange fanout, Queue controllerQueue) { @Bean public ThreadPoolTaskExecutor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); - executor.setCorePoolSize(50); - executor.setMaxPoolSize(50); + executor.setCorePoolSize(ThreadPoolSize.POOL_SIZE); + executor.setMaxPoolSize(ThreadPoolSize.POOL_SIZE); + executor.setWaitForTasksToCompleteOnShutdown(true); executor.setQueueCapacity(500); executor.setThreadNamePrefix("contracts-"); executor.initialize(); diff --git a/services/contracts/src/main/java/com/workup/contracts/ControllerMQListener.java b/services/contracts/src/main/java/com/workup/contracts/ControllerMQListener.java index 396c1ffa..d59baecf 100644 --- a/services/contracts/src/main/java/com/workup/contracts/ControllerMQListener.java +++ b/services/contracts/src/main/java/com/workup/contracts/ControllerMQListener.java @@ -1,35 +1,138 @@ package com.workup.contracts; +import com.workup.contracts.commands.ContractCommand; +import com.workup.contracts.commands.ContractCommandMap; +import com.workup.shared.commands.Command; +import com.workup.shared.commands.CommandRequest; +import com.workup.shared.commands.CommandResponse; +import com.workup.shared.commands.controller.ContinueRequest; +import com.workup.shared.commands.controller.DeleteCommandRequest; +import com.workup.shared.commands.controller.FreezeRequest; +import com.workup.shared.commands.controller.SetLoggingLevelRequest; import com.workup.shared.commands.controller.SetMaxThreadsRequest; -import java.lang.reflect.Field; +import com.workup.shared.commands.controller.UpdateCommandRequest; +import com.workup.shared.enums.ServiceQueueNames; +import com.workup.shared.enums.ThreadPoolSize; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +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.listener.RabbitListenerEndpointRegistry; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Service; @Service -@RabbitListener(queues = "#{controllerQueue.name}") +@RabbitListener(queues = "#{controllerQueue.name}", id = "#{controllerQueue.name}") public class ControllerMQListener { - + @Autowired public ContractCommandMap commandMap; @Autowired public ThreadPoolTaskExecutor taskExecutor; - @Autowired private ApplicationContext context; + @Autowired private RabbitListenerEndpointRegistry registry; @RabbitHandler public void receive(SetMaxThreadsRequest in) throws Exception { try { - ThreadPoolTaskExecutor myBean = context.getBean(ThreadPoolTaskExecutor.class); - Field maxPoolSize = ThreadPoolTaskExecutor.class.getDeclaredField("maxPoolSize"); - maxPoolSize.setAccessible(true); - maxPoolSize.set(myBean, in.getMaxThreads()); - Field corePoolSize = ThreadPoolTaskExecutor.class.getDeclaredField("corePoolSize"); - corePoolSize.setAccessible(true); - corePoolSize.set(myBean, in.getMaxThreads()); + System.out.println("Max threads is: " + taskExecutor.getMaxPoolSize()); + setThreads(in.getMaxThreads()); + ThreadPoolSize.POOL_SIZE = taskExecutor.getMaxPoolSize(); + System.out.println("Max threads set to: " + taskExecutor.getMaxPoolSize()); + } catch (Exception e) { + System.out.println(e.getMessage()); + e.printStackTrace(); + } + } + + @RabbitHandler + public void receive(SetLoggingLevelRequest in) throws Exception { + try { + Logger logger = LogManager.getLogger("com.workup.contracts"); + Configurator.setAllLevels(logger.getName(), Level.valueOf(in.getLevel())); + System.out.println("Logging level set to: " + in.getLevel()); + } catch (Exception e) { + System.out.println(e.getMessage()); + e.printStackTrace(); + } + } + + @RabbitHandler + public void receive(FreezeRequest in) throws Exception { + try { + registry.getListenerContainer(ServiceQueueNames.CONTRACTS).stop(); + setThreads(1); + System.out.println("Stopped all threads."); + } catch (Exception e) { + System.out.println(e.getMessage()); + e.printStackTrace(); + } + } + + @RabbitHandler + public void receive(ContinueRequest in) throws Exception { + try { + registry.getListenerContainer(ServiceQueueNames.CONTRACTS).start(); + setThreads(ThreadPoolSize.POOL_SIZE); + System.out.println("Continued all threads."); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } + + @SuppressWarnings("unchecked") + @RabbitHandler + public void receive(UpdateCommandRequest in) throws Exception { + try { + byte[] byteArray = in.getByteCode(); + Class clazz = + (Class) + (new MyClassLoader(this.getClass().getClassLoader()) + .loadClass(byteArray, in.getClassName())); + + commandMap.replaceCommand( + in.getCommandName(), + (Class>) + ((Command) clazz.newInstance()).getClass()); + + System.out.println("Updated command: " + in.getCommandName()); + // clazz.newInstance().Run(null); + } catch (Exception e) { + System.out.println(e.getMessage()); + e.printStackTrace(); + } + } + + @RabbitHandler + public void receive(DeleteCommandRequest in) throws Exception { + try { + commandMap.removeCommand(in.getCommandName()); + System.out.println("Deleted command: " + in.getCommandName()); + } catch (Exception e) { + System.out.println(e.getMessage()); + e.printStackTrace(); + } + } + + static class MyClassLoader extends ClassLoader { + public MyClassLoader(ClassLoader classLoader) { + super(classLoader); + } + + public Class loadClass(byte[] byteCode, String className) { + return defineClass(className, byteCode, 0, byteCode.length); + } + } + + private void setThreads(int threads) throws NoSuchFieldException, IllegalAccessException { + if (threads > taskExecutor.getCorePoolSize()) { + taskExecutor.setMaxPoolSize(threads); + taskExecutor.setCorePoolSize(threads); + } else { + taskExecutor.setCorePoolSize(threads); + taskExecutor.setMaxPoolSize(threads); + } + } } diff --git a/services/contracts/src/main/java/com/workup/contracts/RabbitMQListener.java b/services/contracts/src/main/java/com/workup/contracts/RabbitMQListener.java index a02686aa..d8a305cf 100644 --- a/services/contracts/src/main/java/com/workup/contracts/RabbitMQListener.java +++ b/services/contracts/src/main/java/com/workup/contracts/RabbitMQListener.java @@ -1,6 +1,8 @@ package com.workup.contracts; import com.workup.contracts.commands.*; +import com.workup.shared.commands.Command; +import com.workup.shared.commands.CommandRequest; import com.workup.shared.commands.contracts.requests.ContractTerminationRequest; import com.workup.shared.commands.contracts.requests.EvaluateMilestoneRequest; import com.workup.shared.commands.contracts.requests.GetContractRequest; @@ -19,6 +21,7 @@ import com.workup.shared.commands.contracts.responses.MarkPaymentCompletedResponse; import com.workup.shared.commands.contracts.responses.ProgressMilestoneResponse; import com.workup.shared.commands.contracts.responses.ViewContractMilestonesResponse; +import com.workup.shared.enums.HttpStatusCode; import com.workup.shared.enums.ServiceQueueNames; import java.util.concurrent.CompletableFuture; import org.springframework.amqp.rabbit.annotation.RabbitHandler; @@ -28,7 +31,7 @@ import org.springframework.stereotype.Service; @Service -@RabbitListener(queues = ServiceQueueNames.CONTRACTS) +@RabbitListener(queues = ServiceQueueNames.CONTRACTS, id = ServiceQueueNames.CONTRACTS) public class RabbitMQListener { @Autowired public ContractCommandMap commandMap; @@ -37,74 +40,157 @@ public class RabbitMQListener { @Async public CompletableFuture receive(InitiateContractRequest in) throws Exception { - InitiateContractResponse response = - ((InitiateContractCommand) commandMap.getCommand("InitiateContract")).Run(in); - return CompletableFuture.completedFuture(response); + try { + InitiateContractResponse response = + (InitiateContractResponse) + ((Command) commandMap.getCommand("InitiateContract")).Run(in); + return CompletableFuture.completedFuture(response); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + InitiateContractResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(ContractTerminationRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((RequestContractTerminationCommand) commandMap.getCommand("RequestContractTermination")) - .Run(in)); + try { + return CompletableFuture.completedFuture( + (ContractTerminationResponse) + ((Command) commandMap.getCommand("RequestContractTermination")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + ContractTerminationResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(HandleTerminationRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((HandleTerminationRequestCommand) commandMap.getCommand("HandleTerminationRequest")) - .Run(in)); + try { + return CompletableFuture.completedFuture( + (HandleTerminationResponse) + ((Command) commandMap.getCommand("HandleTerminationRequest")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + HandleTerminationResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(MarkPaymentCompletedRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((MarkMilestoneAsPaidCommand) commandMap.getCommand("MarkMilestoneAsPaid")).Run(in)); + try { + return CompletableFuture.completedFuture( + (MarkPaymentCompletedResponse) + ((Command) commandMap.getCommand("MarkMilestoneAsPaid")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + MarkPaymentCompletedResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(ViewContractMilestonesRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((ViewContractMilestonesCommand) commandMap.getCommand("ViewContractMilestones")).Run(in)); + try { + return CompletableFuture.completedFuture( + (ViewContractMilestonesResponse) + ((Command) commandMap.getCommand("ViewContractMilestones")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + ViewContractMilestonesResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(GetContractRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((GetContractCommand) commandMap.getCommand("GetContract")).Run(in)); + try { + return CompletableFuture.completedFuture( + (GetContractResponse) + ((Command) commandMap.getCommand("GetContract")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + GetContractResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(EvaluateMilestoneRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((EvaluateMilestoneCommand) commandMap.getCommand("EvaluateMilestone")).Run(in)); + try { + return CompletableFuture.completedFuture( + (EvaluateMilestoneResponse) + ((Command) commandMap.getCommand("EvaluateMilestone")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + EvaluateMilestoneResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(ProgressMilestoneRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((ProgressMilestoneCommand) commandMap.getCommand("ProgressMilestone")).Run(in)); + try { + return CompletableFuture.completedFuture( + (ProgressMilestoneResponse) + ((Command) commandMap.getCommand("ProgressMilestone")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + ProgressMilestoneResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(GetPendingTerminationsRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((GetPendingTerminationsCommand) commandMap.getCommand("GetPendingTerminations")).Run(in)); + try { + return CompletableFuture.completedFuture( + (GetPendingTerminationsResponse) + ((Command) commandMap.getCommand("GetPendingTerminations")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + GetPendingTerminationsResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } // NEW_COMMAND_BOILERPLATE diff --git a/services/contracts/src/main/java/com/workup/contracts/commands/ContractCommand.java b/services/contracts/src/main/java/com/workup/contracts/commands/ContractCommand.java index 998734e3..2c86db5d 100644 --- a/services/contracts/src/main/java/com/workup/contracts/commands/ContractCommand.java +++ b/services/contracts/src/main/java/com/workup/contracts/commands/ContractCommand.java @@ -13,13 +13,13 @@ public abstract class ContractCommand implements Command { - @Setter AmqpTemplate rabbitTemplate; + @Setter public AmqpTemplate rabbitTemplate; - @Setter ContractRepository contractRepository; + @Setter public ContractRepository contractRepository; - @Setter ContractMilestoneRepository contractMilestoneRepository; + @Setter public ContractMilestoneRepository contractMilestoneRepository; - @Setter TerminationRequestRepository terminationRequestRepository; + @Setter public TerminationRequestRepository terminationRequestRepository; - @Setter RedisService redisService; + @Setter public RedisService redisService; } diff --git a/services/contracts/src/main/java/com/workup/contracts/commands/ContractCommandMap.java b/services/contracts/src/main/java/com/workup/contracts/commands/ContractCommandMap.java index afa82792..42fd6693 100644 --- a/services/contracts/src/main/java/com/workup/contracts/commands/ContractCommandMap.java +++ b/services/contracts/src/main/java/com/workup/contracts/commands/ContractCommandMap.java @@ -15,15 +15,15 @@ public class ContractCommandMap extends CommandMap> { - @Autowired ContractRepository contractRepository; + @Autowired public ContractRepository contractRepository; - @Autowired ContractMilestoneRepository contractMilestoneRepository; + @Autowired public ContractMilestoneRepository contractMilestoneRepository; - @Autowired TerminationRequestRepository terminationRequestRepository; + @Autowired public TerminationRequestRepository terminationRequestRepository; - @Autowired AmqpTemplate rabbitTemplate; + @Autowired public AmqpTemplate rabbitTemplate; - @Autowired RedisService redisService; + @Autowired public RedisService redisService; public void registerCommands() { commands.put("InitiateContract", InitiateContractCommand.class); diff --git a/services/jobs/src/main/java/com/workup/jobs/ControllerMQListener.java b/services/jobs/src/main/java/com/workup/jobs/ControllerMQListener.java index 8ef28ae6..b4ae802b 100644 --- a/services/jobs/src/main/java/com/workup/jobs/ControllerMQListener.java +++ b/services/jobs/src/main/java/com/workup/jobs/ControllerMQListener.java @@ -8,7 +8,6 @@ import com.workup.shared.commands.controller.*; import com.workup.shared.enums.ServiceQueueNames; import com.workup.shared.enums.ThreadPoolSize; -import java.lang.reflect.Field; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -29,15 +28,17 @@ public class ControllerMQListener { @Autowired private ApplicationContext context; @Autowired private RabbitListenerEndpointRegistry registry; + private static final Logger logger = LogManager.getLogger(ControllerMQListener.class); + @RabbitHandler public void receive(SetMaxThreadsRequest in) throws Exception { try { - System.out.println("Max threads is: " + taskExecutor.getMaxPoolSize()); + logger.info("Max threads is: " + taskExecutor.getMaxPoolSize()); setThreads(in.getMaxThreads()); ThreadPoolSize.POOL_SIZE = taskExecutor.getMaxPoolSize(); - System.out.println("Max threads set to: " + taskExecutor.getMaxPoolSize()); + logger.info("Max threads set to: " + taskExecutor.getMaxPoolSize()); } catch (Exception e) { - System.out.println(e.getMessage()); + logger.info(e.getMessage()); e.printStackTrace(); } } @@ -45,10 +46,11 @@ public void receive(SetMaxThreadsRequest in) throws Exception { @RabbitHandler public void receive(SetLoggingLevelRequest in) throws Exception { try { - Logger logger = LogManager.getRootLogger(); + Logger logger = LogManager.getLogger("com.workup.jobs"); Configurator.setAllLevels(logger.getName(), Level.valueOf(in.getLevel())); + logger.info("Logging level set to: " + in.getLevel()); } catch (Exception e) { - System.out.println(e.getMessage()); + logger.info(e.getMessage()); e.printStackTrace(); } } @@ -56,12 +58,11 @@ public void receive(SetLoggingLevelRequest in) throws Exception { @RabbitHandler public void receive(FreezeRequest in) throws Exception { try { - registry.getListenerContainer(ServiceQueueNames.JOBS).stop(); - taskExecutor.shutdown(); - setThreads(0); - System.out.println("Stopped all threads."); + registry.getListenerContainer(ServiceQueueNames.PAYMENTS).stop(); + setThreads(1); + logger.info("Stopped all threads."); } catch (Exception e) { - System.out.println(e.getMessage()); + logger.info(e.getMessage()); e.printStackTrace(); } } @@ -69,11 +70,11 @@ public void receive(FreezeRequest in) throws Exception { @RabbitHandler public void receive(ContinueRequest in) throws Exception { try { - taskExecutor.start(); + registry.getListenerContainer(ServiceQueueNames.PAYMENTS).start(); setThreads(ThreadPoolSize.POOL_SIZE); - registry.getListenerContainer(ServiceQueueNames.JOBS).start(); + logger.info("Continued all threads."); } catch (Exception e) { - System.out.println(e.getMessage()); + logger.info(e.getMessage()); e.printStackTrace(); } } @@ -82,19 +83,30 @@ public void receive(ContinueRequest in) throws Exception { @RabbitHandler public void receive(UpdateCommandRequest in) throws Exception { try { - String className = commandMap.getCommand(in.getCommandName()).getClass().getName(); byte[] byteArray = in.getByteCode(); Class clazz = (Class) - (new MyClassLoader(this.getClass().getClassLoader()).loadClass(byteArray, className)); + (new MyClassLoader(this.getClass().getClassLoader()) + .loadClass(byteArray, in.getClassName())); commandMap.replaceCommand( in.getCommandName(), (Class>) ((Command) clazz.newInstance()).getClass()); - System.out.println("Updated command: " + in.getCommandName()); + logger.info("Updated command: " + in.getCommandName()); // clazz.newInstance().Run(null); + } catch (Exception e) { + logger.info(e.getMessage()); + e.printStackTrace(); + } + } + + @RabbitHandler + public void receive(DeleteCommandRequest in) throws Exception { + try { + commandMap.removeCommand(in.getCommandName()); + System.out.println("Deleted command: " + in.getCommandName()); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); @@ -112,12 +124,12 @@ public Class loadClass(byte[] byteCode, String className) { } private void setThreads(int threads) throws NoSuchFieldException, IllegalAccessException { - ThreadPoolTaskExecutor myBean = context.getBean(ThreadPoolTaskExecutor.class); - Field maxPoolSize = ThreadPoolTaskExecutor.class.getDeclaredField("maxPoolSize"); - maxPoolSize.setAccessible(true); - maxPoolSize.set(myBean, threads); - Field corePoolSize = ThreadPoolTaskExecutor.class.getDeclaredField("corePoolSize"); - corePoolSize.setAccessible(true); - corePoolSize.set(myBean, threads); + if (threads > taskExecutor.getCorePoolSize()) { + taskExecutor.setMaxPoolSize(threads); + taskExecutor.setCorePoolSize(threads); + } else { + taskExecutor.setCorePoolSize(threads); + taskExecutor.setMaxPoolSize(threads); + } } } diff --git a/services/jobs/src/main/java/com/workup/jobs/RabbitMQListener.java b/services/jobs/src/main/java/com/workup/jobs/RabbitMQListener.java index 76e5a1b7..fd5c8beb 100644 --- a/services/jobs/src/main/java/com/workup/jobs/RabbitMQListener.java +++ b/services/jobs/src/main/java/com/workup/jobs/RabbitMQListener.java @@ -1,13 +1,6 @@ package com.workup.jobs; -import com.workup.jobs.commands.AcceptProposalCommand; -import com.workup.jobs.commands.CreateProposalCommand; -import com.workup.jobs.commands.GetJobByIdCommand; -import com.workup.jobs.commands.GetMyJobsCommand; -import com.workup.jobs.commands.GetMyProposalsCommand; -import com.workup.jobs.commands.GetProposalsByJobIdCommand; import com.workup.jobs.commands.JobCommandMap; -import com.workup.jobs.commands.SearchJobsCommand; import com.workup.shared.commands.Command; import com.workup.shared.commands.CommandRequest; import com.workup.shared.commands.jobs.proposals.requests.AcceptProposalRequest; @@ -26,6 +19,7 @@ import com.workup.shared.commands.jobs.responses.GetJobByIdResponse; import com.workup.shared.commands.jobs.responses.GetMyJobsResponse; import com.workup.shared.commands.jobs.responses.SearchJobsResponse; +import com.workup.shared.enums.HttpStatusCode; import com.workup.shared.enums.ServiceQueueNames; import java.util.concurrent.CompletableFuture; import org.springframework.amqp.rabbit.annotation.RabbitHandler; @@ -43,69 +37,141 @@ public class RabbitMQListener { @RabbitHandler @Async public CompletableFuture receive(CreateJobRequest in) throws Exception { - CreateJobResponse resp = - (CreateJobResponse) - ((Command) commandMap.getCommand("CreateJob")).Run(in); - return CompletableFuture.completedFuture(resp); + try { + CreateJobResponse resp = + (CreateJobResponse) + ((Command) commandMap.getCommand("CreateJob")).Run(in); + return CompletableFuture.completedFuture(resp); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + CreateJobResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(CreateProposalRequest in) throws Exception { - CreateProposalResponse response = - ((CreateProposalCommand) commandMap.getCommand("CreateProposal")).Run(in); - return CompletableFuture.completedFuture(response); + try { + CreateProposalResponse response = + (CreateProposalResponse) + ((Command) commandMap.getCommand("CreateProposal")).Run(in); + return CompletableFuture.completedFuture(response); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + CreateProposalResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(GetJobByIdRequest request) throws Exception { - GetJobByIdResponse response = - ((GetJobByIdCommand) commandMap.getCommand("GetJobById")).Run(request); - return CompletableFuture.completedFuture(response); + try { + GetJobByIdResponse response = + (GetJobByIdResponse) + ((Command) commandMap.getCommand("GetJobById")).Run(request); + return CompletableFuture.completedFuture(response); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + GetJobByIdResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(SearchJobsRequest request) throws Exception { - SearchJobsResponse response = - ((SearchJobsCommand) commandMap.getCommand("SearchJobs")).Run(request); - return CompletableFuture.completedFuture(response); + try { + SearchJobsResponse response = + (SearchJobsResponse) + ((Command) commandMap.getCommand("SearchJobs")).Run(request); + return CompletableFuture.completedFuture(response); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + SearchJobsResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(GetMyJobsRequest request) throws Exception { - GetMyJobsResponse response = - ((GetMyJobsCommand) commandMap.getCommand("GetMyJobs")).Run(request); - return CompletableFuture.completedFuture(response); + try { + GetMyJobsResponse response = + (GetMyJobsResponse) + ((Command) commandMap.getCommand("GetMyJobs")).Run(request); + return CompletableFuture.completedFuture(response); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + GetMyJobsResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(AcceptProposalRequest request) throws Exception { - AcceptProposalResponse response = - ((AcceptProposalCommand) commandMap.getCommand("AcceptProposal")).Run(request); - return CompletableFuture.completedFuture(response); + try { + AcceptProposalResponse response = + (AcceptProposalResponse) + ((Command) commandMap.getCommand("AcceptProposal")).Run(request); + return CompletableFuture.completedFuture(response); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + AcceptProposalResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(GetProposalsByJobIdRequest request) throws Exception { - GetProposalsByJobIdResponse response = - ((GetProposalsByJobIdCommand) commandMap.getCommand("GetProposalsByJobId")).Run(request); - return CompletableFuture.completedFuture(response); + try { + GetProposalsByJobIdResponse response = + (GetProposalsByJobIdResponse) + ((Command) commandMap.getCommand("GetProposalsByJobId")) + .Run(request); + return CompletableFuture.completedFuture(response); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + GetProposalsByJobIdResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(GetMyProposalsRequest request) throws Exception { - GetMyProposalsResponse response = - ((GetMyProposalsCommand) commandMap.getCommand("GetMyProposals")).Run(request); - return CompletableFuture.completedFuture(response); + try { + GetMyProposalsResponse response = + (GetMyProposalsResponse) + ((Command) commandMap.getCommand("GetMyProposals")).Run(request); + return CompletableFuture.completedFuture(response); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + GetMyProposalsResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } } diff --git a/services/jobs/src/test/java/com/workup/jobs/ContractsMockingListener.java b/services/jobs/src/test/java/com/workup/jobs/ContractsMockingListener.java index a466ec3c..28ce74bb 100644 --- a/services/jobs/src/test/java/com/workup/jobs/ContractsMockingListener.java +++ b/services/jobs/src/test/java/com/workup/jobs/ContractsMockingListener.java @@ -21,10 +21,18 @@ public class ContractsMockingListener { @Async public CompletableFuture receive(InitiateContractRequest in) throws Exception { - return CompletableFuture.completedFuture( - InitiateContractResponse.builder() - .withContractId(contractIdToBeReturned) - .withStatusCode(statusCodeToBeReturned) - .build()); + try { + return CompletableFuture.completedFuture( + InitiateContractResponse.builder() + .withContractId(contractIdToBeReturned) + .withStatusCode(statusCodeToBeReturned) + .build()); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + InitiateContractResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } } diff --git a/services/payments/src/main/java/com/workup/payments/ControllerMQListener.java b/services/payments/src/main/java/com/workup/payments/ControllerMQListener.java index 17526e5f..8877f6e4 100644 --- a/services/payments/src/main/java/com/workup/payments/ControllerMQListener.java +++ b/services/payments/src/main/java/com/workup/payments/ControllerMQListener.java @@ -1,11 +1,14 @@ package com.workup.payments; +import com.workup.payments.commands.PaymentCommand; import com.workup.payments.commands.PaymentCommandMap; +import com.workup.shared.commands.Command; +import com.workup.shared.commands.CommandRequest; +import com.workup.shared.commands.CommandResponse; import com.workup.shared.commands.controller.*; import com.workup.shared.enums.ServiceQueueNames; import com.workup.shared.enums.ThreadPoolSize; import com.zaxxer.hikari.HikariDataSource; -import java.lang.reflect.Field; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -21,21 +24,23 @@ @Service @RabbitListener(queues = "#{controllerQueue.name}", id = "#{controllerQueue.name}") public class ControllerMQListener { + @Autowired public HikariDataSource hikariDataSource; @Autowired public PaymentCommandMap commandMap; @Autowired public ThreadPoolTaskExecutor taskExecutor; - @Autowired private ApplicationContext context; - @Autowired private RabbitListenerEndpointRegistry registry; - @Autowired private HikariDataSource hikariDataSource; + @Autowired public ApplicationContext context; + @Autowired public RabbitListenerEndpointRegistry registry; + + private static final Logger logger = LogManager.getLogger(ControllerMQListener.class); @RabbitHandler public void receive(SetMaxThreadsRequest in) throws Exception { try { - System.out.println("Max threads is: " + taskExecutor.getMaxPoolSize()); + logger.info("Max threads is: " + taskExecutor.getMaxPoolSize()); setThreads(in.getMaxThreads()); ThreadPoolSize.POOL_SIZE = taskExecutor.getMaxPoolSize(); - System.out.println("Max threads set to: " + taskExecutor.getMaxPoolSize()); + logger.info("Max threads set to: " + taskExecutor.getMaxPoolSize()); } catch (Exception e) { - System.out.println(e.getMessage()); + logger.info(e.getMessage()); e.printStackTrace(); } } @@ -43,10 +48,11 @@ public void receive(SetMaxThreadsRequest in) throws Exception { @RabbitHandler public void receive(SetLoggingLevelRequest in) throws Exception { try { - Logger logger = LogManager.getRootLogger(); + Logger logger = LogManager.getLogger("com.workup.payments"); Configurator.setAllLevels(logger.getName(), Level.valueOf(in.getLevel())); + logger.info("Logging level set to: " + in.getLevel()); } catch (Exception e) { - System.out.println(e.getMessage()); + logger.info(e.getMessage()); e.printStackTrace(); } } @@ -54,12 +60,11 @@ public void receive(SetLoggingLevelRequest in) throws Exception { @RabbitHandler public void receive(FreezeRequest in) throws Exception { try { - registry.getListenerContainer(ServiceQueueNames.JOBS).stop(); - taskExecutor.shutdown(); - setThreads(0); - System.out.println("Stopped all threads."); + registry.getListenerContainer(ServiceQueueNames.PAYMENTS).stop(); + setThreads(1); + logger.info("Stopped all threads."); } catch (Exception e) { - System.out.println(e.getMessage()); + logger.info(e.getMessage()); e.printStackTrace(); } } @@ -67,59 +72,82 @@ public void receive(FreezeRequest in) throws Exception { @RabbitHandler public void receive(ContinueRequest in) throws Exception { try { - taskExecutor.start(); + registry.getListenerContainer(ServiceQueueNames.PAYMENTS).start(); setThreads(ThreadPoolSize.POOL_SIZE); - registry.getListenerContainer(ServiceQueueNames.JOBS).start(); + logger.info("Continued all threads."); } catch (Exception e) { - System.out.println(e.getMessage()); + logger.info(e.getMessage()); e.printStackTrace(); } } + @SuppressWarnings("unchecked") @RabbitHandler public void receive(UpdateCommandRequest in) throws Exception { - // try { - // String className = commandMap.getCommand(in.getName()).getClass().getName(); - // System.out.println("Updating command: " + in.getName()); - // System.out.println("Class: " + className); - // Class newClass = new MyClassLoader().loadClass(in.getByteCode(), className); - // commandMap.replaceCommand(in.getName(), newClass); - // } catch (Exception e) { - // System.out.println(e.getMessage()); - // e.printStackTrace(); - // } + try { + byte[] byteArray = in.getByteCode(); + Class clazz = + (Class) + (new MyClassLoader(this.getClass().getClassLoader()) + .loadClass(byteArray, in.getClassName())); + + commandMap.replaceCommand( + in.getCommandName(), + (Class>) + ((Command) clazz.newInstance()).getClass()); + + logger.info("Updated command: " + in.getCommandName()); + // clazz.newInstance().Run(null); + } catch (Exception e) { + logger.info(e.getMessage()); + e.printStackTrace(); + } } @RabbitHandler - private void SetMaxDBConnections(SetMaxDBConnectionsRequest in) { + public void receive(DeleteCommandRequest in) throws Exception { try { - if (hikariDataSource == null) { - System.out.println("HikariDataSource is null"); - return; - } - System.out.println("Max DB connections is: " + hikariDataSource.getMaximumPoolSize()); - hikariDataSource.setMaximumPoolSize(in.getMaxDBConnections()); - System.out.println("Max DB connections set to: " + hikariDataSource.getMaximumPoolSize()); + commandMap.removeCommand(in.getCommandName()); + System.out.println("Deleted command: " + in.getCommandName()); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } + static class MyClassLoader extends ClassLoader { + public MyClassLoader(ClassLoader classLoader) { + super(classLoader); + } + + public Class loadClass(byte[] byteCode, String className) { + return defineClass(className, byteCode, 0, byteCode.length); + } + } + private void setThreads(int threads) throws NoSuchFieldException, IllegalAccessException { - ThreadPoolTaskExecutor myBean = context.getBean(ThreadPoolTaskExecutor.class); - Field maxPoolSize = ThreadPoolTaskExecutor.class.getDeclaredField("maxPoolSize"); - maxPoolSize.setAccessible(true); - maxPoolSize.set(myBean, threads); - Field corePoolSize = ThreadPoolTaskExecutor.class.getDeclaredField("corePoolSize"); - corePoolSize.setAccessible(true); - corePoolSize.set(myBean, threads); + if (threads > taskExecutor.getCorePoolSize()) { + taskExecutor.setMaxPoolSize(threads); + taskExecutor.setCorePoolSize(threads); + } else { + taskExecutor.setCorePoolSize(threads); + taskExecutor.setMaxPoolSize(threads); + } } -} -class MyClassLoader extends ClassLoader { - public Class loadClass(byte[] byteCode, String className) { - System.out.println("Loading class: " + className); - return defineClass(className, byteCode, 0, byteCode.length); + @RabbitHandler + private void SetMaxDBConnections(SetMaxDBConnectionsRequest in) { + try { + if (hikariDataSource == null) { + logger.info("HikariDataSource is null"); + return; + } + logger.info("Max DB connections is: " + hikariDataSource.getMaximumPoolSize()); + hikariDataSource.setMaximumPoolSize(in.getMaxDBConnections()); + logger.info("Max DB connections set to: " + hikariDataSource.getMaximumPoolSize()); + } catch (Exception e) { + logger.info(e.getMessage()); + e.printStackTrace(); + } } } diff --git a/services/payments/src/main/java/com/workup/payments/PaymentsApplication.java b/services/payments/src/main/java/com/workup/payments/PaymentsApplication.java index 819e50fc..dfd16f9b 100644 --- a/services/payments/src/main/java/com/workup/payments/PaymentsApplication.java +++ b/services/payments/src/main/java/com/workup/payments/PaymentsApplication.java @@ -3,7 +3,6 @@ import com.workup.shared.enums.ControllerQueueNames; import com.workup.shared.enums.ServiceQueueNames; import com.workup.shared.enums.ThreadPoolSize; - import org.springframework.amqp.core.AnonymousQueue; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; diff --git a/services/payments/src/main/java/com/workup/payments/RabbitMQListener.java b/services/payments/src/main/java/com/workup/payments/RabbitMQListener.java index 471a93ba..a130bea1 100644 --- a/services/payments/src/main/java/com/workup/payments/RabbitMQListener.java +++ b/services/payments/src/main/java/com/workup/payments/RabbitMQListener.java @@ -2,14 +2,8 @@ import com.workup.payments.commands.PaymentCommandMap; import com.workup.payments.commands.paymentrequest.*; -import com.workup.payments.commands.paymenttransaction.GetClientPaymentTransactionsCommand; -import com.workup.payments.commands.paymenttransaction.GetFreelancerPaymentTransactionsCommand; -import com.workup.payments.commands.wallet.CreateWalletCommand; -import com.workup.payments.commands.wallet.GetWalletCommand; -import com.workup.payments.commands.wallettransaction.CreateWalletTransactionCommand; -import com.workup.payments.commands.wallettransaction.GetWalletTransactionCommand; -import com.workup.payments.commands.wallettransaction.GetWalletTransactionsCommand; -import com.workup.payments.commands.wallettransaction.WithdrawFromWalletCommand; +import com.workup.shared.commands.Command; +import com.workup.shared.commands.CommandRequest; import com.workup.shared.commands.payments.paymentrequest.requests.*; import com.workup.shared.commands.payments.paymentrequest.responses.*; import com.workup.shared.commands.payments.paymenttransaction.requests.GetClientPaymentTransactionsRequest; @@ -28,6 +22,7 @@ import com.workup.shared.commands.payments.wallettransaction.responses.GetWalletTransactionResponse; import com.workup.shared.commands.payments.wallettransaction.responses.GetWalletTransactionsResponse; import com.workup.shared.commands.payments.wallettransaction.responses.WithdrawFromWalletResponse; +import com.workup.shared.enums.HttpStatusCode; import com.workup.shared.enums.ServiceQueueNames; import java.util.concurrent.CompletableFuture; import org.springframework.amqp.rabbit.annotation.RabbitHandler; @@ -37,7 +32,7 @@ import org.springframework.stereotype.Service; @Service -@RabbitListener(queues = ServiceQueueNames.PAYMENTS) +@RabbitListener(queues = ServiceQueueNames.PAYMENTS, id = ServiceQueueNames.PAYMENTS) public class RabbitMQListener { @Autowired public PaymentCommandMap commandMap; @@ -46,109 +41,225 @@ public class RabbitMQListener { @Async public CompletableFuture receive(CreatePaymentRequestRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((CreatePaymentRequestCommand) commandMap.getCommand("CreatePaymentRequest")).Run(in)); + try { + return CompletableFuture.completedFuture( + (CreatePaymentRequestResponse) + ((Command) commandMap.getCommand("CreatePaymentRequest")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + CreatePaymentRequestResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive( CreateWalletTransactionRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((CreateWalletTransactionCommand) commandMap.getCommand("CreateWalletTransaction")) - .Run(in)); + try { + return CompletableFuture.completedFuture( + (CreateWalletTransactionResponse) + ((Command) commandMap.getCommand("CreateWalletTransaction")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + CreateWalletTransactionResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive( GetClientPaymentRequestsRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((GetClientPaymentRequestsCommand) commandMap.getCommand("GetClientPaymentRequests")) - .Run(in)); + try { + return CompletableFuture.completedFuture( + (GetClientPaymentRequestsResponse) + ((Command) commandMap.getCommand("GetClientPaymentRequests")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + GetClientPaymentRequestsResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(GetWalletTransactionRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((GetWalletTransactionCommand) commandMap.getCommand("GetWalletTransaction")).Run(in)); + try { + return CompletableFuture.completedFuture( + (GetWalletTransactionResponse) + ((Command) commandMap.getCommand("GetWalletTransaction")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + GetWalletTransactionResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(GetWalletTransactionsRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((GetWalletTransactionsCommand) commandMap.getCommand("GetWalletTransactions")).Run(in)); + try { + return CompletableFuture.completedFuture( + (GetWalletTransactionsResponse) + ((Command) commandMap.getCommand("GetWalletTransactions")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + GetWalletTransactionsResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(WithdrawFromWalletRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((WithdrawFromWalletCommand) commandMap.getCommand("WithdrawFromWallet")).Run(in)); + try { + return CompletableFuture.completedFuture( + (WithdrawFromWalletResponse) + ((Command) commandMap.getCommand("WithdrawFromWallet")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + WithdrawFromWalletResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive( GetFreelancerPaymentRequestsRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((GetFreelancerPaymentRequestsCommand) - commandMap.getCommand("GetFreelancerPaymentRequests")) - .Run(in)); + try { + return CompletableFuture.completedFuture( + (GetFreelancerPaymentRequestsResponse) + ((Command) commandMap.getCommand("GetFreelancerPaymentRequests")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + GetFreelancerPaymentRequestsResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(GetPaymentRequestRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((GetPaymentRequestCommand) commandMap.getCommand("GetPaymentRequest")).Run(in)); + try { + return CompletableFuture.completedFuture( + (GetPaymentRequestResponse) + ((Command) commandMap.getCommand("GetPaymentRequest")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + GetPaymentRequestResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(PayPaymentRequestRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((PayPaymentRequestCommand) commandMap.getCommand("PayPaymentRequest")).Run(in)); + try { + return CompletableFuture.completedFuture( + (PayPaymentRequestResponse) + ((Command) commandMap.getCommand("PayPaymentRequest")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + PayPaymentRequestResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(CreateWalletRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((CreateWalletCommand) commandMap.getCommand("CreateWallet")).Run(in)); + try { + return CompletableFuture.completedFuture( + (CreateWalletResponse) + ((Command) commandMap.getCommand("CreateWallet")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + CreateWalletResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(GetWalletRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((GetWalletCommand) commandMap.getCommand("GetWallet")).Run(in)); + try { + return CompletableFuture.completedFuture( + (GetWalletResponse) + ((Command) commandMap.getCommand("GetWallet")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + GetWalletResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive( GetClientPaymentTransactionsRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((GetClientPaymentTransactionsCommand) - commandMap.getCommand("GetClientPaymentTransactions")) - .Run(in)); + try { + return CompletableFuture.completedFuture( + (GetClientPaymentTransactionsResponse) + ((Command) commandMap.getCommand("GetClientPaymentTransactions")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + GetClientPaymentTransactionsResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive( GetFreelancerPaymentTransactionsRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((GetFreelancerPaymentTransactionsCommand) - commandMap.getCommand("GetFreelancerPaymentTransactions")) - .Run(in)); + try { + return CompletableFuture.completedFuture( + (GetFreelancerPaymentTransactionsResponse) + ((Command) + commandMap.getCommand("GetFreelancerPaymentTransactions")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + GetFreelancerPaymentTransactionsResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } } diff --git a/services/payments/src/main/java/com/workup/payments/commands/PaymentCommand.java b/services/payments/src/main/java/com/workup/payments/commands/PaymentCommand.java index 16fef43f..8b04219c 100644 --- a/services/payments/src/main/java/com/workup/payments/commands/PaymentCommand.java +++ b/services/payments/src/main/java/com/workup/payments/commands/PaymentCommand.java @@ -14,9 +14,9 @@ public abstract class PaymentCommand implements Command { - private PaymentRequestRepository paymentRequestRepository; - private PaymentTransactionRepository paymentTransactionRepository; - private WalletRepository walletRepository; - private WalletTransactionRepository walletTransactionRepository; - private RedisService redisService; + public PaymentRequestRepository paymentRequestRepository; + public PaymentTransactionRepository paymentTransactionRepository; + public WalletRepository walletRepository; + public WalletTransactionRepository walletTransactionRepository; + public RedisService redisService; } diff --git a/services/payments/src/main/java/com/workup/payments/commands/PaymentCommandMap.java b/services/payments/src/main/java/com/workup/payments/commands/PaymentCommandMap.java index 7c4d561e..3faaf3f5 100644 --- a/services/payments/src/main/java/com/workup/payments/commands/PaymentCommandMap.java +++ b/services/payments/src/main/java/com/workup/payments/commands/PaymentCommandMap.java @@ -24,15 +24,15 @@ public class PaymentCommandMap extends CommandMap> { - @Autowired private PaymentRequestRepository paymentRequestRepository; + @Autowired public PaymentRequestRepository paymentRequestRepository; - @Autowired private PaymentTransactionRepository paymentTransactionRepository; + @Autowired public PaymentTransactionRepository paymentTransactionRepository; - @Autowired private WalletRepository walletRepository; + @Autowired public WalletRepository walletRepository; - @Autowired private WalletTransactionRepository walletTransactionRepository; + @Autowired public WalletTransactionRepository walletTransactionRepository; - @Autowired private RedisService redisService; + @Autowired public RedisService redisService; public void registerCommands() { /* PaymentRequest commands */ diff --git a/services/users/src/main/java/com/workup/users/ControllerMQListener.java b/services/users/src/main/java/com/workup/users/ControllerMQListener.java index 95f9c0c9..2abacd2a 100644 --- a/services/users/src/main/java/com/workup/users/ControllerMQListener.java +++ b/services/users/src/main/java/com/workup/users/ControllerMQListener.java @@ -1,35 +1,141 @@ package com.workup.users; +import com.workup.shared.commands.Command; +import com.workup.shared.commands.CommandRequest; +import com.workup.shared.commands.CommandResponse; +import com.workup.shared.commands.controller.ContinueRequest; +import com.workup.shared.commands.controller.DeleteCommandRequest; +import com.workup.shared.commands.controller.FreezeRequest; +import com.workup.shared.commands.controller.SetLoggingLevelRequest; import com.workup.shared.commands.controller.SetMaxThreadsRequest; -import java.lang.reflect.Field; +import com.workup.shared.commands.controller.UpdateCommandRequest; +import com.workup.shared.enums.ServiceQueueNames; +import com.workup.shared.enums.ThreadPoolSize; +import com.workup.users.commands.UserCommand; +import com.workup.users.commands.UserCommandMap; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +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.listener.RabbitListenerEndpointRegistry; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Service; @Service -@RabbitListener(queues = "#{controllerQueue.name}") +@RabbitListener(queues = "#{controllerQueue.name}", id = "#{controllerQueue.name}") public class ControllerMQListener { + @Autowired public UserCommandMap commandMap; @Autowired public ThreadPoolTaskExecutor taskExecutor; - @Autowired private ApplicationContext context; + @Autowired private RabbitListenerEndpointRegistry registry; + + private static final Logger logger = LogManager.getLogger(ControllerMQListener.class); @RabbitHandler public void receive(SetMaxThreadsRequest in) throws Exception { try { - ThreadPoolTaskExecutor myBean = context.getBean(ThreadPoolTaskExecutor.class); - Field maxPoolSize = ThreadPoolTaskExecutor.class.getDeclaredField("maxPoolSize"); - maxPoolSize.setAccessible(true); - maxPoolSize.set(myBean, in.getMaxThreads()); - Field corePoolSize = ThreadPoolTaskExecutor.class.getDeclaredField("corePoolSize"); - corePoolSize.setAccessible(true); - corePoolSize.set(myBean, in.getMaxThreads()); + logger.info("Max threads is: " + taskExecutor.getMaxPoolSize()); + setThreads(in.getMaxThreads()); + ThreadPoolSize.POOL_SIZE = taskExecutor.getMaxPoolSize(); + logger.info("Max threads set to: " + taskExecutor.getMaxPoolSize()); + } catch (Exception e) { + logger.info(e.getMessage()); + e.printStackTrace(); + } + } + + @RabbitHandler + public void receive(SetLoggingLevelRequest in) throws Exception { + try { + Logger logger = LogManager.getLogger("com.workup.users"); + Configurator.setAllLevels(logger.getName(), Level.valueOf(in.getLevel())); + logger.info("Logging level set to: " + in.getLevel()); + } catch (Exception e) { + logger.info(e.getMessage()); + e.printStackTrace(); + } + } + + @RabbitHandler + public void receive(FreezeRequest in) throws Exception { + try { + registry.getListenerContainer(ServiceQueueNames.USERS).stop(); + setThreads(1); + logger.info("Stopped all threads."); + } catch (Exception e) { + logger.info(e.getMessage()); + e.printStackTrace(); + } + } + + @RabbitHandler + public void receive(ContinueRequest in) throws Exception { + try { + registry.getListenerContainer(ServiceQueueNames.USERS).start(); + setThreads(ThreadPoolSize.POOL_SIZE); + logger.info("Continued all threads."); + } catch (Exception e) { + logger.info(e.getMessage()); + e.printStackTrace(); + } + } + + @SuppressWarnings("unchecked") + @RabbitHandler + public void receive(UpdateCommandRequest in) throws Exception { + try { + byte[] byteArray = in.getByteCode(); + Class clazz = + (Class) + (new MyClassLoader(this.getClass().getClassLoader()) + .loadClass(byteArray, in.getClassName())); + + commandMap.replaceCommand( + in.getCommandName(), + (Class>) + ((Command) clazz.newInstance()).getClass()); + + logger.info("Updated command: " + in.getCommandName()); + // clazz.newInstance().Run(null); + } catch (Exception e) { + logger.info(e.getMessage()); + e.printStackTrace(); + } + } + + @RabbitHandler + public void receive(DeleteCommandRequest in) throws Exception { + try { + commandMap.removeCommand(in.getCommandName()); + System.out.println("Deleted command: " + in.getCommandName()); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } + + static class MyClassLoader extends ClassLoader { + public MyClassLoader(ClassLoader classLoader) { + super(classLoader); + } + + public Class loadClass(byte[] byteCode, String className) { + return defineClass(className, byteCode, 0, byteCode.length); + } + } + + private void setThreads(int threads) throws NoSuchFieldException, IllegalAccessException { + if (threads > taskExecutor.getCorePoolSize()) { + taskExecutor.setMaxPoolSize(threads); + taskExecutor.setCorePoolSize(threads); + } else { + taskExecutor.setCorePoolSize(threads); + taskExecutor.setMaxPoolSize(threads); + } + } } diff --git a/services/users/src/main/java/com/workup/users/RabbitMQListener.java b/services/users/src/main/java/com/workup/users/RabbitMQListener.java index 1d13eb51..dd13ab0f 100644 --- a/services/users/src/main/java/com/workup/users/RabbitMQListener.java +++ b/services/users/src/main/java/com/workup/users/RabbitMQListener.java @@ -1,7 +1,10 @@ package com.workup.users; +import com.workup.shared.commands.Command; +import com.workup.shared.commands.CommandRequest; import com.workup.shared.commands.users.requests.*; import com.workup.shared.commands.users.responses.*; +import com.workup.shared.enums.HttpStatusCode; import com.workup.shared.enums.ServiceQueueNames; import com.workup.users.commands.*; import com.workup.users.commands.UserCommandMap; @@ -13,7 +16,7 @@ import org.springframework.stereotype.Service; @Service -@RabbitListener(queues = ServiceQueueNames.USERS) +@RabbitListener(queues = ServiceQueueNames.USERS, id = ServiceQueueNames.USERS) public class RabbitMQListener { @Autowired public UserCommandMap commandMap; @@ -22,266 +25,558 @@ public class RabbitMQListener { @Async public CompletableFuture receive( FreelancerGetProfileBriefRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((FreelancerGetProfileBriefCommand) commandMap.getCommand("FreelancerGetProfileBrief")) - .Run(in)); + try { + return CompletableFuture.completedFuture( + (FreelancerGetProfileBriefResponse) + ((Command) commandMap.getCommand("FreelancerGetProfileBrief")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + FreelancerGetProfileBriefResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(FreelancerGetProfileRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((FreelancerGetProfileCommand) commandMap.getCommand("FreelancerGetProfile")).Run(in)); + try { + return CompletableFuture.completedFuture( + (FreelancerGetProfileResponse) + ((Command) commandMap.getCommand("FreelancerGetProfile")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + FreelancerGetProfileResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(FreelancerGetResumeRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((FreelancerGetResumeCommand) commandMap.getCommand("FreelancerGetResume")).Run(in)); + try { + return CompletableFuture.completedFuture( + (FreelancerGetResumeResponse) + ((Command) commandMap.getCommand("FreelancerGetResume")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + FreelancerGetResumeResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(FreelancerSetPhotoRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((FreelancerSetPhotoCommand) commandMap.getCommand("FreelancerSetPhoto")).Run(in)); + try { + return CompletableFuture.completedFuture( + (FreelancerSetPhotoResponse) + ((Command) commandMap.getCommand("FreelancerSetPhoto")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + FreelancerSetPhotoResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(FreelancerSetProfileRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((FreelancerSetProfileCommand) commandMap.getCommand("FreelancerSetProfile")).Run(in)); + try { + return CompletableFuture.completedFuture( + (FreelancerSetProfileResponse) + ((Command) commandMap.getCommand("FreelancerSetProfile")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + FreelancerSetProfileResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(FreelancerSetResumeRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((FreelancerSetResumeCommand) commandMap.getCommand("FreelancerSetResume")).Run(in)); + try { + return CompletableFuture.completedFuture( + (FreelancerSetResumeResponse) + ((Command) commandMap.getCommand("FreelancerSetResume")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + FreelancerSetResumeResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(FreelancerGetPhotoRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((FreelancerGetPhotoCommand) commandMap.getCommand("FreelancerGetPhoto")).Run(in)); + try { + return CompletableFuture.completedFuture( + (FreelancerGetPhotoResponse) + ((Command) commandMap.getCommand("FreelancerGetPhoto")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + FreelancerGetPhotoResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(ClientSetProfileRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((ClientSetProfileCommand) commandMap.getCommand("ClientSetProfile")).Run(in)); + try { + return CompletableFuture.completedFuture( + (ClientSetProfileResponse) + ((Command) commandMap.getCommand("ClientSetProfile")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + ClientSetProfileResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(ClientGetProfileRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((ClientGetProfileCommand) commandMap.getCommand("ClientGetProfile")).Run(in)); + try { + return CompletableFuture.completedFuture( + (ClientGetProfileResponse) + ((Command) commandMap.getCommand("ClientGetProfile")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + ClientGetProfileResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(ClientSetPhotoRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((ClientSetPhotoCommand) commandMap.getCommand("ClientSetPhoto")).Run(in)); + try { + return CompletableFuture.completedFuture( + (ClientSetPhotoResponse) + ((Command) commandMap.getCommand("ClientSetPhoto")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + ClientSetPhotoResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(ClientGetPhotoRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((ClientGetPhotoCommand) commandMap.getCommand("ClientGetPhoto")).Run(in)); + try { + return CompletableFuture.completedFuture( + (ClientGetPhotoResponse) + ((Command) commandMap.getCommand("ClientGetPhoto")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + ClientGetPhotoResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive( AddFreelancerAchievementRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((AddFreelancerAchievementCommand) commandMap.getCommand("AddFreelancerAchievement")) - .Run(in)); + try { + return CompletableFuture.completedFuture( + (AddFreelancerAchievementResponse) + ((Command) commandMap.getCommand("AddFreelancerAchievement")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + AddFreelancerAchievementResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(AddFreelancerEducationRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((AddFreelancerEducationCommand) commandMap.getCommand("AddFreelancerEducation")).Run(in)); + try { + return CompletableFuture.completedFuture( + (AddFreelancerEducationResponse) + ((Command) commandMap.getCommand("AddFreelancerEducation")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + AddFreelancerEducationResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive( AddFreelancerExperienceRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((AddFreelancerExperienceCommand) commandMap.getCommand("AddFreelancerExperience")) - .Run(in)); + try { + return CompletableFuture.completedFuture( + (AddFreelancerExperienceResponse) + ((Command) commandMap.getCommand("AddFreelancerExperience")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + AddFreelancerExperienceResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(AddFreelancerSkillRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((AddFreelancerSkillCommand) commandMap.getCommand("AddFreelancerSkill")).Run(in)); + try { + return CompletableFuture.completedFuture( + (AddFreelancerSkillResponse) + ((Command) commandMap.getCommand("AddFreelancerSkill")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + AddFreelancerSkillResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(AddFreelancerLanguageRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((AddFreelancerLanguageCommand) commandMap.getCommand("AddFreelancerLanguage")).Run(in)); + try { + return CompletableFuture.completedFuture( + (AddFreelancerLanguageResponse) + ((Command) commandMap.getCommand("AddFreelancerLanguage")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + AddFreelancerLanguageResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive( GetFreelancerAchievementsRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((GetFreelancerAchievementsCommand) commandMap.getCommand("GetFreelancerAchievements")) - .Run(in)); + try { + return CompletableFuture.completedFuture( + (GetFreelancerAchievementsResponse) + ((Command) commandMap.getCommand("GetFreelancerAchievements")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + GetFreelancerAchievementsResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive( GetFreelancerEducationsRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((GetFreelancerEducationsCommand) commandMap.getCommand("GetFreelancerEducations")) - .Run(in)); + try { + return CompletableFuture.completedFuture( + (GetFreelancerEducationsResponse) + ((Command) commandMap.getCommand("GetFreelancerEducations")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + GetFreelancerEducationsResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive( GetFreelancerExperiencesRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((GetFreelancerExperiencesCommand) commandMap.getCommand("GetFreelancerExperiences")) - .Run(in)); + try { + return CompletableFuture.completedFuture( + (GetFreelancerExperiencesResponse) + ((Command) commandMap.getCommand("GetFreelancerExperiences")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + GetFreelancerExperiencesResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(GetFreelancerSkillsRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((GetFreelancerSkillsCommand) commandMap.getCommand("GetFreelancerSkills")).Run(in)); + try { + return CompletableFuture.completedFuture( + (GetFreelancerSkillsResponse) + ((Command) commandMap.getCommand("GetFreelancerSkills")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + GetFreelancerSkillsResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(GetFreelancerLanguagesRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((GetFreelancerLanguagesCommand) commandMap.getCommand("GetFreelancerLanguages")).Run(in)); + try { + return CompletableFuture.completedFuture( + (GetFreelancerLanguagesResponse) + ((Command) commandMap.getCommand("GetFreelancerLanguages")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + GetFreelancerLanguagesResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive( UpdateFreelancerAchievementRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((UpdateFreelancerAchievementCommand) commandMap.getCommand("UpdateFreelancerAchievement")) - .Run(in)); + try { + return CompletableFuture.completedFuture( + (UpdateFreelancerAchievementResponse) + ((Command) commandMap.getCommand("UpdateFreelancerAchievement")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + UpdateFreelancerAchievementResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive( UpdateFreelancerEducationRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((UpdateFreelancerEducationCommand) commandMap.getCommand("UpdateFreelancerEducation")) - .Run(in)); + try { + return CompletableFuture.completedFuture( + (UpdateFreelancerEducationResponse) + ((Command) commandMap.getCommand("UpdateFreelancerEducation")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + UpdateFreelancerEducationResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive( UpdateFreelancerExperienceRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((UpdateFreelancerExperienceCommand) commandMap.getCommand("UpdateFreelancerExperience")) - .Run(in)); + try { + return CompletableFuture.completedFuture( + (UpdateFreelancerExperienceResponse) + ((Command) commandMap.getCommand("UpdateFreelancerExperience")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + UpdateFreelancerExperienceResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive( RemoveFreelancerAchievementRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((RemoveFreelancerAchievementCommand) commandMap.getCommand("RemoveFreelancerAchievement")) - .Run(in)); + try { + return CompletableFuture.completedFuture( + (RemoveFreelancerAchievementResponse) + ((Command) commandMap.getCommand("RemoveFreelancerAchievement")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + RemoveFreelancerAchievementResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive( RemoveFreelancerEducationRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((RemoveFreelancerEducationCommand) commandMap.getCommand("RemoveFreelancerEducation")) - .Run(in)); + try { + return CompletableFuture.completedFuture( + (RemoveFreelancerEducationResponse) + ((Command) commandMap.getCommand("RemoveFreelancerEducation")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + RemoveFreelancerEducationResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive( RemoveFreelancerExperienceRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((RemoveFreelancerExperienceCommand) commandMap.getCommand("RemoveFreelancerExperience")) - .Run(in)); + try { + return CompletableFuture.completedFuture( + (RemoveFreelancerExperienceResponse) + ((Command) commandMap.getCommand("RemoveFreelancerExperience")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + RemoveFreelancerExperienceResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(RemoveFreelancerSkillRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((RemoveFreelancerSkillCommand) commandMap.getCommand("RemoveFreelancerSkill")).Run(in)); + try { + return CompletableFuture.completedFuture( + (RemoveFreelancerSkillResponse) + ((Command) commandMap.getCommand("RemoveFreelancerSkill")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + RemoveFreelancerSkillResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive( RemoveFreelancerLanguageRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((RemoveFreelancerLanguageCommand) commandMap.getCommand("RemoveFreelancerLanguage")) - .Run(in)); + try { + return CompletableFuture.completedFuture( + (RemoveFreelancerLanguageResponse) + ((Command) commandMap.getCommand("RemoveFreelancerLanguage")) + .Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + RemoveFreelancerLanguageResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(LoginRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((LoginCommand) commandMap.getCommand("Login")).Run(in)); + try { + return CompletableFuture.completedFuture( + (SignUpAndInResponse) + ((Command) commandMap.getCommand("Login")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + SignUpAndInResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(FreelancerRegisterRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((FreelancerRegisterCommand) commandMap.getCommand("FreelancerRegister")).Run(in)); + try { + return CompletableFuture.completedFuture( + (SignUpAndInResponse) + ((Command) commandMap.getCommand("FreelancerRegister")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + SignUpAndInResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } @RabbitHandler @Async public CompletableFuture receive(ClientRegisterRequest in) throws Exception { - return CompletableFuture.completedFuture( - ((ClientRegisterCommand) commandMap.getCommand("ClientRegister")).Run(in)); + try { + return CompletableFuture.completedFuture( + (SignUpAndInResponse) + ((Command) commandMap.getCommand("ClientRegister")).Run(in)); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + SignUpAndInResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } } diff --git a/services/users/src/main/java/com/workup/users/UsersApplication.java b/services/users/src/main/java/com/workup/users/UsersApplication.java index b88fac3a..99472558 100644 --- a/services/users/src/main/java/com/workup/users/UsersApplication.java +++ b/services/users/src/main/java/com/workup/users/UsersApplication.java @@ -2,6 +2,7 @@ import com.workup.shared.enums.ControllerQueueNames; import com.workup.shared.enums.ServiceQueueNames; +import com.workup.shared.enums.ThreadPoolSize; import org.springframework.amqp.core.AnonymousQueue; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; @@ -52,11 +53,12 @@ public Binding fanoutBinding(FanoutExchange fanout, Queue controllerQueue) { return BindingBuilder.bind(controllerQueue).to(fanout); } - @Bean + @Bean(name = "usersTaskExecutor") public ThreadPoolTaskExecutor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); - executor.setCorePoolSize(50); - executor.setMaxPoolSize(50); + executor.setCorePoolSize(ThreadPoolSize.POOL_SIZE); + executor.setMaxPoolSize(ThreadPoolSize.POOL_SIZE); + executor.setWaitForTasksToCompleteOnShutdown(true); executor.setQueueCapacity(500); executor.setThreadNamePrefix("users-"); executor.initialize(); diff --git a/services/users/src/main/java/com/workup/users/commands/UserCommand.java b/services/users/src/main/java/com/workup/users/commands/UserCommand.java index 0f7d7f5c..00cc68a4 100644 --- a/services/users/src/main/java/com/workup/users/commands/UserCommand.java +++ b/services/users/src/main/java/com/workup/users/commands/UserCommand.java @@ -19,16 +19,16 @@ public abstract class UserCommand< static final String PHOTO_BUCKET = "photos:"; static final String RESUME_BUCKET = "resume:"; - @Setter FreelancerRepository freelancerRepository; + @Setter public FreelancerRepository freelancerRepository; - @Setter ExperienceRepository experienceRepository; + @Setter public ExperienceRepository experienceRepository; - @Setter EducationRepository educationRepository; + @Setter public EducationRepository educationRepository; - @Setter AchievementRepository achievementRepository; + @Setter public AchievementRepository achievementRepository; - @Setter ClientRepository clientRepository; + @Setter public ClientRepository clientRepository; - @Setter AmqpTemplate rabbitTemplate; - @Autowired GridFsTemplate gridFsTemplate; + @Setter public AmqpTemplate rabbitTemplate; + @Autowired public GridFsTemplate gridFsTemplate; } diff --git a/services/users/src/main/java/com/workup/users/commands/UserCommandMap.java b/services/users/src/main/java/com/workup/users/commands/UserCommandMap.java index 70e4139f..e5b65e66 100644 --- a/services/users/src/main/java/com/workup/users/commands/UserCommandMap.java +++ b/services/users/src/main/java/com/workup/users/commands/UserCommandMap.java @@ -11,12 +11,12 @@ @Component public class UserCommandMap extends CommandMap> { - @Autowired FreelancerRepository freelancerRepository; - @Autowired ExperienceRepository experienceRepository; - @Autowired ClientRepository clientRepository; - @Autowired EducationRepository educationRepository; - @Autowired AchievementRepository achievementRepository; - @Autowired AmqpTemplate rabbitTemplate; + @Autowired public FreelancerRepository freelancerRepository; + @Autowired public ExperienceRepository experienceRepository; + @Autowired public ClientRepository clientRepository; + @Autowired public EducationRepository educationRepository; + @Autowired public AchievementRepository achievementRepository; + @Autowired public AmqpTemplate rabbitTemplate; public void registerCommands() { diff --git a/services/users/src/test/java/com/workup/users/PaymentMockingListener.java b/services/users/src/test/java/com/workup/users/PaymentMockingListener.java index ed973a87..225346cc 100644 --- a/services/users/src/test/java/com/workup/users/PaymentMockingListener.java +++ b/services/users/src/test/java/com/workup/users/PaymentMockingListener.java @@ -19,7 +19,15 @@ public class PaymentMockingListener { @RabbitHandler @Async public CompletableFuture receive(CreateWalletRequest in) throws Exception { - return CompletableFuture.completedFuture( - CreateWalletResponse.builder().withStatusCode(statusCodeToBeReturned).build()); + try { + return CompletableFuture.completedFuture( + CreateWalletResponse.builder().withStatusCode(statusCodeToBeReturned).build()); + } catch (Exception ex) { + return CompletableFuture.completedFuture( + CreateWalletResponse.builder() + .withErrorMessage("Not Implemented.") + .withStatusCode(HttpStatusCode.NOT_IMPLEMENTED) + .build()); + } } } diff --git a/shared/src/main/java/com/workup/shared/commands/CommandMap.java b/shared/src/main/java/com/workup/shared/commands/CommandMap.java index ece18e29..47a7a0dc 100644 --- a/shared/src/main/java/com/workup/shared/commands/CommandMap.java +++ b/shared/src/main/java/com/workup/shared/commands/CommandMap.java @@ -28,4 +28,8 @@ public R getCommand(String command) throws Exception { public void replaceCommand(String command, Class newCommand) { commands.put(command, newCommand); } + + public void removeCommand(String command) { + commands.remove(command); + } } diff --git a/shared/src/main/java/com/workup/shared/commands/controller/DeleteCommandRequest.java b/shared/src/main/java/com/workup/shared/commands/controller/DeleteCommandRequest.java index 25f42045..d91fe457 100644 --- a/shared/src/main/java/com/workup/shared/commands/controller/DeleteCommandRequest.java +++ b/shared/src/main/java/com/workup/shared/commands/controller/DeleteCommandRequest.java @@ -1,6 +1,13 @@ package com.workup.shared.commands.controller; +import lombok.Builder; +import lombok.Getter; +import lombok.extern.jackson.Jacksonized; + /** Deletes a command from the commands map. */ +@Jacksonized +@Builder +@Getter public class DeleteCommandRequest { - // TODO: Add fields + String commandName; } diff --git a/shared/src/main/java/com/workup/shared/commands/controller/UpdateCommandRequest.java b/shared/src/main/java/com/workup/shared/commands/controller/UpdateCommandRequest.java index b8c01ed0..5597353e 100644 --- a/shared/src/main/java/com/workup/shared/commands/controller/UpdateCommandRequest.java +++ b/shared/src/main/java/com/workup/shared/commands/controller/UpdateCommandRequest.java @@ -10,5 +10,6 @@ @Jacksonized public class UpdateCommandRequest { String commandName; + String className; byte[] byteCode; }