environment: play-1.2.5
I need to put datas to the database while starting the play framework application, so I let it be done in the Bootstrap.java.
Basically, I'm just calling a Model and new an instance for it and storing data to the database repeatedly. Like below.
{
MagazineType magazineType = new MagazineType();
magazineType.abbreviation = "XX";
magazineType.save();
{
{
MagazineTypeFirstCat magazineTypeFirstCat = new MagazineTypeFirstCat();
magazineTypeFirstCat.name = "XX_First";
magazineTypeFirstCat.magazineType = magazineType;
magazineTypeFirstCat.save();
{
{
MagazineTypeSecondCat magazineTypeSecondCat = new MgazineTypeSecondCat();
magazineTypeSecondCat.name = "XX_Second_1";
magazineTypeSecondCat.magazineTypeFirstCat = magazineTypeFirstCat;
magazineTypeSecondCat.save();
}
{
MagazineTypeSecondCat magazineTypeSecondCat = new MgazineTypeSecondCat();
magazineTypeSecondCat.name = "XX_Second_2";
magazineTypeSecondCat.magazineTypeFirstCat = magazineTypeFirstCat;
magazineTypeSecondCat.save();
}
}
}
}
}
But the data is quite large, so the length of the code in Bootstrap.java is over 5000 lines.
When the length of code is under 3000 lines, the flamework will be started just fine, but exceptions occurred if the code length exceeds 3000 lines.
Below is the log for the exception, any suggestion will be appreciated. Thanks in advance.
14:42:20,855 WARN ~ You're running Play! in DEV mode
14:42:21,023 INFO ~ Listening for HTTP on port 9000 (Waiting a first request to start) ...
14:42:35,275 ERROR ~
#6edp5mkl9
Internal Server Error (500) for request GET /
Oops: UnexpectedException
An unexpected error occured caused by exception UnexpectedException: While applying class
play.classloading.enhancers.PropertiesEnhancer on Bootstrap
play.exceptions.UnexpectedException: While applying play.CorePlugin#1f0b7d3 on Bootstrap
at play.plugins.PluginCollection.enhance(PluginCollection.java:454)
at play.classloading.ApplicationClasses$ApplicationClass.enhance
(ApplicationClasses.java:208)
at play.classloading.ApplicationClassloader.loadApplicationClass(ApplicationClassloader.java:157)
at play.classloading.ApplicationClassloader.getAllClasses(ApplicationClassloader.java:417)
at play.classloading.ApplicationClassloader.getAssignableClasses(ApplicationClassloader.java:441)
at {module:crud}/app/views/tags/crud/types.tag.(line:3)
at play.templates.GroovyTemplate.internalRender(GroovyTemplate.java:229)
at play.templates.GroovyTemplate$ExecutableTemplate.invokeTag(GroovyTemplate.java:374)
at {module:crud}/conf/routes.(line:4)
at play.templates.GroovyTemplate.internalRender(GroovyTemplate.java:229)
at play.templates.Template.render(Template.java:26)
at play.templates.GroovyTemplate.render(GroovyTemplate.java:184)
at play.mvc.Router.parse(Router.java:162)
at play.mvc.Router.parse(Router.java:190)
at play.mvc.Router.parse(Router.java:164)
at play.mvc.Router.load(Router.java:50)
at play.mvc.Router.detectChanges(Router.java:219)
at Invocation.HTTP Request(Play!)
Caused by: play.exceptions.UnexpectedException: While applying class play.classloading.enhancers.PropertiesEnhancer on Bootstrap
at play.CorePlugin.enhance(CorePlugin.java:296)
at play.plugins.PluginCollection.enhance(PluginCollection.java:451)
at play.classloading.ApplicationClasses$ApplicationClass.enhance(ApplicationClasses.java:208)
at play.classloading.ApplicationClassloader.loadApplicationClass(ApplicationClassloader.java:157)
at play.classloading.ApplicationClassloader.getAllClasses(ApplicationClassloader.java:417)
at play.classloading.ApplicationClassloader.getAssignableClasses(ApplicationClassloader.java:441)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:229)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:52)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:124)
at Template_1001$_run_closure1.doCall(types.tag:6)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:66)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:44)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:149)
at Template_1001$_run_closure1.doCall(types.tag)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod
(ClosureMetaClass.java:273)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
at groovy.lang.Closure.call(Closure.java:282)
at groovy.lang.Closure.call(Closure.java:277)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:99)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.access$300(GroovyCategorySupport.java:61)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:239)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.use(DefaultGroovyMethods.java:333)
at org.codehaus.groovy.runtime.dgm$717.invoke(Unknown Source)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoMetaMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:307)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:51)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:44)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:153)
at Template_1001.run(types.tag:2)
at play.templates.GroovyTemplate.internalRender(GroovyTemplate.java:229)
at play.templates.GroovyTemplate$ExecutableTemplate.invokeTag(GroovyTemplate.java:374)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:362)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:66)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:44)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:161)
at Template_1000$_run_closure1.doCall(routes:41)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:66)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:44)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:149)
at Template_1000$_run_closure1.doCall(routes)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
at groovy.lang.Closure.call(Closure.java:282)
at groovy.lang.Closure.call(Closure.java:277)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:99)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.access$300(GroovyCategorySupport.java:61)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:239)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.use(DefaultGroovyMethods.java:333)
at org.codehaus.groovy.runtime.dgm$717.invoke(Unknown Source)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoMetaMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:307)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:51)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:44)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:153)
at Template_1000.run(routes:2)
... 9 more
Caused by: javassist.CannotCompileException: invalid opcode address
at javassist.expr.ExprEditor.doit(ExprEditor.java:116)
at javassist.CtBehavior.instrument(CtBehavior.java:618)
at play.classloading.enhancers.PropertiesEnhancer.enhanceThisClass(PropertiesEnhancer.java:130)
at play.CorePlugin.enhance(CorePlugin.java:293)
... 83 more
Caused by: javassist.bytecode.BadBytecode: invalid opcode address
at javassist.bytecode.CodeIterator.nextOpcode(CodeIterator.java:586)
at javassist.bytecode.CodeIterator.next(CodeIterator.java:157)
at javassist.bytecode.stackmap.Liveness.analyze(Liveness.java:237)
at javassist.bytecode.stackmap.Liveness.computeUsage(Liveness.java:216)
at javassist.bytecode.stackmap.Liveness.compute(Liveness.java:38)
at javassist.bytecode.stackmap.TypedBlock.makeBlocks(TypedBlock.java:54)
at javassist.bytecode.stackmap.MapMaker.make(MapMaker.java:91)
at javassist.bytecode.MethodInfo.rebuildStackMap(MethodInfo.java:404)
at javassist.bytecode.MethodInfo.rebuildStackMapIf6(MethodInfo.java:389)
at javassist.expr.ExprEditor.doit(ExprEditor.java:112)
... 86 more
Java class size is limited to 64 kb so your class might be too big.
For such a use case I prefer to put all thi initialization in a sql file and use an sql load script in the bootstrap, it scales better.
Related
I face a problem while executing an admin script, written in groovy. The original goal is removing all zip files from all repositories. My approach was iterating through all repositories, finding components containing the "zip" extension in a single transaction, while removing them in the next transaction.
I wrote the following script to achieve this approach
import org.sonatype.nexus.repository.Repository
import org.sonatype.nexus.repository.storage.Component
import org.sonatype.nexus.repository.storage.Query
import org.sonatype.nexus.repository.storage.StorageFacet
import org.sonatype.nexus.repository.storage.StorageTx
// get repos
final Iterable<Repository> repositories = repository.repositoryManager.browse()
// iterate
repositories.each {
Repository repo ->
if ("hosted".equals(repo.type.toString()) &&
"maven2".equals(repo.format.toString())) {
log.info("Found hosted repository " + repo.name)
managePossibleZips(repo)
}
}
/**
* Manage zip files for given repository
* #param repo
* #return
*/
def managePossibleZips(Repository repo) {
// get storage facet
final StorageFacet storageFacet = repo.facet(StorageFacet)
// find zip files
final Iterable<Component> components = findZips(storageFacet, repo)
// erase zip files
if (0 == components.size) {
log.info("No zip files in this repository")
} else {
eraseZips(storageFacet, components)
}
}
/**
* Find components, that contain extension 'zip', based on supplied repository and storageFacet
*
* #param storageFacet the storage facet
* #param repo the supplied repository
* #return Iterable < Component > List of components containing zip files
*/
def findZips(StorageFacet storageFacet, Repository repo) {
// get transaction
final StorageTx transaction = storageFacet.txSupplier().get()
// tx
transaction.begin()
// find components containing zip files
Iterable<Component> components = transaction.findComponents(
Query.builder().where('extension =').param('zip').build(), [repo]
)
// commit
transaction.commit()
// close
transaction.close()
// return components
return components
}
/**
* Erase supplied components
*
* #param storageFacet the storage facet
* #param components list of components to be erased
*/
def eraseZips(StorageFacet storageFacet, Iterable<Component> components) {
components.each { Component component ->
// get transaction
final StorageTx transaction = storageFacet.txSupplier().get()
// tx
transaction.begin()
// find components containing zip files
transaction.deleteComponent(component);
// commit
transaction.commit()
// close
transaction.close()
// return components
}
}
log.info("All done, bye bye")
While trying to execute the script the following error pops up in the syslog
2018-10-09 10:53:07,809+0200 INFO [qtp402299672-4809] admin org.sonatype.nexus.quartz.internal.task.QuartzTaskInfo - Task 'nx32' [script] runNow
2018-10-09 10:53:07,809+0200 INFO [qtp402299672-4809] admin org.sonatype.nexus.quartz.internal.task.QuartzTaskInfo - Task 'nx32' [script] state change WAITING -> RUNNING
2018-10-09 10:53:07,821+0200 INFO [quartz-5-thread-18] *SYSTEM org.sonatype.nexus.internal.script.ScriptTask - Task log: /opt/sonatype-work/nexus3/log/tasks/script-20181009105307817.log
2018-10-09 10:53:08,174+0200 INFO [quartz-5-thread-18] *SYSTEM org.sonatype.nexus.internal.script.ScriptTask - Found hosted repository ps-myrepo-mvn
2018-10-09 10:53:08,175+0200 WARN [quartz-5-thread-18] *SYSTEM org.sonatype.nexus.quartz.internal.task.QuartzTaskJob - Task d446ea78-eeb0-4b17-be55-3e611e696762 : 'nx32' [script] execution failure
javax.script.ScriptException: javax.script.ScriptException: java.lang.IllegalArgumentException: Nested DB TX!
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:158)
at org.sonatype.nexus.internal.script.ScriptServiceImpl.eval(ScriptServiceImpl.java:153)
at org.sonatype.nexus.internal.script.ScriptServiceImpl.eval(ScriptServiceImpl.java:162)
at org.sonatype.nexus.internal.script.ScriptTask.execute(ScriptTask.java:78)
at org.sonatype.nexus.scheduling.TaskSupport.call(TaskSupport.java:93)
at org.sonatype.nexus.quartz.internal.task.QuartzTaskJob.doExecute(QuartzTaskJob.java:145)
at org.sonatype.nexus.quartz.internal.task.QuartzTaskJob.execute(QuartzTaskJob.java:108)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.sonatype.nexus.thread.internal.MDCAwareRunnable.run(MDCAwareRunnable.java:40)
at org.apache.shiro.subject.support.SubjectRunnable.doRun(SubjectRunnable.java:120)
at org.apache.shiro.subject.support.SubjectRunnable.run(SubjectRunnable.java:108)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: javax.script.ScriptException: java.lang.IllegalArgumentException: Nested DB TX!
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:155)
... 15 common frames omitted
Caused by: java.lang.IllegalArgumentException: Nested DB TX!
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:135)
at org.sonatype.nexus.repository.storage.StorageTxImpl.<init>(StorageTxImpl.java:154)
at org.sonatype.nexus.repository.storage.StorageFacetImpl.openStorageTx(StorageFacetImpl.java:263)
at org.sonatype.nexus.repository.storage.StorageFacetImpl.lambda$0(StorageFacetImpl.java:145)
at com.google.common.base.Supplier$get.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at Script25.findZips(Script25.groovy:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at groovy.lang.DelegatingMetaClass.invokeMethod(DelegatingMetaClass.java:151)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl$2.invokeMethod(GroovyScriptEngineImpl.java:301)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:69)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:174)
at Script25.managePossibleZips(Script25.groovy:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:384)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:69)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
at Script25$_run_closure1.doCall(Script25.groovy:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at groovy.lang.Closure.call(Closure.java:414)
at groovy.lang.Closure.call(Closure.java:430)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2040)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2025)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2066)
at org.codehaus.groovy.runtime.dgm$162.invoke(Unknown Source)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:274)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at Script25.run(Script25.groovy:11)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:317)
... 16 common frames omitted
try to wrap your transaction in a try-catch block and always close your transaction, like this:
try {
tx.begin()
components = tx.countComponents(Query.builder().where('1').eq(1).build(), [repo])
tx.commit()
} catch (Exception e) {
log.warn("Transaction failed {}", e.toString())
tx.rollback()
} finally {
tx.close()
}
Also you probably need to restart Nexus, to get rid of the locked transactions. Otherwise each script with a transaction will yield now the Nested DB TX exception
Edit: the countComponents() function in my code is just an example
I am trying to generate the Table classes from an existing SQL tables in MySQL.
I am using Slick 2.1 (slick_2.11-2.1.0-M2), mysql-connector-java-5.1.31-bin
I have created a simple Scala file:
object MySQLPlayground {
def main(args: Array[String]) {
scala.slick.model.codegen.SourceCodeGenerator.main(
Array("scala.slick.driver.MySQLDriver", "com.mysql.jdbc.Driver",
"jdbc:mysql://localhost/fannuaire", "src/main/scala", "modelGene", "user", "password")
)
}
}
But it seems the driver path is wrong. I have the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: scala/reflect/runtime/package$
at scala.slick.model.codegen.SourceCodeGenerator$.main(SourceCodeGenerator.scala:60)
at com.scala.mysql.MySQLPlayground$.main(MySQLPlayground.scala:10)
at com.scala.mysql.MySQLPlayground.main(MySQLPlayground.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.ClassNotFoundException: scala.reflect.runtime.package$
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 8 more
Since the doc says :
Slick’s code generator comes with a default runner that can be used
from the command line or from Java/Scala.
I was expecting it to work out of the box.
Is there any other set up I have to do? Is the driver's path correct?
Thanks
Please add scala-reflect to your code generator project dependencies.
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value
I'm trying to make a simple https request using this library https://github.com/scalaj/scalaj-http . The request contains some json data.
Here is what I'm doing:
val jsonHeaders = """{"jsonrpc": "2.0", "method": "someMethod", "params": {"dataIds":["12348" , "456"]}, "data2": "777"}"""
val result = Http.postData("https://someurl.com/json-rpc", jsonHeaders)
.header("content-type", "application/json")
.header("X-Application", "myCode")
.header("X-Authentication", "myCode2")
.option(HttpOptions.readTimeout(10000))
.asString
//.responseCode -- the same error
println(result)
And it always returns me a timeout error:
[error] (run-main) java.net.SocketTimeoutException: connect timed out
java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(Unknown Source)
at scalaj.http.Http$$anonfun$3.apply(Http.scala:263)
at scalaj.http.Http$$anonfun$3.apply(Http.scala:261)
at scalaj.http.Http$Request.process(Http.scala:102)
at scalaj.http.Http$Request.apply(Http.scala:90)
at scalaj.http.Http$Request.asString(Http.scala:133)
at Application$delayedInit$body.apply(Application.scala:27)
at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:71)
at scala.App$$anonfun$main$1.apply(App.scala:71)
at scala.collection.immutable.List.foreach(List.scala:318)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32)
at scala.App$class.main(App.scala:71)
at Application$.main(Application.scala:8)
at Application.main(Application.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
[trace] Stack trace suppressed: run last compile:run for the full output.
java.lang.RuntimeException: Nonzero exit code: 1
at scala.sys.package$.error(package.scala:27)
If I do just
val jsonHeaders = """{"version123": "12.0", "method": "someMethod", "params": {"dataIds": ["12348" , "456"]}, "data2": "777"}"""
val result = Http.postData("https://someurl.com/some-url2", jsonHeaders)
.header("content-type", "application/json")
.header("X-Application", "myCode")
.header("X-Application1234", "myCode2")
.option(HttpOptions.readTimeout(10000))
println(result)
it returns
Request(POST,<function2>,<function1>,List(),List((X-Authentication,myCode2), (X-Application1234,myCode), (content-type,application/json)),List(<function1>, <function1>, <function1>),DIRECT)
What do I do wrong and is there any another simple way to send https request? Even if involves spray framework, it would be ok (I don't find any example of how to do that in spray, though).
UPDATE:
An example has been taken from here Doing HTTP request in Scala
You need to specify a connection timeout alongside your current read timeout with: .option(HttpOptions.connTimeout(10000)).option(HttpOptions.readTimeout(50000)). Change the 10000 to a value that works for you. The default connection timeout is a pretty aggressive 100.
You do already have a read timeout specified, but the exception says it is timing out on establishing the connection, not on the the reading of the response.
See docs: https://github.com/scalaj/scalaj-http#custom-connect-and-read-timeouts
On the HttpRequest object there is also a .timeout() member method. This method is defined as:
/** The socket connection and read timeouts in milliseconds. Defaults are 1000 and 5000 respectively */
def timeout(connTimeoutMs: Int, readTimeoutMs: Int): HttpRequest = options(
Seq(HttpOptions.connTimeout(connTimeoutMs), HttpOptions.readTimeout(readTimeoutMs))
)
The nice part about this is that it was a bit easier to mock using Mockito. See github as the reference.
I've updated my Elastic Search from 0.19.4 to 0.20.6 and I'm not getting the desired results.. I'm doing a dynamic search after key presses and the code that was working fine before acts different now.
The Search:
'' = (empty field) fine
a = 9400 hits
ab = 126 hits ERROR
abc = 2 hits ERROR
abcd 0 hits fine
Dependencies I've changed:
runtime 'org.elasticsearch:elasticsearch-lang-groovy:1.1.0' --> runtime 'org.elasticsearch:elasticsearch-lang-groovy:1.3.0'
runtime 'org.elasticsearch:elasticsearch:0.19.4' --> runtime 'org.elasticsearch:elasticsearch:0.20.6'
runtime 'org.xerial.snappy:snappy-java:1.0.4.1' (new)
Stack:
2013-04-09 10:47:58,130 [http-bio-8080-exec-2] DEBUG xxxx.SearchController - result stuff is: [hits:org.elasticsearch.search.internal.InternalSearchHits#9b0d61b]
2013-04-09 10:47:58,137 [http-bio-8080-exec-2] ERROR xxxx.SearchController - Problem...
org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: Error converting Bean with class org.elasticsearch.search.internal.InternalSearchHit
at grails.converters.JSON.value(JSON.java:199)
at grails.converters.JSON.convertAnother(JSON.java:162)
at grails.converters.JSON.value(JSON.java:199)
at grails.converters.JSON.convertAnother(JSON.java:162)
at grails.converters.JSON.value(JSON.java:199)
at grails.converters.JSON.convertAnother(JSON.java:162)
at grails.converters.JSON.value(JSON.java:199)
at grails.converters.JSON.render(JSON.java:134)
at grails.converters.JSON.render(JSON.java:150)
at xxx.xxxx.xxxx.SearchController.autocomplete(SearchController.groovy:514)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.reflect.InvocationTargetException
... 13 more
Caused by: java.lang.NullPointerException
at org.elasticsearch.common.compress.lzf.LZFCompressor.isCompressed(LZFCompressor.java:76)
at org.elasticsearch.common.compress.CompressorFactory.compressor(CompressorFactory.java:124)
at org.elasticsearch.common.compress.CompressorFactory.uncompressIfNeeded(CompressorFactory.java:174)
at org.elasticsearch.search.internal.InternalSearchHit.sourceRef(InternalSearchHit.java:172)
at org.elasticsearch.search.internal.InternalSearchHit.getSourceRef(InternalSearchHit.java:181)
... 13 more
Where the code fails:
try {
log.debug("result stuff is: ${results}");
render results as JSON
}
catch ( Exception e ) {
log.error("Problem...",e);
}
This was due to a slight change in ES structure, had to rewrite my conversion method :)
I'm using a fresh ActiveMQ 5.8.0 installation, where I have a message in a queue called 'testing'. I also replaced the ACTIVEMQ_SUNJMX line in bin/activemq to enable JMX:
ACTIVEMQ_SUNJMX_START="-Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"
Then I query JMX via the following code:
try {
JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi"));
connector.connect();
MBeanServerConnection connection = connector.getMBeanServerConnection();
ObjectName mbeanName = new ObjectName("org.apache.activemq:BrokerName=localhost,Type=Broker");
BrokerViewMBean mbean = MBeanServerInvocationHandler.newProxyInstance(connection, mbeanName, BrokerViewMBean.class, true);
System.out.println("Id:" + mbean.getBrokerId()); // here the exception will be thrown
}
catch (Exception x) {
x.printStackTrace();
}
The Exception is thrown when accessing the mbean.
java.lang.reflect.UndeclaredThrowableException
at $Proxy0.getBrokerId(Unknown Source)
at testing.TestJmx.main(TestJmx.java:25)
Caused by: javax.management.InstanceNotFoundException: org.apache.activemq:BrokerName=localhost,Type=Broker
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1095)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(DefaultMBeanServerInterceptor.java:643)
at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(JmxMBeanServer.java:668)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1424)
at javax.management.remote.rmi.RMIConnectionImpl.access$200(RMIConnectionImpl.java:89)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1292)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1380)
at javax.management.remote.rmi.RMIConnectionImpl.getAttribute(RMIConnectionImpl.java:621)
at sun.reflect.GeneratedMethodAccessor30.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
at sun.rmi.transport.Transport$1.run(Transport.java:177)
at sun.rmi.transport.Transport$1.run(Transport.java:174)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:273)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:251)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:160)
at com.sun.jmx.remote.internal.PRef.invoke(Unknown Source)
at javax.management.remote.rmi.RMIConnectionImpl_Stub.getAttribute(Unknown Source)
at javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection.getAttribute(RMIConnector.java:901)
at javax.management.MBeanServerInvocationHandler.invoke(MBeanServerInvocationHandler.java:280)
... 2 more
I checked that that port is listening, VisualVM also shows me the mbeans, ideas?
The fact that bean names have changed is documented on the 5.8.0 release page.
So your ObjectName should be of the form:
ObjectName mbeanName = new ObjectName("org.apache.activemq:type=Broker,brokerName=localhost");
Excellent answer. Here you have an example of use: API Docs
There is also something important that changes from 5.7 to 5.8. The default URL for JMX services goes from:
service:jmx:rmi://localhost/jndi/rmi://localhost:1099/jmxrmi
to:
service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
Lovely question and answer too, I was having a hard time configuring JMX. ActiveMQ docs isn't up-to-date in this regard. Eg : http://activemq.apache.org/jmx.html doesn't say that from 5.8.0 onwards "SUNJMX" becomes "ACTIVEMQ_SUNJMX_START".
The config provided by OP , ie :
ACTIVEMQ_SUNJMX_START="-Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"
, and the corresponding code is the one that really worked for me. Thanks ! ( I am using ActiveMQ 5.8)