1
1
package com .microsoft .azure .functions .worker ;
2
2
3
+ import java .net .MalformedURLException ;
4
+ import java .net .URISyntaxException ;
5
+ import java .net .URL ;
3
6
import java .util .logging .*;
4
7
import javax .annotation .*;
5
8
6
9
import org .apache .commons .cli .*;
7
10
import org .apache .commons .lang3 .exception .ExceptionUtils ;
8
11
12
+ import static com .microsoft .azure .functions .worker .Constants .*;
13
+
9
14
/**
10
15
* The entry point of the Java Language Worker. Every component could get the command line options from this singleton
11
16
* Application instance, and typically that instance will be passed to your components as constructor arguments.
@@ -15,36 +20,17 @@ private Application(String[] args) {
15
20
this .parseCommandLine (args );
16
21
}
17
22
18
- @ Override
19
- public String getHost () { return this .host ; }
20
- @ Override
21
- public int getPort () { return this .port ; }
22
- @ Override
23
- public boolean logToConsole () { return this .logToConsole ; }
24
- @ Override
25
- public Integer getMaxMessageSize () { return this .maxMessageSize ; }
26
- private String getWorkerId () { return this .workerId ; }
27
- private String getRequestId () { return this .requestId ; }
28
-
29
- private void printUsage () {
30
- HelpFormatter formatter = new HelpFormatter ();
31
- formatter .printHelp ("Application" , this .OPTIONS , true );
32
- }
33
-
34
- private boolean isCommandlineValid () { return this .commandParseSucceeded ; }
35
-
36
23
@ PostConstruct
37
24
private void parseCommandLine (String [] args ) {
38
25
CommandLineParser parser = new DefaultParser ();
39
26
try {
40
- CommandLine commands = parser .parse (this .OPTIONS , args );
41
- this .host = this .parseHost (commands .getOptionValue ("h" ));
42
- this .port = this .parsePort (commands .getOptionValue ("p" ));
43
- this .workerId = this .parseWorkerId (commands .getOptionValue ("w" ));
44
- this .requestId = this .parseRequestId (commands .getOptionValue ("q" ));
45
- this .logToConsole = commands .hasOption ("l" );
46
- if (commands .hasOption ("m" )) {
47
- this .maxMessageSize = this .parseMaxMessageSize (commands .getOptionValue ("m" ));
27
+ CommandLine commands = parser .parse (this .OPTIONS , args , true );
28
+ this .uri = this .parseUri (commands .getOptionValue (FUNCTIONS_URI_OPTION ));
29
+ this .workerId = this .parseWorkerId (commands .getOptionValue (FUNCTIONS_WORKER_ID_OPTION ));
30
+ this .requestId = this .parseRequestId (commands .getOptionValue (FUNCTIONS_REQUEST_ID_OPTION ));
31
+ this .logToConsole = commands .hasOption (FUNCTIONS_CONSOLE_LOG_OPTION );
32
+ if (commands .hasOption (FUNCTIONS_GRPC_MAX_MESSAGE_LENGTH_OPTION )) {
33
+ this .maxMessageSize = this .parseMaxMessageSize (commands .getOptionValue (FUNCTIONS_GRPC_MAX_MESSAGE_LENGTH_OPTION ));
48
34
}
49
35
this .commandParseSucceeded = true ;
50
36
} catch (ParseException ex ) {
@@ -53,84 +39,123 @@ private void parseCommandLine(String[] args) {
53
39
}
54
40
}
55
41
56
- private String parseHost (String input ) { return input ; }
57
-
58
- private int parsePort (String input ) throws ParseException {
59
- try {
60
- int result = Integer .parseInt (input );
61
- if (result < 1 || result > 65535 ) {
62
- throw new IndexOutOfBoundsException ("port number out of range" );
42
+ public static void main (String [] args ) {
43
+ WorkerLogManager .getSystemLogger ().log (Level .INFO , "Azure Functions Java Worker version [ " + version () + "]" );
44
+ Application app = new Application (args );
45
+ if (!app .isCommandlineValid ()) {
46
+ app .printUsage ();
47
+ System .exit (1 );
48
+ } else {
49
+ try (JavaWorkerClient client = new JavaWorkerClient (app )) {
50
+ client .listen (app .getWorkerId (), app .getRequestId ()).get ();
51
+ } catch (Exception ex ) {
52
+ WorkerLogManager .getSystemLogger ().log (Level .SEVERE , ExceptionUtils .getRootCauseMessage (ex ), ex );
53
+ System .exit (-1 );
63
54
}
64
- return result ;
65
- } catch (NumberFormatException | IndexOutOfBoundsException ex ) {
66
- throw new ParseException (String .format (
67
- "port number \" %s\" is not qualified. It must be an integer within range [1, 65535]" , input ));
68
55
}
69
56
}
70
57
71
- private String parseRequestId (String input ) { return input ; }
72
-
73
- private String parseWorkerId (String input ) { return input ; }
74
-
75
- private Integer parseMaxMessageSize (String input ) {
76
- return Integer .parseInt (input );
77
- }
78
-
79
58
private boolean commandParseSucceeded = false ;
80
- private String host ;
59
+ private String uri , host , workerId , requestId ;
81
60
private int port ;
82
- private String workerId , requestId ;
83
61
private boolean logToConsole ;
84
62
private Integer maxMessageSize = null ;
85
-
86
63
private final Options OPTIONS = new Options ()
87
- .addOption (Option .builder ("h" ).longOpt ("host" )
88
- .hasArg ().argName ("HostName" )
89
- .desc ("The address of the machine that the Azure Functions host is running on" )
90
- .required ()
91
- .build ())
92
- .addOption (Option .builder ("p" ).longOpt ("port" )
93
- .hasArg ().argName ("PortNumber" )
94
- .desc ("The port number which the Azure Functions host is listening to" )
64
+ .addOption (Option .builder ("u" ).longOpt (FUNCTIONS_URI_OPTION )
65
+ .hasArg ().argName ("Uri" )
66
+ .desc ("The uri of the machine that the Azure Functions host is running on" )
95
67
.required ()
96
68
.build ())
97
- .addOption (Option .builder ("w" ).longOpt ("workerId" )
69
+ .addOption (Option .builder ("w" ).longOpt (FUNCTIONS_WORKER_ID_OPTION )
98
70
.hasArg ().argName ("WorkerId" )
99
71
.desc ("The ID of this running worker throughout communication session" )
100
72
.required ()
101
73
.build ())
102
- .addOption (Option .builder ("q" ).longOpt ("requestId" )
74
+ .addOption (Option .builder ("q" ).longOpt (FUNCTIONS_REQUEST_ID_OPTION )
103
75
.hasArg ().argName ("RequestId" )
104
76
.desc ("The startup request ID of this communication session" )
105
77
.required ()
106
78
.build ())
107
- .addOption (Option .builder ("l" ).longOpt ("consoleLog" )
108
- .desc ("Whether to duplicate all host logs to console as well" )
109
- .build ())
110
- .addOption (Option .builder ("m" ).longOpt ("grpcMaxMessageLength" )
79
+ .addOption (Option .builder ("l" ).longOpt (FUNCTIONS_GRPC_MAX_MESSAGE_LENGTH_OPTION )
111
80
.hasArg ().argName ("MessageSizeInBytes" )
112
81
.desc ("The maximum message size could be used by GRPC protocol" )
82
+ .build ())
83
+ .addOption (Option .builder ("m" ).longOpt (FUNCTIONS_CONSOLE_LOG_OPTION )
84
+ .desc ("Whether to duplicate all host logs to console as well" )
113
85
.build ());
114
86
87
+ @ Override
88
+ public String getHost () {
89
+ return this .host ;
90
+ }
115
91
116
- public static void main (String [] args ) {
117
- WorkerLogManager .getSystemLogger ().log (Level .INFO , "Azure Functions Java Worker version [ " + version () + "]" );
118
- Application app = new Application (args );
119
- if (!app .isCommandlineValid ()) {
120
- app .printUsage ();
121
- System .exit (1 );
122
- } else {
123
- try (JavaWorkerClient client = new JavaWorkerClient (app )) {
124
- client .listen (app .getWorkerId (), app .getRequestId ()).get ();
125
- } catch (Exception ex ) {
126
- WorkerLogManager .getSystemLogger ().log (Level .SEVERE , ExceptionUtils .getRootCauseMessage (ex ), ex );
127
- System .exit (-1 );
92
+ @ Override
93
+ public int getPort () {
94
+ return this .port ;
95
+ }
96
+
97
+ public String getUri () {
98
+ return this .uri ;
99
+ }
100
+
101
+ @ Override
102
+ public boolean logToConsole () {
103
+ return this .logToConsole ;
104
+ }
105
+
106
+ @ Override
107
+ public Integer getMaxMessageSize () {
108
+ return this .maxMessageSize ;
109
+ }
110
+
111
+ private String getWorkerId () {
112
+ return this .workerId ;
113
+ }
114
+
115
+ private String getRequestId () {
116
+ return this .requestId ;
117
+ }
118
+
119
+ private boolean isCommandlineValid () {
120
+ return this .commandParseSucceeded ;
121
+ }
122
+
123
+ private String parseUri (String uri ) throws ParseException {
124
+ try {
125
+ URL url = new URL (uri );
126
+ url .toURI ();
127
+ this .host = url .getHost ();
128
+ this .port = url .getPort ();
129
+ if (port < 1 || port > 65535 ) {
130
+ throw new IndexOutOfBoundsException ("port number out of range" );
128
131
}
132
+ return uri ;
133
+ } catch (MalformedURLException | URISyntaxException | IndexOutOfBoundsException e ) {
134
+ throw new ParseException (String .format (
135
+ "Error parsing URI \" %s\" . Please provide a valid URI" , uri ));
129
136
}
130
137
}
131
138
139
+ private String parseRequestId (String input ) {
140
+ return input ;
141
+ }
142
+
143
+ private String parseWorkerId (String input ) {
144
+ return input ;
145
+ }
146
+
147
+ private Integer parseMaxMessageSize (String input ) {
148
+ return Integer .parseInt (input );
149
+ }
150
+
132
151
public static String version () {
133
152
String jarVersion = Application .class .getPackage ().getImplementationVersion ();
134
153
return jarVersion != null && !jarVersion .isEmpty () ? jarVersion : "Unknown" ;
135
154
}
155
+
156
+ private void printUsage () {
157
+ HelpFormatter formatter = new HelpFormatter ();
158
+ formatter .setWidth (100 );
159
+ formatter .printHelp ("Application" , this .OPTIONS , true );
160
+ }
136
161
}
0 commit comments