Java Stream API: ArrayList to JsonArray
03 January 2017
You can convert an ArrayList to a Java EE JsonArray using the Java Stream API in the following way.
// set up example ArrayList<Pet> pets = new ArrayList<>(); pets.add(new Pet("Goldie", "Fish")); pets.add(new Pet("Daisy", "Cow")); pets.add(new Pet("Snowball", "Cat")); // the work pets.stream() .map((a) -> { return Json.createObjectBuilder() .add("id", a.getName()) .add("type", a.getGroup()) .build(); }) .collect( Json::createArrayBuilder, JsonArrayBuilder::add, JsonArrayBuilder::add ) .build();
The .map
operation of the stream API takes a Function<T,R>
. The function converts each item to a JsonObject. Then the .collect
operation creates the JsonArray using each of the JsonObjects.
I hope you find this useful.
Read : Java Stream API: ArrayList to JsonArray
Helper Script: MongoDB as a Windows Service
24 October 2016
I would like to share a Windows CMD script for adding MongoDB to a development environment. If you do development work that includes MongoDB on a Windows' PC then you should find this script helpful.
The script is designed to be used in application development environments as opposed to production. After MongoDB has been installed using the MongoDB installer this CMD script creates the MongoDB data folder and the log folder; it then adds MongoDB as a Windows Service using mongod by MongoDB. And then to save you from going into the Windows Service Tool script the script starts the MongoDB service for you. After you have run the script MongoDB will start each time your PC starts.
Note you will need to run this script as an administrator. This script assumed that the MongoDB installer added MongoDB's bin to the path during its install.
add-mongo-as-service.cmd
:: Name: add-mongo-as-service.cmd :: Purpose: Adds MongoDB as a service to a Windows application development environment. :: Note: Run this script as an administrator. :: Author: www.geoffhayward.eu :: Revision: Oct 2016 - initial version @ECHO OFF SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION SET ME=%~n0 set MY_DIR=%~dp0 IF NOT EXIST "%USERPROFILE%\Mongo\" MKDIR "%USERPROFILE%\Mongo\" IF NOT EXIST "%USERPROFILE%\Mongo\data\" MKDIR "%USERPROFILE%\Mongo\data\" IF NOT EXIST "%USERPROFILE%\Mongo\logs\" MKDIR "%USERPROFILE%\Mongo\logs\" CALL mongod --dbpath "%USERPROFILE%\Mongo\data" --logpath "%USERPROFILE%\Mongo\logs\log.txt" --install CALL NET START MongoDB ENDLOCAL
Add the script to your source control to enjoy a more productive way of quickly setting up your environment.
Read : Helper Script: MongoDB as a Windows Service
WildFly: Removing a Failed Deployment with JBoss CLI
05 September 2016
I was having trouble working out how to remove the WAR file of a failed deployment from WildFly using the JBoss CLI. I found that I could not simply type undeploy <WAR name>
to remove a failed deployment from WildFly using the JBoss CLI. However, I did find that the following command let me remove a failed deployment and it's WAR file.
/deployment=<WAR name>:remove
After running this command I checked that the failed deployment and it's WAR file had been removed by running deployment-into
and then by looking in the WildFly deployment's folder on the disc. This command had successfully removed the deployment and it's WAR file.
Read : WildFly: Removing a Failed Deployment with JBoss CLI
Setting WildFly's Logging Level with Windows Batch Script
05 August 2016
I would like to share a simple Windows Batch script that I made. The Batch script changes the WildFly's logging level quickly and easily. It is as easy as running change-logging-level.cmd --file-debug --console-error
change-logging-level.cmd
:: Name: change-logging-level.cmd :: Purpose: Set's the logging level to very low or the setting given as an argument :: Author: geoffhayward.eu :: Revision: August 2016 - initial version @ECHO OFF SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION SET NOPAUSE=true SET ME=%~n0 SET SCRIPT=%TEMP%\%ME%-%DATE:/=-%.txt SET CON_LEVEL=ERROR SET FILE_LEVEL=ERROR :parse_args IF NOT "%~1"=="" ( IF "%~1"=="--all" ( SET CON_LEVEL=ALL SET FILE_LEVEL=ALL ) IF "%~1"=="--console-all" ( SET CON_LEVEL=ALL ) IF "%~1"=="--file-all" ( SET FILE_LEVEL=ALL ) IF "%~1"=="--config" ( SET CON_LEVEL=CONFIG SET FILE_LEVEL=CONFIG ) IF "%~1"=="--console-config" ( SET CON_LEVEL=CONFIG ) IF "%~1"=="--file-config" ( SET FILE_LEVEL=CONFIG ) IF "%~1"=="--debug" ( SET CON_LEVEL=DEBUG SET FILE_LEVEL=DEBUG ) IF "%~1"=="--console-debug" ( SET CON_LEVEL=DEBUG ) IF "%~1"=="--file-debug" ( SET FILE_LEVEL=DEBUG ) IF "%~1"=="--error" ( SET CON_LEVEL=ERROR SET FILE_LEVEL=ERROR ) IF "%~1"=="--console-error" ( SET CON_LEVEL=ERROR ) IF "%~1"=="--file-error" ( SET FILE_LEVEL=ERROR ) IF "%~1"=="--fatal" ( SET CON_LEVEL=FATAL SET FILE_LEVEL=FATAL ) IF "%~1"=="--console-fatal" ( SET CON_LEVEL=FATAL ) IF "%~1"=="--file-fatal" ( SET FILE_LEVEL=FATAL ) IF "%~1"=="--fine" ( SET CON_LEVEL=FINE SET FILE_LEVEL=FINE ) IF "%~1"=="--console-fine" ( SET CON_LEVEL=FINE ) IF "%~1"=="--file-fine" ( SET FILE_LEVEL=FINE ) IF "%~1"=="--finer" ( SET CON_LEVEL=FINER SET FILE_LEVEL=FINER ) IF "%~1"=="--console-finer" ( SET CON_LEVEL=FINER ) IF "%~1"=="--file-finer" ( SET FILE_LEVEL=FINER ) IF "%~1"=="--finest" ( SET CON_LEVEL=FINEST SET FILE_LEVEL=FINEST ) IF "%~1"=="--console-finest" ( SET CON_LEVEL=FINEST ) IF "%~1"=="--file-finest" ( SET FILE_LEVEL=FINEST ) IF "%~1"=="--info" ( SET CON_LEVEL=INFO SET FILE_LEVEL=INFO ) IF "%~1"=="--console-info" ( SET CON_LEVEL=INFO ) IF "%~1"=="--file-info" ( SET FILE_LEVEL=INFO ) IF "%~1"=="--off" ( SET CON_LEVEL=OFF SET FILE_LEVEL=OFF ) IF "%~1"=="--console-off" ( SET CON_LEVEL=OFF ) IF "%~1"=="--file-off" ( SET FILE_LEVEL=OFF ) IF "%~1"=="--trace" ( SET CON_LEVEL=TRACE SET FILE_LEVEL=TRACE ) IF "%~1"=="--console-trace" ( SET CON_LEVEL=TRACE ) IF "%~1"=="--file-trace" ( SET FILE_LEVEL=TRACE ) IF "%~1"=="--warn" ( SET CON_LEVEL=WARN SET FILE_LEVEL=WARN ) IF "%~1"=="--console-warn" ( SET CON_LEVEL=WARN ) IF "%~1"=="--file-warn" ( SET FILE_LEVEL=WARN ) IF "%~1"=="--warning" ( SET CON_LEVEL=WARNING SET FILE_LEVEL=WARNING ) IF "%~1"=="--console-warning" ( SET CON_LEVEL=WARNING ) IF "%~1"=="--file-warning" ( SET FILE_LEVEL=WARNING ) SHIFT GOTO :parse_args ) ECHO batch > %SCRIPT% ECHO /subsystem=logging/console-handler=CONSOLE:change-log-level(level=%CON_LEVEL%)>> %SCRIPT% ECHO /subsystem=logging/periodic-rotating-file-handler=FILE:change-log-level(level=%CON_LEVEL%)>> %SCRIPT% ECHO run-batch >> %SCRIPT% CALL %JBOSS_HOME%\bin\jboss-cli.bat -c --file="%TEMP%\%ME%-%DATE:/=-%.txt" ENDLOCAL
This version of the Batch script is designed for Windows based dev environments, with WildFly as the target dev Java EE application container. The Batch script creates a simple JBoss CLI script and then sends that script into JBoss CLI. Note %JBOSS_HOME%
needs to be set as an environment variable.
Calling this 'change-logging-level.cmd' script without any arguments will set WildFly's logging level down to 'ERROR' for both the console and file logging. Supplying arguments will override the default, for example change-logging-level.cmd --fine
will immediately (except long running transactions), set all logging to 'fine'.
Read : Setting WildFly's Logging Level with Windows Batch Script
The Java 8 Lambda way: ArrayList to an Array
15 July 2016
In an earlier post this year, 'Convert an ArrayList to an Array in Java', I commented on a way to convert an ArrayList to an Array in Java. Here is the example code from the earlier post.
List<String> results = new ArrayList<>(); ... results.toArray(new String[results.size()]);
This earlier way works fine, however it's old school. Here is the Java 8 Lambda way to convert an ArrayList to an Array:
Long[] longArray = longArrayList.stream() .map(Long::new) .toArray(Long[]::new);
This example works by mapping a new object for each item in the ArrayList's stream to the new stream, and then collecting that stream as an array.
Read : The Java 8 Lambda way: ArrayList to an Array