1
1
package com .redislabs .riot ;
2
2
3
3
import java .net .InetAddress ;
4
- import java .text .NumberFormat ;
5
4
import java .util .List ;
6
5
import java .util .Locale ;
7
6
8
- import org .springframework .batch .core .ExitStatus ;
9
- import org .springframework .batch .core .Job ;
10
- import org .springframework .batch .core .JobExecution ;
11
- import org .springframework .batch .core .JobParameters ;
12
- import org .springframework .batch .core .configuration .annotation .JobBuilderFactory ;
13
- import org .springframework .batch .core .configuration .annotation .StepBuilderFactory ;
14
- import org .springframework .batch .core .launch .support .SimpleJobLauncher ;
15
- import org .springframework .batch .core .repository .JobRepository ;
16
- import org .springframework .batch .core .repository .support .MapJobRepositoryFactoryBean ;
17
- import org .springframework .batch .item .ItemProcessor ;
18
- import org .springframework .batch .item .ItemStreamWriter ;
19
- import org .springframework .batch .item .support .AbstractItemCountingItemStreamItemReader ;
20
- import org .springframework .batch .support .transaction .ResourcelessTransactionManager ;
21
7
import org .springframework .boot .CommandLineRunner ;
22
8
import org .springframework .boot .SpringApplication ;
23
9
import org .springframework .boot .autoconfigure .SpringBootApplication ;
24
- import org .springframework .core .task .SyncTaskExecutor ;
25
- import org .springframework .transaction .PlatformTransactionManager ;
26
10
27
- import com .redislabs .riot .batch .JobBuilder ;
28
- import com .redislabs .riot .cli .BaseCommand ;
29
- import com .redislabs .riot .cli .ImportCommand ;
11
+ import com .redislabs .riot .cli .Export ;
12
+ import com .redislabs .riot .cli .Import ;
30
13
import com .redislabs .riot .redis .RedisConnectionBuilder ;
31
14
32
15
import io .lettuce .core .RedisURI ;
33
16
import lombok .Getter ;
34
17
import picocli .CommandLine ;
35
18
import picocli .CommandLine .Command ;
36
19
import picocli .CommandLine .DefaultExceptionHandler ;
37
- import picocli .CommandLine .HelpCommand ;
38
20
import picocli .CommandLine .Option ;
39
21
import picocli .CommandLine .RunLast ;
40
22
import redis .clients .jedis .Protocol ;
41
23
42
24
@ SpringBootApplication
43
- @ Command (name = "riot" , subcommands = { HelpCommand .class , ImportCommand .class })
25
+ @ Command (name = "riot" , subcommands = { Export .class , Import .class })
44
26
public class RiotApplication extends BaseCommand implements CommandLineRunner {
45
27
28
+ public static void main (String [] args ) {
29
+ SpringApplication .run (RiotApplication .class , args );
30
+ }
31
+
46
32
public static final String DEFAULT_HOST = "localhost" ;
47
33
48
34
public enum RedisDriver {
@@ -54,34 +40,26 @@ public enum RedisDriver {
54
40
*/
55
41
@ Option (names = "--spring.output.ansi.enabled" , hidden = true )
56
42
private String ansiEnabled ;
57
- @ Option (names = "--max" , description = "Maximum number of items to read." , paramLabel = "<count>" )
58
- private Integer maxCount ;
59
- @ Option (names = "--threads" , description = "Number of partitions to use for processing. (default: ${DEFAULT-VALUE})." )
60
- private int threads = 1 ;
61
- @ Option (names = "--chunk-size" , description = "The chunk size commit interval. (default: ${DEFAULT-VALUE})." )
62
- private int chunkSize = JobBuilder .DEFAULT_CHUNK_SIZE ;
63
- @ Option (names = "--sleep" , description = "Sleep duration in milliseconds between each read." )
64
- private Long sleep ;
43
+
44
+ @ Option (names = "--driver" , description = "Redis driver: ${COMPLETION-CANDIDATES}. (default: ${DEFAULT-VALUE})" )
45
+ @ Getter
46
+ private RedisDriver driver = RedisDriver .Jedis ;
65
47
@ Option (names = "--host" , description = "Redis server host. (default: localhost)." )
66
48
private InetAddress host ;
67
- @ Getter
68
49
@ Option (names = "--port" , description = "Redis server port. (default: ${DEFAULT-VALUE})." )
69
50
private int port = RedisURI .DEFAULT_REDIS_PORT ;
70
51
@ Option (names = "--command-timeout" , description = "Redis command timeout in seconds for synchronous command execution (default: ${DEFAULT-VALUE})." )
71
52
private long commandTimeout = RedisURI .DEFAULT_TIMEOUT ;
72
- @ Getter
73
53
@ Option (names = "--connection-timeout" , description = "Redis connect timeout in milliseconds. (default: ${DEFAULT-VALUE})." )
74
54
private int connectionTimeout = Protocol .DEFAULT_TIMEOUT ;
75
55
@ Option (names = "--socket-timeout" , description = "Redis socket timeout in milliseconds. (default: ${DEFAULT-VALUE})." )
76
56
private int socketTimeout = Protocol .DEFAULT_TIMEOUT ;
77
- @ Getter
78
57
@ Option (names = "--password" , description = "Redis database password." , interactive = true )
79
- protected String password ;
58
+ private String password ;
80
59
@ Option (names = "--max-idle" , description = "Maximum number of idle connections in the pool. Use a negative value to indicate an unlimited number of idle connections. (default: ${DEFAULT-VALUE})." )
81
60
private int maxIdle = 8 ;
82
61
@ Option (names = "--min-idle" , description = "Target for the minimum number of idle connections to maintain in the pool. This setting only has an effect if it is positive. (default: ${DEFAULT-VALUE})." )
83
62
private int minIdle = 0 ;
84
- @ Getter
85
63
@ Option (names = "--max-total" , description = "Maximum number of connections that can be allocated by the pool at a given time. Use a negative value for no limit. (default: ${DEFAULT-VALUE})" )
86
64
private int maxTotal = 8 ;
87
65
@ Option (names = "--max-wait" , description = "Maximum amount of time in milliseconds a connection allocation should block before throwing an exception when the pool is exhausted. Use a negative value to block indefinitely (default)." )
@@ -90,25 +68,6 @@ public enum RedisDriver {
90
68
private int database = 0 ;
91
69
@ Option (names = "--client-name" , description = "Redis client name." )
92
70
private String clientName ;
93
- @ Getter
94
- @ Option (names = "--driver" , description = "Redis driver: ${COMPLETION-CANDIDATES}. (default: ${DEFAULT-VALUE})" )
95
- private RedisDriver driver = RedisDriver .Jedis ;
96
-
97
- public static void main (String [] args ) {
98
- SpringApplication .run (RiotApplication .class , args );
99
- }
100
-
101
- @ Override
102
- public void run (String ... args ) {
103
- CommandLine commandLine = new CommandLine (this );
104
- commandLine .registerConverter (Locale .class , s -> new Locale .Builder ().setLanguageTag (s ).build ());
105
- commandLine .setCaseInsensitiveEnumValuesAllowed (true );
106
- RunLast handler = new RunLast ();
107
- handler .useOut (System .out );
108
- DefaultExceptionHandler <List <Object >> exceptionHandler = CommandLine .defaultExceptionHandler ();
109
- exceptionHandler .useErr (System .err );
110
- commandLine .parseWithHandlers (handler , exceptionHandler , args );
111
- }
112
71
113
72
public RedisConnectionBuilder redisConnectionBuilder () {
114
73
RedisConnectionBuilder builder = new RedisConnectionBuilder ();
@@ -127,52 +86,23 @@ public RedisConnectionBuilder redisConnectionBuilder() {
127
86
return builder ;
128
87
}
129
88
130
- public String getHostname () {
89
+ private String getHostname () {
131
90
if (host != null ) {
132
91
return host .getHostName ();
133
92
}
134
93
return DEFAULT_HOST ;
135
94
}
136
95
137
- public <I , O > ExitStatus call (AbstractItemCountingItemStreamItemReader <I > reader , ItemProcessor <I , O > processor ,
138
- ItemStreamWriter <O > writer ) throws Exception {
139
- PlatformTransactionManager transactionManager = new ResourcelessTransactionManager ();
140
- MapJobRepositoryFactoryBean jobRepositoryFactory = new MapJobRepositoryFactoryBean (transactionManager );
141
- jobRepositoryFactory .afterPropertiesSet ();
142
- JobRepository jobRepository = jobRepositoryFactory .getObject ();
143
- JobBuilderFactory jobBuilderFactory = new JobBuilderFactory (jobRepository );
144
- StepBuilderFactory stepBuilderFactory = new StepBuilderFactory (jobRepository , transactionManager );
145
- JobBuilder <I , O > builder = new JobBuilder <>(jobBuilderFactory , stepBuilderFactory );
146
- builder .setChunkSize (chunkSize );
147
- if (maxCount != null ) {
148
- reader .setMaxItemCount (maxCount );
149
- }
150
- builder .setReader (reader );
151
- builder .setProcessor (processor );
152
- builder .setWriter (writer );
153
- builder .setPartitions (threads );
154
- builder .setSleep (sleep );
155
- Job job = builder .build ();
156
- SimpleJobLauncher jobLauncher = new SimpleJobLauncher ();
157
- jobLauncher .setJobRepository (jobRepository );
158
- jobLauncher .setTaskExecutor (new SyncTaskExecutor ());
159
- jobLauncher .afterPropertiesSet ();
160
- long startTime = System .currentTimeMillis ();
161
- System .out .println ("Running job" );
162
- JobExecution execution = jobLauncher .run (job , new JobParameters ());
163
- if (execution .getExitStatus ().equals (ExitStatus .FAILED )) {
164
- execution .getAllFailureExceptions ().forEach (e -> e .printStackTrace ());
165
- } else if (execution .getExitStatus ().equals (ExitStatus .COMPLETED )) {
166
- long endTime = System .currentTimeMillis ();
167
- long duration = endTime - startTime ;
168
- NumberFormat numberFormat = NumberFormat .getIntegerInstance ();
169
- double durationInSeconds = (double ) duration / 1000 ;
170
- int writeCount = execution .getStepExecutions ().iterator ().next ().getWriteCount ();
171
- double throughput = writeCount / durationInSeconds ;
172
- System .out .println ("Processed " + numberFormat .format (writeCount ) + " items in " + durationInSeconds
173
- + " seconds (" + numberFormat .format (throughput ) + " writes/sec)" );
174
- }
175
- return execution .getExitStatus ();
96
+ @ Override
97
+ public void run (String ... args ) {
98
+ CommandLine commandLine = new CommandLine (this );
99
+ commandLine .registerConverter (Locale .class , s -> new Locale .Builder ().setLanguageTag (s ).build ());
100
+ commandLine .setCaseInsensitiveEnumValuesAllowed (true );
101
+ RunLast handler = new RunLast ();
102
+ handler .useOut (System .out );
103
+ DefaultExceptionHandler <List <Object >> exceptionHandler = CommandLine .defaultExceptionHandler ();
104
+ exceptionHandler .useErr (System .err );
105
+ commandLine .parseWithHandlers (handler , exceptionHandler , args );
176
106
}
177
107
178
108
}
0 commit comments