Compact server with customized code reload and sql-logs with one maven team for productive development

I would like to share the experience of using payara-micro in the development of corporate applications. I hope that someone will save time, since such a decision did not come immediately. If you are already using Payara or Glassfish as an industrial server, or just going to plunge into the world of javaee, then payara-micro and this article is for you.


You will need a web application that is built by maven into a war archive, and Java 8 (did not check for older ones).


First, I will give the full text of the profile, after which we will sort it into pieces and complete it with missing details that are not visible in the source code.


maven profile for payara-micro
<profile><id>micro</id><build><plugins><plugin><artifactId>maven-dependency-plugin</artifactId><version>2.6</version><executions><execution><id>copy-payara-micro</id><phase>package</phase><goals><goal>copy</goal></goals><configuration><outputDirectory>${project.build.directory}</outputDirectory><stripVersion>true</stripVersion><silent>true</silent><artifactItems><artifactItem><groupId>fish.payara.extras</groupId><artifactId>payara-micro</artifactId><type>jar</type></artifactItem></artifactItems></configuration></execution></executions></plugin><cut/><plugin><groupId>org.codehaus.mojo</groupId><artifactId>exec-maven-plugin</artifactId><version>1.2.1</version><executions><execution><goals><goal>exec</goal></goals></execution></executions><configuration><executable>java</executable><arguments><!--http://blog.payara.fish/using-hotswapagent-to-speed-up-development--><argument>-XXaltjvm=dcevm</argument><argument>-javaagent:hotswap/hotswap-agent.jar</argument><argument>-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=9009</argument><argument>-Duser.language=en</argument><argument>-Duser.region=US</argument><argument>-Ddb.ora.url=jdbc:p6spy:oracle:thin:@localhost:1521:XE</argument><argument>-Ddb.pg.url=jdbc:p6spy:postgresql://localhost:5432/postgres</argument><argument>-jar</argument><argument>${project.build.directory}/${project.build.finalName}-microbundle.jar</argument><!--<argument>&#45;&#45;prebootcommandfile</argument>--><!--<argument>src/main/setup/payara-micro-domain-config.txt</argument>--><argument>--domainConfig</argument><argument>src/main/setup/domain.xml</argument><argument>--deploy</argument><argument>${project.build.directory}/${project.build.finalName}.war</argument><argument>--rootDir</argument><argument>${project.build.directory}/payaramicro</argument></arguments></configuration></plugin><plugin><groupId>fish.payara.maven.plugins</groupId><artifactId>payara-micro-maven-plugin</artifactId><version>1.0.0</version><executions><execution><goals><goal>bundle</goal><goal>start</goal></goals></execution></executions><configuration><payaraVersion>4.1.2.173</payaraVersion><autoDeployArtifact>false</autoDeployArtifact><customJars><artifactItem><groupId>p6spy</groupId><artifactId>p6spy</artifactId><version>2.3.1</version></artifactItem><artifactItem><groupId>com.oracle</groupId><artifactId>ojdbc6</artifactId><version>11.2.0.3</version></artifactItem><dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId><version>42.2.0</version></dependency></customJars></configuration></plugin></plugins></build><cut/><dependencies><dependency><groupId>fish.payara.extras</groupId><artifactId>payara-micro</artifactId><version>4.1.2.173</version><scope>provided</scope></dependency><dependency><groupId>p6spy</groupId><artifactId>p6spy</artifactId><version>2.3.1</version><scope>provided</scope></dependency><dependency><groupId>com.oracle</groupId><artifactId>ojdbc6</artifactId><version>11.2.0.3</version><scope>provided</scope></dependency><dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId><version>42.2.0</version><scope>provided</scope></dependency></dependencies></profile>

The first thing to do is download the payara-micro archive itself. This is exactly what the first plugin ( maven-dependency-plugin ) does . The archive is a full-fledged application server that can be launched from the command line as a simple java-application.


The latest plugin ( payara-micro-maven-plugin ) has no features and is configured for a stable operating version of payra-micro 4.1.2.173 . From the possibilities of the plugin, the function of packing a web application into a special bundle, ready to launch, is used here. There are other ways to launch payara-micro, but here they are not useful to us.


It remains to generate the desired command line and put it all together. Exec-maven-plugin is used to run from the command line . Consider more command line options:


<configuration><executable>java</executable><arguments><!--http://blog.payara.fish/using-hotswapagent-to-speed-up-development--><argument>-XXaltjvm=dcevm</argument><argument>-javaagent:hotswap/hotswap-agent.jar</argument><argument>-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=9009</argument><argument>-Duser.language=en</argument><argument>-Duser.region=US</argument><argument>-Ddb.ora.url=jdbc:p6spy:oracle:thin:@localhost:1521:XE</argument><argument>-Ddb.pg.url=jdbc:p6spy:postgresql://localhost:5432/postgres</argument><argument>-jar</argument><argument>${project.build.directory}/${project.build.finalName}-microbundle.jar</argument><argument>--domainConfig</argument><argument>src/main/setup/domain.xml</argument><argument>--deploy</argument><argument>${project.build.directory}/${project.build.finalName}.war</argument><argument>--rootDir</argument><argument>${project.build.directory}/payaramicro</argument></arguments></configuration>

The first two arguments ( -XXaltjvm = dcevm -javaagent: hotswap / hotswap-agent.jar ) are required to change the source code without reloading the application. Thanks to the second argument ( -agentlib: jdwp = transport = dt_socket, server = y, suspend = n, address = 9009 ), we immediately start the application in debug mode, which will allow you to change not only the body of the methods themselves through the means of applying code changes in debugging , but also add new fields and more. You only need to connect with a debugger from the IDE, compile the modified source file and apply the changes.


The following two arguments ( -Duser.language = en -Duser.region = US ) are needed for the jdbc client to work correctly with Oracle. Connection settings to the test DBMS are passed through -D arguments. In my applications, I use a link of the form $ {db.ora.url} to these parameters in the resource file, which automatically creates them at the start. The substitution of the file that creates resources with test or industrial settings is also implemented through the profile in maven and its ability to filter text files.


Key arguments at startup indicate the path ( --domainConfig src / main / setup / domain.xml ) to the domain settings file, as well as the path to the war-file ( --deploy $ {project.build.directory} / $ {project. build.finalName} .war ) to be deployed to the server. Domain settings were obtained by me on the basis of a standard file inside the archive. The content will give as an example. For use in the development of these settings is more than enough.


domain.xml
<!--
    DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
    Copyright (c) 2010-2013 Oracle and/or its affiliates. All rights reserved.
    The contents of this file are subject to the terms of either the GNU
    General Public License Version 2 only ("GPL") or the Common Development
    and Distribution License("CDDL") (collectively, the "License").  You
    may not use this file except in compliance with the License.  You can
    obtain a copy of the License at
    https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
    or packager/legal/LICENSE.txt.  See the License for the specific
    language governing permissions and limitations under the License.
    When distributing the software, include this License Header Notice in each
    file and include the License file at packager/legal/LICENSE.txt.
    GPL Classpath Exception:
    Oracle designates this particular file as subject to the "Classpath"
    exception as provided by Oracle in the GPL Version 2 section of the License
    file that accompanied this code.
    Modifications:
    If applicable, add the following below the License Header, with the fields
    enclosed by brackets [] replaced by your own identifying information:
    "Portions Copyright [year] [name of copyright owner]"
    Contributor(s):
    If you wish your version of this file to be governed by only the CDDL or
    only the GPL Version 2, indicate your decision by adding "[Contributor]
    elects to include this software in this distribution under the [CDDL or GPL
    Version 2] license."  If you don't indicate a single choice of license, a
    recipient has the option to distribute your version of this file under
    either the CDDL, the GPL Version 2 or to extend the choice of license to
    its licensees as provided above.  However, if you add GPL Version 2 code
    and therefore, elected the GPL Version 2 license, then the option applies
    only if the new code is made subject to such option by the copyright
    holder.
Portions Copyright [2016] [Payara Foundation and/or its affiliates]
--><domainlog-root="${com.sun.aas.instanceRoot}/logs"application-root="${com.sun.aas.instanceRoot}/applications"version="10.0"><security-configurations><authorization-servicedefault="true"name="authorizationService"><security-providername="simpleAuthorization"type="Simple"provider-name="simpleAuthorizationProvider"><authorization-provider-configsupport-policy-deploy="false"name="simpleAuthorizationProviderConfig"></authorization-provider-config></security-provider></authorization-service></security-configurations><system-applications /><applications /><resources><jdbc-resourcepool-name="DerbyPool"jndi-name="jdbc/__default"object-type="system-all" /><jdbc-connection-poolis-isolation-level-guaranteed="false"name="DerbyPool"datasource-classname="org.apache.derby.jdbc.EmbeddedDataSource"res-type="javax.sql.DataSource"><propertyname="databaseName"value="${com.sun.aas.instanceRoot}/lib/databases/embedded_default" /><propertyname="connectionAttributes"value=";create=true" /></jdbc-connection-pool><context-servicedescription="context service"jndi-name="concurrent/__defaultContextService"object-type="system-all"></context-service><managed-executor-servicemaximum-pool-size="200"core-pool-size="6"long-running-tasks="true"keep-alive-seconds="300"hung-after-seconds="300"task-queue-capacity="20000"jndi-name="concurrent/__defaultManagedExecutorService"object-type="system-all"></managed-executor-service><managed-scheduled-executor-servicecore-pool-size="6"long-running-tasks="true"keep-alive-seconds="300"hung-after-seconds="300"jndi-name="concurrent/__defaultManagedScheduledExecutorService"object-type="system-all"></managed-scheduled-executor-service><managed-thread-factorydescription="thread factory"jndi-name="concurrent/__defaultManagedThreadFactory"object-type="system-all"></managed-thread-factory></resources><servers><servername="server"config-ref="server-config"><resource-refref="jdbc/__default" /></server></servers><configs><configname="server-config"><health-check-service-configurationenabled="false"><log-notifierenabled="true"/><eventbus-notifierenabled="false"/><cpu-usage-checkerunit="MINUTES"name="CPU"time="1"enabled="true" /><machine-memory-usage-checkerunit="MINUTES"name="MMEM"time="3"enabled="true" /><heap-memory-usage-checkerunit="MINUTES"name="HEAP"time="3"enabled="true" /><hogging-threads-checkerunit="MINUTES"name="HT"time="5"enabled="true" /><garbage-collector-checkerunit="MINUTES"name="GC"time="5"enabled="true" /></health-check-service-configuration><http-serviceaccess-logging-enabled="false"><access-logformat="%client.name% %auth-user-name% %datetime% %request% %status% %response.length%"rotation-interval-in-minutes="15"rotation-suffix="yyyy-MM-dd"></access-log><virtual-serverid="server"access-logging-enabled="false"access-log=""network-listeners="http-listener, https-listener"></virtual-server></http-service><iiop-service><orbuse-thread-pool-ids="thread-pool-1"></orb><iiop-listenerid="orb-listener-1"enabled="false"address="0.0.0.0"></iiop-listener></iiop-service><admin-servicesystem-jmx-connector-name="system"type="das-and-server"><jmx-connectorport="8686"address="0.0.0.0"security-enabled="false"auth-realm-name="admin-realm"name="system"enabled="false"></jmx-connector><das-config></das-config></admin-service><connector-serviceclass-loading-policy="global"shutdown-timeout-in-seconds="30"></connector-service><!--<ejb-container steady-pool-size="0" max-pool-size="32" session-store="${com.sun.aas.instanceRoot}/session-store" pool-resize-quantity="8">--><!--<ejb-timer-service ejb-timer-service="Hazelcast"></ejb-timer-service>--><!--</ejb-container>--><log-servicefile="${com.sun.aas.instanceRoot}/logs/server.log"log-rotation-limit-in-bytes="2000000"><module-log-levels /></log-service><security-serviceactivate-default-principal-to-role-mapping="true"jacc="simple"><auth-realmclassname="com.sun.enterprise.security.auth.realm.file.FileRealm"name="admin-realm"><propertyvalue="${com.sun.aas.instanceRoot}/config/admin-keyfile"name="file" /><propertyvalue="fileRealm"name="jaas-context" /></auth-realm><auth-realmclassname="com.sun.enterprise.security.auth.realm.file.FileRealm"name="file"><propertyvalue="${com.sun.aas.instanceRoot}/config/keyfile"name="file" /><propertyvalue="fileRealm"name="jaas-context" /></auth-realm><auth-realmclassname="com.sun.enterprise.security.auth.realm.certificate.CertificateRealm"name="certificate" /><jacc-providerpolicy-configuration-factory-provider="com.sun.enterprise.security.provider.PolicyConfigurationFactoryImpl"policy-provider="com.sun.enterprise.security.provider.PolicyWrapper"name="default"><propertyvalue="${com.sun.aas.instanceRoot}/generated/policy"name="repository" /></jacc-provider><jacc-providerpolicy-configuration-factory-provider="com.sun.enterprise.security.jacc.provider.SimplePolicyConfigurationFactory"policy-provider="com.sun.enterprise.security.jacc.provider.SimplePolicyProvider"name="simple" /><audit-moduleclassname="com.sun.enterprise.security.ee.Audit"name="default"><propertyvalue="false"name="auditOn" /></audit-module><message-security-configauth-layer="SOAP"><provider-configprovider-id="XWS_ClientProvider"class-name="com.sun.xml.wss.provider.ClientSecurityAuthModule"provider-type="client"><request-policyauth-source="content" /><response-policyauth-source="content" /><propertyvalue="s1as"name="encryption.key.alias" /><propertyvalue="s1as"name="signature.key.alias" /><propertyvalue="false"name="dynamic.username.password" /><propertyvalue="false"name="debug" /></provider-config><provider-configprovider-id="ClientProvider"class-name="com.sun.xml.wss.provider.ClientSecurityAuthModule"provider-type="client"><request-policyauth-source="content" /><response-policyauth-source="content" /><propertyvalue="s1as"name="encryption.key.alias" /><propertyvalue="s1as"name="signature.key.alias" /><propertyvalue="false"name="dynamic.username.password" /><propertyvalue="false"name="debug" /><propertyvalue="${com.sun.aas.instanceRoot}/config/wss-server-config-1.0.xml"name="security.config" /></provider-config><provider-configprovider-id="XWS_ServerProvider"class-name="com.sun.xml.wss.provider.ServerSecurityAuthModule"provider-type="server"><request-policyauth-source="content" /><response-policyauth-source="content" /><propertyvalue="s1as"name="encryption.key.alias" /><propertyvalue="s1as"name="signature.key.alias" /><propertyvalue="false"name="debug" /></provider-config><provider-configprovider-id="ServerProvider"class-name="com.sun.xml.wss.provider.ServerSecurityAuthModule"provider-type="server"><request-policyauth-source="content" /><response-policyauth-source="content" /><propertyvalue="s1as"name="encryption.key.alias" /><propertyvalue="s1as"name="signature.key.alias" /><propertyvalue="false"name="debug" /><propertyvalue="${com.sun.aas.instanceRoot}/config/wss-server-config-1.0.xml"name="security.config" /></provider-config></message-security-config><propertyvalue="SHA-256"name="default-digest-algorithm" /></security-service><transaction-servicetx-log-dir="${com.sun.aas.instanceRoot}/logs" ></transaction-service><hazelcast-runtime-configurationenabled="false"multicastGroup="224.2.2.4"multicastPort="2904"generate-names="true"></hazelcast-runtime-configuration><phone-home-runtime-configuration></phone-home-runtime-configuration><request-tracing-service-configuration><log-notifierenabled="true"></log-notifier></request-tracing-service-configuration><notification-service-configurationenabled="true"><log-notifier-configurationenabled="true"></log-notifier-configuration><eventbus-notifier-configurationenabled="false"></eventbus-notifier-configuration></notification-service-configuration><batch-runtime-configurationtable-prefix="jbatch"data-source-lookup-name="jdbc/__default"></batch-runtime-configuration><availability-serviceavailability-enabled="true" ><web-container-availabilityavailability-enabled="true"persistence-scope="modified-session"sso-failover-enabled="true"persistence-type="hazelcast"></web-container-availability><!--<ejb-container-availability sfsb-ha-persistence-type="hazelcast" sfsb-persistence-type="hazelcast" ></ejb-container-availability>--></availability-service><network-config><protocols><protocolname="http-listener"><httpdefault-virtual-server="server"xpowered-by="false"max-connections="250"comet-support-enabled="true"><file-cacheenabled="false"></file-cache></http></protocol><protocolsecurity-enabled="true"name="https-listener"><httpdefault-virtual-server="server"xpowered-by="false"comet-support-enabled="true"max-connections="250"><file-cacheenabled="false"></file-cache></http><sslclassname="com.sun.enterprise.security.ssl.GlassfishSSLImpl"ssl3-enabled="false"cert-nickname="s1as"></ssl></protocol></protocols><network-listeners><network-listenerport="8080"protocol="http-listener"transport="tcp"name="http-listener"thread-pool="http-thread-pool"enabled="true" /><network-listenerport="8443"protocol="https-listener"transport="tcp"name="https-listener"thread-pool="http-thread-pool"enabled="false" /></network-listeners><transports><transportbyte-buffer-type="HEAP"name="tcp"acceptor-threads="-1"></transport></transports></network-config><thread-pools><thread-poolname="http-thread-pool"min-thread-pool-size="10"max-thread-pool-size="200"max-queue-size="4096"></thread-pool><thread-poolname="thread-pool-1"min-thread-pool-size="2"max-thread-pool-size="200"/></thread-pools></config></configs><system-propertyname="fish.payara.classloading.delegate"value="false"/><propertyname="administrative.domain.name"value="domain1"/></domain>

I will also give an example of a resource description file for automatically creating them at the start of the application.


glassfish-resources.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd"><resources><jdbc-resourceenabled="true"jndi-name="java:app/ora_con"object-type="user"pool-name="java:app/OraPool"/><jdbc-connection-poolallow-non-component-callers="false"associate-with-thread="false"connection-creation-retry-attempts="0"connection-creation-retry-interval-in-seconds="10"connection-leak-reclaim="false"connection-leak-timeout-in-seconds="0"connection-validation-method="table"driver-classname="com.p6spy.engine.spy.P6SpyDriver"fail-all-connections="false"idle-timeout-in-seconds="300"is-connection-validation-required="true"is-isolation-level-guaranteed="true"lazy-connection-association="false"lazy-connection-enlistment="false"match-connections="false"max-connection-usage-count="0"max-pool-size="32"max-wait-time-in-millis="60000"name="java:app/OraPool"non-transactional-connections="false"ping="false"pool-resize-quantity="2"pooling="true"res-type="java.sql.Driver"statement-cache-size="0"statement-leak-reclaim="false"statement-leak-timeout-in-seconds="0"statement-timeout-in-seconds="-1"steady-pool-size="8"validate-atmost-once-period-in-seconds="0"validation-table-name="DUAL"wrap-jdbc-objects="true"><propertyname="URL"value="${db.ora.url}"/><propertyname="User"value="system"/><propertyname="Password"value="1"/><propertyname="property.dynamic-reconfiguration-waittimeout-in-seconds"value="60" /></jdbc-connection-pool><jdbc-resourceenabled="true"jndi-name="java:app/pg_con"object-type="user"pool-name="java:app/PGPool"/><jdbc-connection-poolallow-non-component-callers="false"associate-with-thread="false"connection-creation-retry-attempts="0"connection-creation-retry-interval-in-seconds="10"connection-leak-reclaim="false"connection-leak-timeout-in-seconds="0"connection-validation-method="table"driver-classname="com.p6spy.engine.spy.P6SpyDriver"fail-all-connections="false"idle-timeout-in-seconds="300"is-connection-validation-required="true"is-isolation-level-guaranteed="true"lazy-connection-association="false"lazy-connection-enlistment="false"match-connections="false"max-connection-usage-count="0"max-pool-size="32"max-wait-time-in-millis="60000"name="java:app/PGPool"non-transactional-connections="false"ping="false"pool-resize-quantity="2"pooling="true"res-type="java.sql.Driver"statement-cache-size="0"statement-leak-reclaim="false"statement-leak-timeout-in-seconds="0"statement-timeout-in-seconds="-1"steady-pool-size="8"validate-atmost-once-period-in-seconds="0"validation-table-name="DUAL"wrap-jdbc-objects="true"><propertyname="URL"value="${db.pg.url}"/><propertyname="User"value="postgres"/><propertyname="Password"value="postgres"/><propertyname="property.dynamic-reconfiguration-waittimeout-in-seconds"value="60" /></jdbc-connection-pool></resources>

You can install dcevm from here . Be sure to install it as an alternative JVM, so that you can always work on a regular default.


You can download the latest version of the agent for unlimited source code changes, as well as reloading the code inside frameworks like hibernate from here .


Ideas on setting up a reload code for payara were underlined here . But the truth is only the idea itself, since the implementation for payara-micro is not considered there.


Thanks to the p6spy library, you can find your sql logs in the project root folder - the spy.log file. The p6spy library can be customized if necessary, but this is a topic for a completely different article.


After placing the profile described above in your pom.xml and the additional files discussed above, you can launch your application with the following command in the appropriate folders:


mvn install exec:exec -P micro

And no additional plugins for the application server in the IDE are required ...


Also popular now: