|
1 | 1 | package com.workup.contracts;
|
2 | 2 |
|
| 3 | +import com.workup.contracts.commands.ContractCommand; |
| 4 | +import com.workup.contracts.commands.ContractCommandMap; |
| 5 | +import com.workup.shared.commands.Command; |
| 6 | +import com.workup.shared.commands.CommandRequest; |
| 7 | +import com.workup.shared.commands.CommandResponse; |
| 8 | +import com.workup.shared.commands.controller.ContinueRequest; |
| 9 | +import com.workup.shared.commands.controller.DeleteCommandRequest; |
| 10 | +import com.workup.shared.commands.controller.FreezeRequest; |
| 11 | +import com.workup.shared.commands.controller.SetLoggingLevelRequest; |
3 | 12 | import com.workup.shared.commands.controller.SetMaxThreadsRequest;
|
4 |
| -import java.lang.reflect.Field; |
| 13 | +import com.workup.shared.commands.controller.UpdateCommandRequest; |
| 14 | +import com.workup.shared.enums.ServiceQueueNames; |
| 15 | +import com.workup.shared.enums.ThreadPoolSize; |
| 16 | +import org.apache.logging.log4j.Level; |
| 17 | +import org.apache.logging.log4j.LogManager; |
| 18 | +import org.apache.logging.log4j.Logger; |
| 19 | +import org.apache.logging.log4j.core.config.Configurator; |
5 | 20 | import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
6 | 21 | import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
| 22 | +import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry; |
7 | 23 | import org.springframework.beans.factory.annotation.Autowired;
|
8 | 24 | import org.springframework.context.ApplicationContext;
|
9 | 25 | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
10 | 26 | import org.springframework.stereotype.Service;
|
11 | 27 |
|
12 | 28 | @Service
|
13 |
| -@RabbitListener(queues = "#{controllerQueue.name}") |
| 29 | +@RabbitListener(queues = "#{controllerQueue.name}", id = "#{controllerQueue.name}") |
14 | 30 | public class ControllerMQListener {
|
15 |
| - |
| 31 | + @Autowired public ContractCommandMap commandMap; |
16 | 32 | @Autowired public ThreadPoolTaskExecutor taskExecutor;
|
17 |
| - |
18 | 33 | @Autowired private ApplicationContext context;
|
| 34 | + @Autowired private RabbitListenerEndpointRegistry registry; |
19 | 35 |
|
20 | 36 | @RabbitHandler
|
21 | 37 | public void receive(SetMaxThreadsRequest in) throws Exception {
|
22 | 38 | try {
|
23 |
| - ThreadPoolTaskExecutor myBean = context.getBean(ThreadPoolTaskExecutor.class); |
24 |
| - Field maxPoolSize = ThreadPoolTaskExecutor.class.getDeclaredField("maxPoolSize"); |
25 |
| - maxPoolSize.setAccessible(true); |
26 |
| - maxPoolSize.set(myBean, in.getMaxThreads()); |
27 |
| - Field corePoolSize = ThreadPoolTaskExecutor.class.getDeclaredField("corePoolSize"); |
28 |
| - corePoolSize.setAccessible(true); |
29 |
| - corePoolSize.set(myBean, in.getMaxThreads()); |
| 39 | + System.out.println("Max threads is: " + taskExecutor.getMaxPoolSize()); |
| 40 | + setThreads(in.getMaxThreads()); |
| 41 | + ThreadPoolSize.POOL_SIZE = taskExecutor.getMaxPoolSize(); |
| 42 | + System.out.println("Max threads set to: " + taskExecutor.getMaxPoolSize()); |
| 43 | + } catch (Exception e) { |
| 44 | + System.out.println(e.getMessage()); |
| 45 | + e.printStackTrace(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @RabbitHandler |
| 50 | + public void receive(SetLoggingLevelRequest in) throws Exception { |
| 51 | + try { |
| 52 | + Logger logger = LogManager.getLogger("com.workup.contracts"); |
| 53 | + Configurator.setAllLevels(logger.getName(), Level.valueOf(in.getLevel())); |
| 54 | + System.out.println("Logging level set to: " + in.getLevel()); |
| 55 | + } catch (Exception e) { |
| 56 | + System.out.println(e.getMessage()); |
| 57 | + e.printStackTrace(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @RabbitHandler |
| 62 | + public void receive(FreezeRequest in) throws Exception { |
| 63 | + try { |
| 64 | + registry.getListenerContainer(ServiceQueueNames.CONTRACTS).stop(); |
| 65 | + setThreads(1); |
| 66 | + System.out.println("Stopped all threads."); |
| 67 | + } catch (Exception e) { |
| 68 | + System.out.println(e.getMessage()); |
| 69 | + e.printStackTrace(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @RabbitHandler |
| 74 | + public void receive(ContinueRequest in) throws Exception { |
| 75 | + try { |
| 76 | + registry.getListenerContainer(ServiceQueueNames.CONTRACTS).start(); |
| 77 | + setThreads(ThreadPoolSize.POOL_SIZE); |
| 78 | + System.out.println("Continued all threads."); |
30 | 79 | } catch (Exception e) {
|
31 | 80 | System.out.println(e.getMessage());
|
32 | 81 | e.printStackTrace();
|
33 | 82 | }
|
34 | 83 | }
|
| 84 | + |
| 85 | + @SuppressWarnings("unchecked") |
| 86 | + @RabbitHandler |
| 87 | + public void receive(UpdateCommandRequest in) throws Exception { |
| 88 | + try { |
| 89 | + byte[] byteArray = in.getByteCode(); |
| 90 | + Class<?> clazz = |
| 91 | + (Class<?>) |
| 92 | + (new MyClassLoader(this.getClass().getClassLoader()) |
| 93 | + .loadClass(byteArray, in.getClassName())); |
| 94 | + |
| 95 | + commandMap.replaceCommand( |
| 96 | + in.getCommandName(), |
| 97 | + (Class<? extends ContractCommand<? extends CommandRequest, ? extends CommandResponse>>) |
| 98 | + ((Command<?, ?>) clazz.newInstance()).getClass()); |
| 99 | + |
| 100 | + System.out.println("Updated command: " + in.getCommandName()); |
| 101 | + // clazz.newInstance().Run(null); |
| 102 | + } catch (Exception e) { |
| 103 | + System.out.println(e.getMessage()); |
| 104 | + e.printStackTrace(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @RabbitHandler |
| 109 | + public void receive(DeleteCommandRequest in) throws Exception { |
| 110 | + try { |
| 111 | + commandMap.removeCommand(in.getCommandName()); |
| 112 | + System.out.println("Deleted command: " + in.getCommandName()); |
| 113 | + } catch (Exception e) { |
| 114 | + System.out.println(e.getMessage()); |
| 115 | + e.printStackTrace(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + static class MyClassLoader extends ClassLoader { |
| 120 | + public MyClassLoader(ClassLoader classLoader) { |
| 121 | + super(classLoader); |
| 122 | + } |
| 123 | + |
| 124 | + public Class<?> loadClass(byte[] byteCode, String className) { |
| 125 | + return defineClass(className, byteCode, 0, byteCode.length); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private void setThreads(int threads) throws NoSuchFieldException, IllegalAccessException { |
| 130 | + if (threads > taskExecutor.getCorePoolSize()) { |
| 131 | + taskExecutor.setMaxPoolSize(threads); |
| 132 | + taskExecutor.setCorePoolSize(threads); |
| 133 | + } else { |
| 134 | + taskExecutor.setCorePoolSize(threads); |
| 135 | + taskExecutor.setMaxPoolSize(threads); |
| 136 | + } |
| 137 | + } |
35 | 138 | }
|
0 commit comments