I still have some misunderstanding about the way vertx suppose you'll deploy/start yor app. Still there are two questions, but they are hardly related for me.
Question 1: I there a way to make your app launchable from both: command line and IDEA?
In one hand there is a Starter class (provided by Vert.x) to launch our application from command line. It provides main method for you. But if I launch it from shell I won't have an ability to debug it, right?
In other hand to lanch your app in IDEA you need to create main method manually. Also some features like configurations file are described only for command line way. I.e. java -jar target/my-first-app-1.0-SNAPSHOT-fat.jar -conf src/main/conf/my-application-conf.json
Question 2: How to set configuration file programmatically?
I have a ServerVerticle, all questions there:
public class ServerVerticle extends AbstractVerticle {
//QUESTION 1: I need this method for IDEA debugging, but don't need when launch app from command line. How to be??
public static void main(String[] args) {
Consumer<Vertx> runner = vertx -> {
try {
vertx.deployVerticle("server.ServerVerticle", new DeploymentOptions());
} catch (Throwable t) {
t.printStackTrace();
}
};
Vertx vertx = Vertx.vertx(new VertxOptions());
runner.accept(vertx);
}
#Override
public void start() {
vertx.deployVerticle(...); //deploy another verticles
//QUESTION 2: how to pass configuration file programmaticaly in vertx? I can parse json file with java I/O, but if some configs are used accross the whole app?
vertx.createNetServer()
.connectHandler(this::handleMessage)
.listen(8080, "localhost");
}
}
In order to run your application from IntelliJ all you need to do is to add a run configuration where:
main class: io.vertx.core.Launcher
arguments: run ServerVerticle -conf /path/to/your/config.json
In order to set the configuration programmatically you need to pass a Json object to your deployment options as explained on the javadocs.
Related
I have a Java app that retrieves username/password creds from a resource property file.
For obvious reasons, I don't include the actual username/password values when I commit to SVN. I just make sure they're replaced with bogus values before committing.
Now I'm wanting to build my app on a remote Jenkins 2.0 box, however, since the values being stored in SVN are bogus, when Jenkins checks out my code and runs my integration tests, they fail because they can't authenticate when using the bogus values.
I was thinking maybe I could add some logic to my application that will first try to retrieve the username/password creds via a System.getProperty() and if those properties don't exist, then try looking at the resource file.
So, is it possible with Jenkins 2.0 to pass an argument to the JVM during a build so that a System.getProperty() would work?
You can do this using Jenkins Pipeline with declarative syntax:
Jenkinsfile:
pipeline {
agent {
label 'master'
}
environment {
//foo var
foo = "Hello !";
}
stages {
stage("test-var") {
steps {
// If you are using LINUX use EXPORT
bat "set foo=\"${env.foo}\""
bat "java -jar /someplace/DummyMain.jar"
}
}
}
}
Please notice I am using: System.getenv("foo"); instead of getProperty
Java Class:
/**
*
* #author daniel
*/
public class DummyMain {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
String property = System.getenv("foo");
System.out.println(property);
}
}
You will get: Hello ! when you run your Pipeline. If you are more familiar with Script Groovy syntax instead of Pipeline Declarative Syntax should not be a problem.
Just write the logic for read the Property File and you are set !
Background:
I am currently working on a project in eclipse that programatically executes JUnit tests that are pushed to a server.
So far everything works but I would like to know the results of the tests (specifically any failures) so I can push them out to an email. Right now the tests just output to the console but that doesn't seem to give me much output to actually use.
Right now I use the Runtime class to call the tests but that doesn't seem to have the functionality I need for getting results.
I have looked into the JUnitCore class but can't call any tests outside of the current java project.
So my main question would be how can I use JUnitCore to run junit tests in a specific JAR file? Or is there an easier way to approach this problem using a different class?
This is the only thing I've been able to get to work:
RunTests()
{
junitCore = new JUnitCore();
junitCore.run(AllTests.class);
}
But I would like to do something along the lines of this:
RunTests()
{
junitCore = new JUnitCore();
junitCore.run("C:\\$batch\\test\\hil research\\201507071307\\CommsTestRunner\\plugins\\TestSuite\\US35644.class");
}
I would appreciate any suggestions to this problem I am having. I'm an EE and was just introduced to java last month so this has been quite the challenge for me.
JUnitCore expects to read loaded classes, not class files in a JAR. Your real question is likely how to load the JAR (or directory of .class files) so it can be run.
Poke around with URLClassLoader; once you've amended the classpath appropriately, you can get a Class out of findClass and pass it into the JUnitCore methods you've found.
Since the tests might have classes that are also used by your server (but not necessarily at the same version) I would suggest not having your server directly run the tests. Instead, you can have your server start a new JVM that runs the tests. This is how IDEs like Eclipse run tests. You simply need to write a main class that has JUnit run the tests, and serializes the results on disk.
Your main class would look something like this:
public class MyRunner {
public static void main(String... args) throws IOException {
String path = System.getProperty("resultPath");
if (path == null) {
throw new NullPointerException("must specify resultPath property");
}
// Possibly install a security manager to prevent calls to System.exit()
Result result = new JUnitCore().runMain(new RealSystem(), args);
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(path)) {
out.writeObject(result);
}
System.exit(result.wasSuccessful() ? 0 : 1);
}
}
Then your server simply needs to construct a java command line with the jars that include the tests, the JUnit jar file, and a jar that contains MyRunner.
Currently working on Selenium WebDriver and using Java. I have a project called*Test*.
In that Project i have many Java Programs such as Login.java, Testing1.java etc.,.
The scenario is i want to run all my scripts daily morning at 12.00 a.m. Is there any possibility to create a scheduler to run my scripts automatically.
Create an testng.xml file say name as testsuite.xml.
Now follow below 2 steps:
Step 1: Create an batch file for scheduler:
use below code - modify it and paste in notepad. save the notepad in working directory as"run.bat"
set ProjectPath=C:\Selenium\Selenium_tests\DemoProject
echo %ProjectPath%
set classpath=%ProjectPath%\bin;%ProjectPath%\Lib\*
echo %classpath%
java org.testng.TestNG %ProjectPath%\testsuite.xml
a) First line is for setting project path .
b) second line is for verifying that path is set or not.
c) third
line is for setting classpath - lib folder contain all the jar
file added to project build path
d) fourth line is for verifying
whether classpath is set or not
e) fifth line is for executing
xml file having details of all test.
Step 2:
Go to control panel.
Administrative tool.
Task scheduler and create a task which will trigger run.bat file at the time you want.
It will work.
check with quartz scheduler.. http://quartz-scheduler.org/
I am currently working on a similar project where I have to check different web applications for their availability every ~5 minutes and report any errors via mail. I am also using TestNG ans the WebDriver together. I solved my "scheduling problem" by using the TimerTask class.
Here's a short code example: (Find more code examples here)
import java.util.Timer;
import java.util.TimerTask;
public class KeepMeAwake {
*
* #param args
*/
public static void main(String[] args) {
TimerTask action = new TimerTask() {
public void run() {
Beep b = Beep.getInstance();
b.beep();
}
};
Timer caretaker = new Timer();
caretaker.schedule(action, 1000, 5000);
}
}
Since it implements Runnable, you can run multiple threads with it.
Hope that helps.
If you have questions how to integrate it with your TestNG set up, just shoot.
Follow the above steps and in windows scheduler do the steps :
Creating .bat file steps
Task Scheduler in Windows > Create new Task>
'Action' settings - "Start in (Optional)" option.
Go the task properties --> Action tab --> Edit --> Fill up as below:
Action: Start a program
Program/script: path to your batch script e.g. C:\Users\beruk\bodo.bat
Add arguments (optional): <if necessary - depending on your script>
Start in (optional): Put the full path to your batch script location e.g. C:\Users\beruk\(Do not put quotes around Start In)
Then Click OK
It works for me. Good Luck!
is there any framework that supports building console apps in java?
I know about Spring-Shell but that creates a shell that is not needed for my purpose.
For example i would like to create annotated Command-Classes like...
#Command(name="c", desc="Copies files from source directory to target directory.")
public class MyCopyCommand {
#Argument(mandatory=true, desc="source directory")
private String sourceDirectory;
#Argument(mandatory=true, desc="target directory")
private String targetDirectory;
#Execute
public void copy() {
// copy files
}
}
And some Executor-Class should process the given program arguments and invoke the appropriate Command-Class.
Is there any Framwork like that? The best it would integrate with Spring.
There is Apache Felix Gogo and it is extensible. The example even uses the same #Command annotation as in the OP.
Now i've implemented it with Spring-Shell. Spring-Shell doesn't open a shell if the program is invoked with a command.
./myApp => opens shell
./myApp myCommand => doesn't open a shell
That suites for my purpose.
I want to point my play application to a particular application config file based on the environment it is running in. There are three and they correspond to the standard Play states:
application.dev.conf
application.test.conf
application.prod.conf
A co-worker shared a method for doing this which requires setting an OS environment variable.
I'd like to eliminate the need to set an OS variable.
My preference is the use whatever Play uses at startup to know what mode it is in.
For example, if you execute play run from the command line, part of the output is "[info] play - Application started (Dev)"
I want to use this information in my Global.java where I override onLoadConfig like so:
public Configuration onLoadConfig(Configuration baseConfiguration, File f, ClassLoader loader) {
String playEnv=<some static method to get mode>;
Config additionalConfig = ConfigFactory.parseFile(new File(f,"conf/application."+playEnv+".conf"));
Config baseConfig = baseConfiguration.getWrappedConfiguration().underlying();
return new Configuration(baseConfig.withFallback(additionalConfig));
}
Everything that I find is how to do this after the application has been started i.e. using isDev(), isTest(), isProd().
Is there static method that provides the mode while I am overriding onLoadConfig in Global.java?
I think play run is dev, play start is prod.
EDIT: If you're looking to see what the current mode is, it's passed in through play.api.Play.current:
Play.current.mode match {
case Play.Mode.Dev => "dev"
case Play.Mode.Prod => "prod"
}
The issue appeared to be addressed in the latest Play (3.0.0). There is another onLoadConfig method added to Global witch has a mode: {Dev,Prod,Test} parameter.
public Configuration onLoadConfig(Configuration config, File path, ClassLoader classloader, Mode mode) {
return onLoadConfig(config, path, classloader); // default implementation
}
Play allows to specifying alternative configuration file with command line so no need for setting OS variables.
You can of course create some bash script / bat file to avoid writing it every time