-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Elshimaa Betah <79271594+ShimaaBetah@users.noreply.github.com> Co-authored-by: Ziad Othman <ziadsadek999@gmail.com>
- Loading branch information
1 parent
d86d13f
commit 2372d42
Showing
19 changed files
with
436 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 114 additions & 11 deletions
125
services/contracts/src/main/java/com/workup/contracts/ControllerMQListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
String className = commandMap.getCommand(in.getCommandName()).getClass().getName(); | ||
byte[] byteArray = in.getByteCode(); | ||
Class<?> clazz = | ||
(Class<?>) | ||
(new MyClassLoader(this.getClass().getClassLoader()).loadClass(byteArray, className)); | ||
|
||
commandMap.replaceCommand( | ||
in.getCommandName(), | ||
(Class<? extends ContractCommand<? extends CommandRequest, ? extends CommandResponse>>) | ||
((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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.