Skip to content

Commit c5062e0

Browse files
Ahmad45123ziadsadek999ShimaaBetah
authored
Added DeleteComnmand as well as tested & fixed what we found. (#104)
Co-authored-by: Ziad Othman <ziadsadek999@gmail.com> Co-authored-by: Elshimaa Betah <79271594+ShimaaBetah@users.noreply.github.com>
1 parent 9fa0f03 commit c5062e0

File tree

24 files changed

+1198
-346
lines changed

24 files changed

+1198
-346
lines changed

controller/pom.xml

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,41 @@
3333
<version>${project.version}</version>
3434
<exclusions>
3535
<exclusion>
36-
<groupId>org.testcontainers</groupId>
37-
<artifactId>cassandra</artifactId>
36+
<groupId>*</groupId>
37+
<artifactId>*</artifactId>
3838
</exclusion>
39+
</exclusions>
40+
</dependency>
41+
<dependency>
42+
<groupId>com.workup</groupId>
43+
<artifactId>contracts</artifactId>
44+
<version>${project.version}</version>
45+
<exclusions>
3946
<exclusion>
40-
<groupId>org.springframework.boot</groupId>
41-
<artifactId>spring-boot-starter-data-cassandra</artifactId>
47+
<groupId>*</groupId>
48+
<artifactId>*</artifactId>
49+
</exclusion>
50+
</exclusions>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.workup</groupId>
54+
<artifactId>payments</artifactId>
55+
<version>${project.version}</version>
56+
<exclusions>
57+
<exclusion>
58+
<groupId>*</groupId>
59+
<artifactId>*</artifactId>
4260
</exclusion>
61+
</exclusions>
62+
</dependency>
63+
<dependency>
64+
<groupId>com.workup</groupId>
65+
<artifactId>users</artifactId>
66+
<version>${project.version}</version>
67+
<exclusions>
4368
<exclusion>
44-
<groupId>org.springframework.boot</groupId>
45-
<artifactId>spring-boot-starter-web</artifactId>
69+
<groupId>*</groupId>
70+
<artifactId>*</artifactId>
4671
</exclusion>
4772
</exclusions>
4873
</dependency>

controller/src/main/java/com/workup/controller/CLIHandler.java

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import asg.cliche.CLIException;
44
import asg.cliche.Command;
55
import com.workup.shared.commands.controller.*;
6-
import com.workup.shared.commands.jobs.requests.CreateJobRequest;
76
import com.workup.shared.enums.ControllerQueueNames;
8-
import com.workup.shared.enums.ServiceQueueNames;
97
import java.io.IOException;
108
import java.util.HashMap;
119
import java.util.Map;
@@ -57,6 +55,11 @@ public String maxdb(String app, int maxDBConn) {
5755
return "Command Sent!";
5856
}
5957

58+
@Command(description = "Adds a new command")
59+
public String addCommand(String app, String commandName, String className) throws Exception {
60+
return updateCommand(app, commandName, className);
61+
}
62+
6063
@Command(description = "starts a specific app")
6164
public String start(String app) {
6265
app = app.toLowerCase();
@@ -78,12 +81,7 @@ public String freeze(String app) {
7881
return "Command sent";
7982
}
8083

81-
@Command(description = "stops a specific app")
82-
public String setmq(String app, int appNum) {
83-
return "setmq";
84-
}
85-
86-
@Command(description = "stops a specific app")
84+
@Command(description = "sets a logging level")
8785
public String setLoggingLevel(String app, String level) {
8886
app = app.toLowerCase();
8987
if (!appQueueMap.containsKey(app)) {
@@ -96,17 +94,6 @@ public String setLoggingLevel(String app, String level) {
9694
return "Command sent!!";
9795
}
9896

99-
@Command(description = "test")
100-
public void test() {
101-
CreateJobRequest request = CreateJobRequest.builder().withTitle("Ziko").build();
102-
rabbitTemplate.convertSendAndReceive(ServiceQueueNames.JOBS, request);
103-
}
104-
105-
@Command(description = "Creates a new command")
106-
public String addcommand(String app, String commandName, String className) {
107-
return "Add command";
108-
}
109-
11097
@Command(description = "Updates an existing command")
11198
public String updateCommand(String app, String commandName, String className) throws Exception {
11299
app = app.toLowerCase();
@@ -120,6 +107,7 @@ public String updateCommand(String app, String commandName, String className) th
120107
"",
121108
UpdateCommandRequest.builder()
122109
.withCommandName(commandName)
110+
.withClassName(className)
123111
.withByteCode(byteArray)
124112
.build());
125113
} catch (Exception ex) {
@@ -142,12 +130,13 @@ private byte[] getByteCode(String commandName, String className)
142130
}
143131

144132
@Command(description = "Deletes an existing command")
145-
public String deletecommand(String app, String commandName, String className) {
146-
return "Delete command";
147-
}
148-
149-
@Command(description = "stops a specific app")
150-
public String updateClass(String app, int appNum) {
151-
return "update class";
133+
public String deleteCommand(String app, String commandName) {
134+
app = app.toLowerCase();
135+
if (!appQueueMap.containsKey(app)) {
136+
return "Error: app can only be jobs, users, contracts or payments!";
137+
}
138+
rabbitTemplate.convertAndSend(
139+
appQueueMap.get(app), "", DeleteCommandRequest.builder().commandName(commandName).build());
140+
return "Command sent";
152141
}
153142
}

services/contracts/src/main/java/com/workup/contracts/ContractsApplication.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.workup.shared.enums.ControllerQueueNames;
44
import com.workup.shared.enums.ServiceQueueNames;
5+
import com.workup.shared.enums.ThreadPoolSize;
56
import org.springframework.amqp.core.AmqpTemplate;
67
import org.springframework.amqp.core.AnonymousQueue;
78
import org.springframework.amqp.core.Binding;
@@ -79,8 +80,9 @@ public Binding fanoutBinding(FanoutExchange fanout, Queue controllerQueue) {
7980
@Bean
8081
public ThreadPoolTaskExecutor taskExecutor() {
8182
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
82-
executor.setCorePoolSize(50);
83-
executor.setMaxPoolSize(50);
83+
executor.setCorePoolSize(ThreadPoolSize.POOL_SIZE);
84+
executor.setMaxPoolSize(ThreadPoolSize.POOL_SIZE);
85+
executor.setWaitForTasksToCompleteOnShutdown(true);
8486
executor.setQueueCapacity(500);
8587
executor.setThreadNamePrefix("contracts-");
8688
executor.initialize();
Lines changed: 114 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,138 @@
11
package com.workup.contracts;
22

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;
312
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;
520
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
621
import org.springframework.amqp.rabbit.annotation.RabbitListener;
22+
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
723
import org.springframework.beans.factory.annotation.Autowired;
824
import org.springframework.context.ApplicationContext;
925
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
1026
import org.springframework.stereotype.Service;
1127

1228
@Service
13-
@RabbitListener(queues = "#{controllerQueue.name}")
29+
@RabbitListener(queues = "#{controllerQueue.name}", id = "#{controllerQueue.name}")
1430
public class ControllerMQListener {
15-
31+
@Autowired public ContractCommandMap commandMap;
1632
@Autowired public ThreadPoolTaskExecutor taskExecutor;
17-
1833
@Autowired private ApplicationContext context;
34+
@Autowired private RabbitListenerEndpointRegistry registry;
1935

2036
@RabbitHandler
2137
public void receive(SetMaxThreadsRequest in) throws Exception {
2238
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.");
3079
} catch (Exception e) {
3180
System.out.println(e.getMessage());
3281
e.printStackTrace();
3382
}
3483
}
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+
}
35138
}

0 commit comments

Comments
 (0)