Default locations
The default location for the license is a file called license.txt in the current working directory or ".". The default location for the Log4J configuration is conf/log4j.xml relative to the current working directory.
Loading from a different location
In order to load the license and Log4J configuration from a different location, we'll need to make use of a different constructor when instantiating the server. Looking at the Javadoc, we find this constructor:
public NIOServer(InetSocketAddress address,
InetSocketAddress streamingAdapterAddress,
URL licenseUrl,
URL log4jConfigurationUrl)
This constructor allows us to specify a URL for the license and logging configuration. The Javadoc also tells us the types of URL we can specify:
- File URLs e.g. file:///C:/licenses/license.txt
- Remote URLs e.g. http://www.example.com/logging/log4j.xml
- JAR URLs e.g. jar:file:///C:/lib/streamhub-license.jar!/license.txt or jar:http://www.example.com/logging/streamhub-logging.jar!/log4j.xml
- Classpath URLs via com.streamhub.util.UrlLoader e.g. UrlLoader.load("classpath:/license.txt"), or UrlLoader.load("classpath:/conf/log4j.xml")
As an example, the below starts a server as normal on port 80, loading the license and logging configuration from the classpath. This assumes we have prod.license and prod-logging.xml available on the classpath, perhaps through another JAR.
URL licenseUrl = UrlLoader.load("classpath:/prod.license");
URL loggingUrl = UrlLoader.load("classpath:/prod-logging.xml");
PushServer server = new NIOServer(new InetSocketAddress(80), null, licenseUrl, loggingUrl);
Note: we pass null through as the second parameter to indicate we don't want to start a streaming adapter. Streaming adapters are used to push updates through the server from remote locations and aren't covered in this article. Also, note we make use of the com.streamhub.util.UrlLoader to generate the URLs as classpath resources aren't supported by default in Java.
The second example below loads the files from a remote location, this would usually be an internal location only visible to the server and not accessible by the outside world.
URL licenseUrl = new URL("http://staging1.example.com/streamhub/license-staging.txt");
URL loggingUrl = new URL("http://staging1.example.com/streamhub/log4j-staging.xml");
PushServer server = new NIOServer(new InetSocketAddress(80), null, licenseUrl, loggingUrl);
The final example loads the files locally from the filesystem:
URL licenseUrl = new URL("file:///C:/prod/license.txt");
URL loggingUrl = new URL("file:///C:/prod/log4j.xml");
PushServer server = new NIOServer(new InetSocketAddress(80), null, licenseUrl, loggingUrl);
That last example brings this tutorial to an end. We've covered the default locations of license.txt and conf/log4j.xml and how to override them by passing URL arguments to the four parameter constructor. For more detail on the various constructors available, refer to the Javadoc online. More detail on the XML that goes in the Log4J configuration file can be found in the Log4J XML Configuration Primer.
No comments:
Post a Comment