Skip to content
This repository was archived by the owner on May 12, 2025. It is now read-only.

Commit 6d3ffa4

Browse files
committed
Add TasksCommand, rework system commands
1 parent cb6f0f1 commit 6d3ffa4

File tree

25 files changed

+682
-306
lines changed

25 files changed

+682
-306
lines changed

README.md

Lines changed: 99 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,11 @@ For more information please visit `spring shell` [website](https://projects.spri
1111
or [2.0.1 reference documentation](https://docs.spring.io/spring-shell/docs/2.0.1.RELEASE/reference/htmlsingle/).
1212

1313
* [Getting started](#getting-started)
14-
* [Actuator commands](#actuator-commands)
14+
* [Commands](#commands)
1515
* [Post processors](#post-processors)
16-
* [Save](#save)
17-
* [Pretty](#pretty)
18-
* [Json](#json)
19-
* [Grep](#grep)
20-
* [Highlight](#highlight)
21-
* [Custom](#custom)
2216
* [Parameter providers](#parameter-providers)
23-
* [Enum](#enum)
24-
* [File](#file)
25-
* [Custom](#custom-values)
2617
* [Custom authentication](#custom-authentication)
2718
* [Command helper](#command-helper)
28-
* [Print output](#print-output)
29-
* [Read input](#read-input)
30-
* [Table](#table)
31-
* [Confirmation](#confirmation)
3219
* [Banner](#banner)
3320
* [Listeners](#listeners)
3421
* [Session Manager](#session-manager)
@@ -103,7 +90,7 @@ ssh:
10390
restricted: true
10491
authorized-roles:
10592
- ADMIN
106-
jvm:
93+
system:
10794
enable: true
10895
restricted: true
10996
authorized-roles:
@@ -119,13 +106,14 @@ ssh:
119106
postprocessors:
120107
enable: true
121108
restricted: false
122-
thread:
123-
enable: true
109+
# since 1.3.0, command which allows you to list ssh sessions, and stop them
110+
manage-sessions:
111+
enable: false
124112
restricted: true
125113
authorized-roles:
126114
- ADMIN
127-
# since 1.3.0, command which allows you to list ssh sessions, and stop them
128-
manage-sessions:
115+
# since 1.5.0
116+
tasks:
129117
enable: false
130118
restricted: true
131119
authorized-roles:
@@ -214,12 +202,37 @@ public class TestCommands {
214202
}
215203
```
216204

217-
## Actuator commands
205+
## Commands
206+
207+
All commands group can be deactivated by enable property :
208+
209+
```yaml
210+
ssh:
211+
shell:
212+
commands:
213+
<command>:
214+
enable: true
215+
```
216+
217+
Sub commands in group can be also filtered by includes and excludes properties :
218+
219+
```yaml
220+
ssh:
221+
shell:
222+
commands:
223+
<command>:
224+
includes:
225+
- xxx
226+
excludes:
227+
- xxx
228+
```
229+
230+
### Actuator
218231

219232
If `org.springframework.boot:spring-boot-starter-actuator` dependency is present, actuator commands
220233
will be available.
221234

222-
Command availability is binded to endpoint activation.
235+
Command availability is also bind to endpoint activation.
223236

224237
```yaml
225238
management:
@@ -228,16 +241,60 @@ management:
228241
enabled: false
229242
```
230243

231-
It can also be deactivated by putting command name is exclusion list.
244+
### Tasks
232245

233-
```yaml
234-
ssh:
235-
shell:
236-
actuator:
237-
excludes:
238-
- audit
239-
- ...
240-
```
246+
Activated by default if you have ``@EnableScheduling``,
247+
these commands allow you to interact with spring boot scheduled tasks :
248+
249+
* `tasks-list` : List scheduled tasks
250+
* `tasks-stop` : Stop one or all scheduled tasks
251+
* `tasks-restart` : Restart one or all scheduled tasks
252+
253+
#### Task scheduler
254+
255+
Based on spring documentation ``org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.setScheduler``
256+
the task scheduler used for scheduled tasks will be :
257+
258+
If not specified, it will look for unique bean of type ``TaskScheduler``, or with name
259+
``taskScheduler``. Otherwise a local single-threaded will be created.
260+
261+
The ``TasksCommand`` keep the same mechanism in order to be able to restart stopped scheduled tasks.
262+
It also provides a ``setTaskScheduler()`` in case you want to specify custom one.
263+
264+
##### Examples
265+
266+
| Context | Task scheduler used in TaskCommand
267+
| --- | ---
268+
| No ``TaskScheduler`` bean in context | Local single-threaded
269+
| One ``TaskScheduler`` bean named **ts** in context | **ts** bean
270+
| Multiple ``TaskScheduler`` beans named **ts1**, **ts2** in context | Local single-threaded (could not find name **taskScheduler**)
271+
| Multiple ``TaskScheduler`` beans named **taskScheduler**, **ts2**, **ts3** in context | **taskScheduler** bean
272+
| Task scheduler specified in method ``SchedulingConfigurer#configureTasks`` | Local single-threaded (not set in task)
273+
| Task scheduler specified in method ``SchedulingConfigurer#configureTasks`` **AND** ``com.github.fonimus.ssh.shell.commands.TasksCommand.setTaskScheduler`` | Scheduler manually set
274+
275+
276+
### Jmx
277+
278+
* `jmx-info`: Displays information about jmx mbean. Use -a option to query attribute values.
279+
* `jmx-invoke`: Invoke operation on object name.
280+
* `jmx-list`: List jmx mbeans.
281+
282+
### System
283+
284+
* `system-env`: List system environment.
285+
* `system-properties`: List system properties.
286+
* `system-threads`: List jvm threads.
287+
288+
### Datasource
289+
290+
* `datasource-list`: List available datasources
291+
* `datasource-properties`: Datasource properties command. Executes 'show variables'
292+
* `datasource-query`: Datasource query command.
293+
* `datasource-update`: Datasource update command.
294+
295+
### Post processors
296+
297+
* `postprocessors`: Display the available post processors
241298

242299
## Post processors
243300

@@ -654,7 +711,7 @@ public String myCommand() {
654711

655712
### Manage sessions commands
656713

657-
If activated `ssh.shell.default-commands.manage-sessions=true`, the following commands are available :
714+
If activated `ssh.shell.commands.manage-sessions.enable=true`, the following commands are available :
658715

659716
* `manage-sessions-info`: Displays information about single session
660717
* `manage-sessions-list`: Displays active sessions
@@ -684,6 +741,17 @@ public class ApplicationTest {}
684741

685742
## Release notes
686743

744+
### 1.5.0
745+
746+
* Add tasks commands :
747+
* ``tasks-list``
748+
* ``tasks-stop``
749+
* ``tasks-restart``
750+
* Rename commands :
751+
* ``system-env`` instead of _jvm-env_
752+
* ``system-properties`` instead of _jvm-properties_
753+
* ``system-threads`` instead of _threads_
754+
687755
### 1.4.2
688756

689757
* Rework commands properties (check [configuration chapter](#configuration)) to add the following :

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
<groupId>com.github.fonimus</groupId>
2222
<artifactId>ssh-shell-spring-boot-parent</artifactId>
23-
<version>1.4.3-SNAPSHOT</version>
23+
<version>1.5.0-SNAPSHOT</version>
2424

2525
<packaging>pom</packaging>
2626

samples/basic/README.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@
3030
script: Read and execute commands from a file.
3131
stacktrace: Display the full stacktrace of the last error.
3232
33-
Datasource Commands
34-
datasource-list: List available datasources
35-
datasource-properties: Datasource properties command. Executes 'show variables'
36-
datasource-query: Datasource query command.
37-
datasource-update: Datasource update command.
38-
3933
Demo Command
4034
authentication: Authentication command
4135
echo: Echo command
@@ -54,9 +48,9 @@
5448
* manage-sessions-stop: Stop session
5549
5650
System Commands
57-
jvm-env: List system environment.
58-
jvm-properties: List system properties.
59-
threads: Thread command.
51+
system-env: List system environment.
52+
system-properties: List system properties.
53+
system-threads: Thread command.
6054
6155
Commands marked with (*) are currently unavailable.
6256
Type `help <command>` to learn more.

samples/basic/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<parent>
2020
<artifactId>ssh-shell-spring-boot-parent</artifactId>
2121
<groupId>com.github.fonimus</groupId>
22-
<version>1.4.3-SNAPSHOT</version>
22+
<version>1.5.0-SNAPSHOT</version>
2323
<relativePath>../../pom.xml</relativePath>
2424
</parent>
2525
<modelVersion>4.0.0</modelVersion>

samples/complete/README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
_ _ _ _
2121
_____| |_ __| |_ ___| | |
2222
(_-<_-< ' \ (_-< ' \/ -_) | |
23-
/__/__/_||_| /__/_||_\___|_|_| v1.0.1-SNAPSHOT
23+
/__/__/_||_| /__/_||_\___|_|_| v1.5.0-SNAPSHOT
2424
2525
2626
Please type `help` to see available commands
2727
complete::>help
2828
AVAILABLE COMMANDS
2929
3030
Actuator Commands
31-
audit: Display audit endpoint.
31+
* audit: Display audit endpoint.
3232
beans: Display beans endpoint.
3333
conditions: Display conditions endpoint.
3434
configprops: Display configprops endpoint.
@@ -42,7 +42,7 @@
4242
scheduledtasks: Display scheduledtasks endpoint.
4343
* sessions: Display sessions endpoint.
4444
shutdown: Shutdown application.
45-
threaddump: Display threaddump endpoint.
45+
* threaddump: Display threaddump endpoint.
4646
4747
Built-In Commands
4848
clear: Clear the shell screen.
@@ -84,9 +84,14 @@
8484
manage-sessions-stop: Stop session
8585
8686
System Commands
87-
jvm-env: List system environment.
88-
jvm-properties: List system properties.
89-
threads: Thread command.
87+
system-env: List system environment.
88+
system-properties: List system properties.
89+
system-threads: Thread command.
90+
91+
Tasks Commands
92+
tasks-list: Display the available scheduled tasks
93+
tasks-restart: Restart all or specified task(s)
94+
tasks-stop: Stop all or specified task(s)
9095
9196
Commands marked with (*) are currently unavailable.
9297
Type `help <command>` to learn more.

samples/complete/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<parent>
2020
<artifactId>ssh-shell-spring-boot-parent</artifactId>
2121
<groupId>com.github.fonimus</groupId>
22-
<version>1.4.3-SNAPSHOT</version>
22+
<version>1.5.0-SNAPSHOT</version>
2323
<relativePath>../../pom.xml</relativePath>
2424
</parent>
2525
<modelVersion>4.0.0</modelVersion>

samples/complete/src/main/java/com/github/fonimus/ssh/shell/complete/DemoCommand.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,24 @@ public String displaySshSession() {
272272
* For scheduled command example
273273
*/
274274
@Scheduled(initialDelay = 0, fixedDelay = 60000)
275-
public void log() {
276-
LOGGER.info("In scheduled task..");
275+
public void logWithDelay() {
276+
LOGGER.info("In 'fixed-delay' scheduled task..");
277+
}
278+
279+
/**
280+
* For scheduled command example
281+
*/
282+
@Scheduled(initialDelay = 0, fixedRate = 60000)
283+
public void logWithRate() {
284+
LOGGER.info("In 'fixed-rate' scheduled task..");
285+
}
286+
287+
/**
288+
* For scheduled command example
289+
*/
290+
@Scheduled(cron = "0/60 * * * * *")
291+
public void logWithCron() {
292+
LOGGER.info("In 'cron' scheduled task..");
277293
}
278294
}
279295

samples/complete/src/main/java/com/github/fonimus/ssh/shell/complete/DemoConfiguration.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.github.fonimus.ssh.shell.complete;
1818

19+
import com.github.fonimus.ssh.shell.commands.TasksCommand;
1920
import com.github.fonimus.ssh.shell.listeners.SshShellListener;
2021
import com.github.fonimus.ssh.shell.postprocess.PostProcessor;
2122
import lombok.extern.slf4j.Slf4j;
@@ -24,6 +25,10 @@
2425
import org.springframework.context.annotation.Bean;
2526
import org.springframework.context.annotation.Configuration;
2627
import org.springframework.context.annotation.Primary;
28+
import org.springframework.scheduling.TaskScheduler;
29+
import org.springframework.scheduling.annotation.SchedulingConfigurer;
30+
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
31+
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
2732

2833
import javax.sql.DataSource;
2934
import java.util.List;
@@ -33,7 +38,13 @@
3338
*/
3439
@Slf4j
3540
@Configuration
36-
public class DemoConfiguration {
41+
public class DemoConfiguration implements SchedulingConfigurer {
42+
43+
private final TasksCommand tasksCommand;
44+
45+
public DemoConfiguration(TasksCommand tasksCommand) {
46+
this.tasksCommand = tasksCommand;
47+
}
3748

3849
@Bean
3950
public PostProcessor<String> quotePostProcessor() {
@@ -84,4 +95,34 @@ public DataSourceProperties secondDsProps() {
8495
public DataSource secondDataSource() {
8596
return secondDsProps().initializeDataSourceBuilder().build();
8697
}
98+
99+
@Bean
100+
public TaskScheduler taskScheduler() {
101+
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
102+
threadPoolTaskScheduler.setPoolSize(5);
103+
threadPoolTaskScheduler.setThreadNamePrefix("my-pool-");
104+
return threadPoolTaskScheduler;
105+
}
106+
107+
@Bean
108+
public TaskScheduler threadPoolTaskExecutor2() {
109+
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
110+
threadPoolTaskScheduler.setPoolSize(6);
111+
threadPoolTaskScheduler.setThreadNamePrefix("my-pool2-");
112+
return threadPoolTaskScheduler;
113+
}
114+
115+
@Override
116+
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
117+
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
118+
119+
scheduler.setPoolSize(10);
120+
scheduler.setThreadNamePrefix("my-scheduled-task-pool-");
121+
scheduler.initialize();
122+
123+
taskRegistrar.setTaskScheduler(scheduler);
124+
tasksCommand.setTaskScheduler(scheduler);
125+
126+
taskRegistrar.addCronTask(() -> LOGGER.info("In 'cron' scheduled task (registrar).."), "0/60 * * * * *");
127+
}
87128
}

samples/complete/src/test/java/com/github/fonimus/ssh/shell/complete/DemoCommandTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ void testCommandEx() {
8686

8787
@Test
8888
void testLog() {
89-
cmd.log();
89+
cmd.logWithCron();
90+
cmd.logWithDelay();
9091
}
9192

9293
@Test

0 commit comments

Comments
 (0)