Run Groovy application - groovy

Now I need development and run simple Groovy TCP server.
Could you please help me make the right choice how I can run my application?
I know follow methods how I can run my Groovy simple application:
1) I can run:
groovy myserver.groovy
2) I can create jar-file and run it. In this case I can write follow code (accordingly documentation):
import org.codehaus.groovy.runtime.InvokerHelper
class MyApp extends Script {
def run() {
// TODO
}
static void main(String[] args) {
InvokerHelper.runScript(MyApp, args)
}
}
Please help me, which way is more effective?

For simple cases you can run your Groovy script in "listening" mode with the -l flag, like this:
groovy -l 9010 SimpleServer.groovy
This starts the SimpleServer script listening on port 9010. I took this example from mrhaki's Groovy Goodness blog here: http://mrhaki.blogspot.com/2009/12/groovy-goodness-serversocket-scripts.html. Check it out for the complete example.

Related

What is the proper syntax for using subcontrollers with Ufront?

In my main controller I followed the instructions in the Controller documentation and I have the following meta data:
#:route(GET, "/about/*")
var aboutController:AboutController;
Then in my AboutController file I have:
package controller;
import api.TestApi;
import api.PortfolioItem;
using ufront.MVC;
using ufront.web.result.AddClientActionResult;
class AboutController extends Controller
{
#:route(GET, "/graphicDesign")
public function graphicDesign()
{
// return new PartialViewResult({… etcetera
}
}
When I visit the /about/graphicDesign path in my browser, the PHP server generates an error:
PHP Fatal error: Call to a member function execute() on null in /Users/allan/Documents/Freelance/Confidant/Website/3d confidant site/ufront/www/lib/controller/HomeController.class.php on line 70
The PHP lines 69-71 have:
public function execute_aboutController() {
return $this->context->injector->_instantiate(_hx_qtype("controller.AboutController"))->execute();
}
So, do I need different syntax so that the controller instantiates properly?
fyi i upgraded to 3.4 i don't have the same issues.
yes remoting does not work but only when targeting php7 . in fact even when not targeting php7 and running in a php7 apache environment doesn't work either. also in works with Mamp & php 5.6.
i had no probs with sub controllers though.
my answer is . did you try on another php environment ?

Compile groovy script statically with command line arguments

I am trying to statically compile a groovy script to speed up it's execution, but am not able to get it to work if command line arguments are used. My actual script is much longer, but the one-line script I use for this question perfectly reproduces my error.
Using the following script (test.groovy)
println(args.length)
This can be compiled with the command groovyc test.groovy and ran by the java command java -cp .;%GROOVY_HOME%\lib\* test and will simply print the number of command line arguments used.
Now, if I provide the script (config.groovy)
withConfig(configuration) {
ast(groovy.transform.CompileStatic)
}
and compile with groovyc -configscript config.groovy test.groovy, I get an error
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
testing.groovy: 1: [Static type checking] - The variable [args] is undeclared.
# line 1, column 9.
println(args.length)
^
1 error
This error only occurs when I attempt to compile statically. I can get it to work by wrapping the script in a class and putting my code in a main method (which, of course, is what the compiler does with a script), but not when I try to just use the script (which is what I prefer to do). For some reason, the variable args is unknown when compiled statically. I've tried this.args but still receive the error. If I try to declare a type for args (String[] args), it no longer receives the command line arguments.
Is there a way to still get the command line arguments when a script is compiled statically this way?
I am using Groovy version 2.4.10 on Windows 7 with Java 8.
The Script works via dynamic evaluation of the bindings object. If you want to use static compilation, you need to use the explicit form, changing your test.groovy script into the following:
String[] args = (String[])binding.getVariable('args')
println args.length
Using your already provided configuration script you do get a static compiled Script. I tested running it this way:
groovyc --configscript config.groovy test.groovy
java -cp .;%GROOVY_HOME%\lib\groovy-2.5.3.jar test 1 2 3
This prints 3.
If you want to not modify test.groovy at all, you can create a new base class:
import groovy.transform.CompileStatic
#CompileStatic
abstract class StaticBase extends Script {
StaticBase() {
}
StaticBase(Binding binding) {
super(binding)
}
String[] getArgs() {
(String[]) getBinding().getVariable("args")
}
}
Since the base class has a method getArgs, then when the test.groovy refers to args, the static compiler picks up the call to that method.
groovyc --configscript config.groovy -b StaticBase test.groovy
java -cp .;%GROOVY_HOME%\lib\groovy-2.5.3.jar test 1 2
The code in test.class has a run method whose code represents this.println(this.getArgs().length)
There's difference in executing Groovy class and running simple script. It's not correct that compiler simply wraps your script in main method, the body of the script will be copied into a run method.
Example:
println(args.length)
will be converted to
import org.codehaus.groovy.runtime.InvokerHelper
class Main extends Script {
def run() {
println(args.length)
}
static void main(String[] args) {
InvokerHelper.runScript(Main, args)
}
}
This compiles fine due to dynamic types.
Now, if we add #CompileStatic annotation to that class, we'll get the error of undeclared variable.
So, you have to wrap your code in class in order to use static compiling.
You can read more about Scripts versus classes in documentation.

WSO2 ESB Can't run Groovy script stored in registry

I'm trying to execute a Groovy script that I'm storing in the WSO2 ESB Local registry. When I do that I'm getting the following error:
ERROR {org.apache.synapse.mediators.bsf.ScriptMediator} - The script engine returned a NoSuchMethodException executing the external groovy script : Value {name ='null', keyValue ='file:Scripts/Groovy/test.groovy'} function mediate {org.apache.synapse.mediators.bsf.ScriptMediator}
java.lang.NoSuchMethodException: No signature of method: com.sun.script.groovy.GroovyScriptEngine.mediate() is applicable for argument types: (org.apache.synapse.mediators.bsf.ScriptMessageContext) values: [org.apache.synapse.mediators.bsf.ScriptMessageContext#716f8a10]
Possible solutions: wait()
If I put the code in-line in the script mediator, everything is running ok.
I've tried to wrap the script code like this <x><![CDATA[...code...]]></x>, as shown in the Using Ruby Scripts for Mediation example:
Sample 353: Using Ruby Scripts for Mediation. I add the groovy-all-2.4.7.jar to ESB_HOME\repository\components\lib too.
How can I run the groovy scripts stored in the registry? What am I doing wrong?
Here is the Groovy Script and the proxy, wiht which I'm testing:
Groovy Script
class Example {
static def DisplayName() {
println("This is how methods work in groovy");
println("This is an example of a simple method");
}
static void main(String[] args) {
DisplayName();
}
}
Proxy service
<proxy name="TestScriptProxy" startOnLoad="true" trace="disable"
transports="http https" xmlns="http://ws.apache.org/ns/synapse">
<target>
<inSequence>
<script language="groovy"><![CDATA[println "This is an in-line script";]]></script>
<script function="DisplayName"
key="file:Scripts/Groovy/test.groovy" language="groovy"/>
</inSequence>
<outSequence/>
<faultSequence/>
</target>
</proxy>
ESB version is 5.0.0 running on Windows 10.
If you have the Groovy script in the registry, the source should be the registry path. For example : gov:scripts/Groovy/test.groovy.
If you are referring a file give path relative to ESB_HOME. For example if scripts folder is in ESB_HOME directory, file:scripts/Groovy/test.groovy
First load the script file as below :
<localEntry key="DisplayNameScript" src="file:scripts/Groovy/test.groovy"/>
Now call the function
<script language="groovy" key="DisplayNameScript" function="DisplayName"/>

Yii 2 Running a Cron in the basic template

I have seen instructions on running a cron in the advanced template, but cant figure out how to do it on the basic template. I have the following controller in the basic controllers folder.
<?php
namespace app\controllers;
use yii\console\Controller;
/**
* Cron controller
*/
class CronController extends Controller {
public function actionIndex() {
echo "cron service runnning";
}
public function actionMail($to) {
echo "Sending mail to " . $to;
}
}
I have navigated to the root of my application and tried all these comands
yii cron
php yii cron
Im getting unknown comand "cron"
I know this is an old question but failed to find anyone with an actual answer to this.
If you look at the code for the Yii exec file you'll notice it requires /common/config/aliases.php file as well as console/config/main.php. You can naturally change these or copy over these from the advanced template.
Hope that solves the problem for anyone wanting to do the same thing.

initialize jmockit without -javaagent

I use jmockit and junit to write unit test for a module and run it in a STB. I use jmockit-1.7 because the STB only have java 5.
I got this error when run unit test:
java.lang.IllegalStateException: Jmockit has not been initialized. Check that your Java 5 VM has been started with -javaagent:jmockit.jar command line option
but my STB use siege java VM, so it doesn't have -javaagent command line option
I have google, and found a solution from Running tests with JMockit
#BeforeClass
public static void Initialize()
{
Mockit.setUpMocks();
}
But it's not work. And i have to uses jmockit-0.999.19 to have Mockit.setUpMocks();
Could any one help me initialize jmockit without -javaagent and run in java 1.5?
3 years ago...
Update: In the latest version JMockit v1.40 support for JDK6 will be dropped... so use JDK7+ ;-)

Resources