sábado, 19 de agosto de 2017

Heroku Lab Run Non-web Java Dynos


El laboratorio esta hecho para corren con maven, en netbeans por default usa ant, aunque tiene algunos arquetipos de maven que se pueden usar. En este caso el arquetivo es diferente a los incluidos en netbeans, así que usaremos un projecto default de maven, para despues modificar el archivo de control maven pom.xml.

se usan dos archivos java
src/main/java/OneOffProcess.java
public class OneOffProcess
{
    public static void main(String[] args)
    {
        System.out.println("OneOffProcess executed.");
    }
} 
 
src/main/java/WorkerProcess.java
public class WorkerProcess
{
    public static void main(String[] args)
    {
        while(true) {
            try {
                Thread.sleep(1000);
            } catch(InterruptedException e) {}

            System.out.println("Worker process woke up");
        }
    }
}
 
El pom default generado por netbeans es remplaado por 
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.bext</groupId>
    <artifactId>RunNonWebJavaHeroku</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>appassembler-maven-plugin</artifactId>
                <version>1.1.1</version>
                <configuration>
                    <assembleDirectory>target</assembleDirectory>
                    <programs>
                        <program>
                            <mainClass>WorkerProcess</mainClass>
                            <name>worker</name>
                        </program>
                        <program>
                            <mainClass>OneOffProcess</mainClass>
                            <name>oneoff</name>
                        </program>
                    </programs>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>assemble</goal>
                        </goals>
                    </execution>
                </executions>            
            </plugin>
        </plugins>
    </build>
</project>


 Desde el IDE o la consola de comandos construimos la aplicacion
C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building RunNonWebJavaHeroku 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ RunNonWebJavaHeroku ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ RunNonWebJavaHeroku ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ RunNonWebJavaHeroku ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ RunNonWebJavaHeroku ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ RunNonWebJavaHeroku ---
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ RunNonWebJavaHeroku ---
[INFO] Building jar: C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku\target\RunNonWebJavaHeroku-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- appassembler-maven-plugin:1.1.1:assemble (default) @ RunNonWebJavaHeroku ---
[INFO] Installing C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku\target\RunNonWebJavaHeroku-1.0-SNAPSHOT.jar to C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku\target\repo\org\bext\RunNonWebJavaHeroku\1.0-SNAPSHOT\RunNonWebJavaHeroku-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 05:44 min
[INFO] Finished at: 2017-08-17T13:59:14-05:00
[INFO] Final Memory: 8M/126M
[INFO] ------------------------------------------------------------------------

C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku>

Corremos la aplicación localmente

C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku>target\bin\worker
Worker Process woke up
Worker Process woke up
Worker Process woke up
Worker Process woke up
Worker Process woke up
...

C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku>target\bin\oneoff
OneOffPrecess Executed.!

Deployamos a Heroku

  commiteamos los cambios de git, desde el IDE o linea de comandos.

C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku>git init
Reinitialized existing Git repository in C:/Users/Bext/Documents/NetBeansProjects/exercise/RunNonWebJavaHeroku/.git/

C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku>git add .
warning: LF will be replaced by CRLF in target/bin/oneoff.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in target/bin/worker.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in target/repo/org/bext/RunNonWebJavaHeroku/1.0-SNAPSHOT/maven-metadata-appassembler.xml.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in target/repo/org/bext/RunNonWebJavaHeroku/maven-metadata-appassembler.xml.
The file will have its original line endings in your working directory.

C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku>git commit -m "Listo para Deployar"
[master warning: LF will be replaced by CRLF in target/repo/org/bext/RunNonWebJavaHeroku/1.0-SNAPSHOT/maven-metadata-appassembler.xml.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in target/repo/org/bext/RunNonWebJavaHeroku/maven-metadata-appassembler.xml.
The file will have its original line endings in your working directory.
fd6a9dc] Listo para Deployar
warning: LF will be replaced by CRLF in target/repo/org/bext/RunNonWebJavaHeroku/1.0-SNAPSHOT/maven-metadata-appassembler.xml.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in target/repo/org/bext/RunNonWebJavaHeroku/maven-metadata-appassembler.xml.
The file will have its original line endings in your working directory.
 4 files changed, 3 insertions(+), 3 deletions(-)
 rewrite target/RunNonWebJavaHeroku-1.0-SNAPSHOT.jar (64%)
 rewrite target/repo/org/bext/RunNonWebJavaHeroku/1.0-SNAPSHOT/RunNonWebJavaHeroku-1.0-SNAPSHOT.jar (64%)

C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku>

Creamos la aplicación en heroku (remoto)

C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku>heroku create
Creating app... done, floating-eyrie-93131
https://floating-eyrie-93131.herokuapp.com/ | https://git.heroku.com/floating-eyrie-93131.git

Deployamos el codigo


C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku>git remote -v
heroku  https://git.heroku.com/immense-castle-67621.git (fetch)
heroku  https://git.heroku.com/immense-castle-67621.git (push)

C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku>heroku git:remote -a floating-eyrie-93131
set git remote heroku to https://git.heroku.com/floating-eyrie-93131.git

C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku>git remote -v
heroku  https://git.heroku.com/floating-eyrie-93131.git (fetch)
heroku  https://git.heroku.com/floating-eyrie-93131.git (push)


C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku>git push heroku master
Counting objects: 69, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (49/49), done.
Writing objects: 100% (69/69), 13.01 KiB | 0 bytes/s, done.
Total 69 (delta 18), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Java app detected
remote: -----> Installing OpenJDK 1.8... done
remote: -----> Installing Maven 3.3.9... done
....

Scaling Worker Processes

C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku>heroku ps:scale worker=1
Scaling dynos... done, now running worker at 1:Free

C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku>heroku logs --tail
2017-08-18T00:38:37.659743+00:00 app[worker.1]: Worker Process woke up
2017-08-18T00:38:38.659965+00:00 app[worker.1]: Worker Process woke up
2017-08-18T00:38:39.660199+00:00 app[worker.1]: Worker Process woke up
2017-08-18T00:38:40.660405+00:00 app[worker.1]: Worker Process woke up
2017-08-18T00:38:41.660590+00:00 app[worker.1]: Worker Process woke up
2017-08-18T00:38:42.660820+00:00 app[worker.1]: Worker Process woke up
2017-08-18T00:38:43.661039+00:00 app[worker.1]: Worker Process woke up

One-off Dynos


C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku>heroku run "sh target/bin/oneoff"
Running sh target/bin/oneoff on floating-eyrie-93131... up, run.1450 (Free)
OneOffPrecess Executed.!

C:\Users\Bext\Documents\NetBeansProjects\exercise\RunNonWebJavaHeroku>

No hay comentarios:

Publicar un comentario