I want to automatically invoke the Karaf "dev:watch" command if I detect that I'm running in a dev environment. I've considered adding dev:watch * directly to etc/shell.init.script but I don't want it to run unconditionally. So, I'm considering creating a simple service that checks a Java property (something simple like -Ddevelopment=true) and invokes org.apache.karaf.shell.dev.Watch itself. I think I can ask OSGi for a Function instance with (&(osgi.command.function=watch)(osgi.command.scope=dev)) but then I need to create a mock CommandSession just to invoke it. That just seems too complicated. Is there a better approach?
Since Apache Karaf 3.0.0 most commands are backed by OSGi services.
So for example the bundle:watch command is using the service
"org.apache.karaf.bundle.core.BundleWatcher".
So just bind this service and you can call the bundle:watch functionality very conveniently.
It has been a while since the question, but I will answer.
You need to use CommandSession class, it is not trivial. This blog post could guide you. It is related with Pax Exam, but could be applied in any situation. There are more alternatives like using a remote SSH client or even better the remote JXM management console (reference).
The init script can also be used to test for a condition and run a command if that condition is satisfied, so there is no need to create a command session yourself.
The Karaf source itself reveals one answer:
In the KarafTestSupport class which is used for integration testing Karaf itself
(see https://git-wip-us.apache.org/repos/asf?p=karaf.git;a=blob;f=itests/src/test/java/org/apache/karaf/itests/KarafTestSupport.java;h=ebdea09ae8c6d926c8e4ac1fae6672f2c00a53dc;hb=HEAD)
The relevant method starts:
/**
* Executes a shell command and returns output as a String.
* Commands have a default timeout of 10 seconds.
*
* #param command The command to execute.
* #param timeout The amount of time in millis to wait for the command to execute.
* #param silent Specifies if the command should be displayed in the screen.
* #param principals The principals (e.g. RolePrincipal objects) to run the command under
* #return
*/
protected String executeCommand(final String command, final Long timeout, final Boolean silent, final Principal ... principals) {
waitForCommandService(command);
String response;
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final PrintStream printStream = new PrintStream(byteArrayOutputStream);
final SessionFactory sessionFactory = getOsgiService(SessionFactory.class);
final Session session = sessionFactory.create(System.in, printStream, System.err);
//
//
//
// Snip
Related
I'm developing system/integration tests based on Testcontainers solution. It is required for me to use our own database PostgreSQL image with already applied database schema.
For this reason I'm using Testcontainers GenericContainer.
private static final GenericContainer postgresDb = new GenericContainer(POSTGRES_IMAGE).withExposedPorts(5432);
I'm developing tests with Spring Boot, so for this reason I've created abstract class which will hold this config for all tests
#ActiveProfiles("test")
#SpringBootTest
public abstract class AbstractTests {
private static final DockerImageName POSTGRES_IMAGE = DockerImageName.parse("docker-name:latest");
private static final GenericContainer postgresDb;
static {
postgresDb = new GenericContainer(POSTGRES_IMAGE)
.withExposedPorts(5432);
postgresDb.withStartupTimeout(Duration.ofSeconds(30))
.start();
}
#DynamicPropertySource
static void properties(DynamicPropertyRegistry registry) throws InterruptedException {
final String s = "jdbc:postgresql://"+ postgresDb.getHost() +":"+ postgresDb.getMappedPort(5432) + "/test";
registry.add("spring.datasource.url", () ->s);
}
}
However, the problem is that when when tests are running, container still is starting. This withStartupTimeout(Duration.ofSeconds(30)) doesn't for work some reason.
When I stop in debug at properties method and give a couple of seconds to start a container all tests run fine.
When tests fail I see next log:
org.postgresql.util.PSQLException: FATAL: the database system is starting up
If I put Thread.sleep(..) it also works, it is not preferable solution.
What is the right solution to wait or right strategy to know that container is ready?
I think the answer is in their documentation, particularly this part:
Log output Wait Strategy
In some situations a container's log output is a simple way to determine if it is ready or not. For example, we can wait for a `Ready' message in the container's logs as follows:
public GenericContainer containerWithLogWait = new GenericContainer(DockerImageName.parse("redis:5.0.3"))
.withExposedPorts(6379)
.waitingFor(
Wait.forLogMessage(".*Ready to accept connections.*\\n", 1)
);
Note: you will want to change the message to something like:
".*database system is ready to accept connections.*"
I have a Spring Boot project, built using Maven, where I intend to use embedded mongo db. I am using Eclipse on Windows 7.
I am behind a proxy that uses automatic configuration script, as I have observed in the Connection tab of Internet Options.
I am getting the following exception when I try to run the application.
java.io.IOException: Could not open inputStream for https://downloads.mongodb.org/win32/mongodb-win32-i386-3.2.2.zip
at de.flapdoodle.embed.process.store.Downloader.downloadInputStream(Downloader.java:131) ~[de.flapdoodle.embed.process-2.0.1.jar:na]
at de.flapdoodle.embed.process.store.Downloader.download(Downloader.java:69) ~[de.flapdoodle.embed.process-2.0.1.jar:na]
....
MongoDB gets downloaded just fine, when I hit the following URL in my web browser:
https://downloads.mongodb.org/win32/mongodb-win32-i386-3.2.2.zip
This leads me to believe that probably I'm missing some configuration in my Eclipse or may be the maven project itself.
Please help me to find the right configuration.
What worked for me on a windows machine:
Download the zip file (https://downloads.mongodb.org/win32/mongodb-win32-i386-3.2.2.zip)
manually and put it (not unpack) into this folder:
C:\Users\<Username>\.embedmongo\win32\
Indeed the problem is about your proxy (a corporate one I guess).
If the proxy do not require authentication, you can solve your problem easily just by adding the appropriate -Dhttp.proxyHost=... and -Dhttp.proxyPort=... (or/and the same with "https.[...]") as JVM arguments in your eclipse junit Runner, as suggested here : https://github.com/learning-spring-boot/learning-spring-boot-2nd-edition-code/issues/2
One solution to your problem is to do the following.
Download MongoDB and place it on a ftp server which is inside your corporate network (for which you would not need proxy).
Then write a configuration in your project like this
#Bean
#ConditionalOnProperty("mongo.proxy")
public IRuntimeConfig embeddedMongoRuntimeConfig() {
final Command command = Command.MongoD;
final IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
.defaults(command)
.artifactStore(new ExtractedArtifactStoreBuilder()
.defaults(command)
.download(new DownloadConfigBuilder()
.defaultsForCommand(command)
.downloadPath("your-ftp-path")
.build())
.build())
.build();
return runtimeConfig;
}
With the property mongo.proxy you can control whether Spring Boot downloads MongoDB from your ftp server or from outside. If it is set to true then it downloads from the ftp server. If not then it tries to download from the internet.
The easiest way seems to me to customize the default configuration:
#Bean
DownloadConfigBuilderCustomizer mongoProxyCustomizer() {
return configBuilder -> {
configBuilder.proxyFactory(new HttpProxyFactory(host, port));
};
}
Got the same issue (with Spring Boot 2.6.1 the spring.mongodb.embedded.version property is mandatory).
To configure the proxy, I've added the configuration bean by myself:
#Value("${spring.mongodb.embedded.proxy.domain}")
private String proxyDomain;
#Value("${spring.mongodb.embedded.proxy.port}")
private Integer proxyPort;
#Bean
RuntimeConfig embeddedMongoRuntimeConfig(ObjectProvider<DownloadConfigBuilderCustomizer> downloadConfigBuilderCustomizers) {
Logger logger = LoggerFactory.getLogger(this.getClass().getPackage().getName() + ".EmbeddedMongo");
ProcessOutput processOutput = new ProcessOutput(Processors.logTo(logger, Slf4jLevel.INFO), Processors.logTo(logger, Slf4jLevel.ERROR), Processors.named("[console>]", Processors.logTo(logger, Slf4jLevel.DEBUG)));
return Defaults.runtimeConfigFor(Command.MongoD, logger).processOutput(processOutput).artifactStore(this.getArtifactStore(logger, downloadConfigBuilderCustomizers.orderedStream())).isDaemonProcess(false).build();
}
private ExtractedArtifactStore getArtifactStore(Logger logger, Stream<DownloadConfigBuilderCustomizer> downloadConfigBuilderCustomizers) {
de.flapdoodle.embed.process.config.store.ImmutableDownloadConfig.Builder downloadConfigBuilder = Defaults.downloadConfigFor(Command.MongoD);
downloadConfigBuilder.progressListener(new Slf4jProgressListener(logger));
downloadConfigBuilderCustomizers.forEach((customizer) -> {
customizer.customize(downloadConfigBuilder);
});
DownloadConfig downloadConfig = downloadConfigBuilder
.proxyFactory(new HttpProxyFactory(proxyDomain, proxyPort)) // <--- HERE
.build();
return Defaults.extractedArtifactStoreFor(Command.MongoD).withDownloadConfig(downloadConfig);
}
In my case, I had to add the HTTPS corporate proxy to Intellij Run Configuration.
Https because it was trying to download:
https://downloads.mongodb.org/win32/mongodb-win32-x86_64-4.0.2.zip
application.properties:
spring.data.mongodb.database=test
spring.data.mongodb.port=27017
spring.mongodb.embedded.version=4.0.2
Please keep in mind this is a (DEV) setup.
How to pass VertxOptions from command line (like worker threads)?
I'm talking about something like this:
java -jar fat.jar --workerThreads 40
or
vertx run server.js --workerThreads 40
There is no mention of that parameter in manual or API.
Is there any way to do this?
I know that there is an API:
var Vertx = require("vertx-js/vertx");
var vertx = Vertx.vertx({
"workerPoolSize" : 40
});
But when I use that API I get warning from Vertx:
You're already on a Vert.x context, are you sure you want to create a new Vertx instance?
So I thinking I am doing something wrong...
You need to put it as a system property with a vertx.options prefix.
So for the fat jar it would be:
java -jar fat.jar -Dvertx.options.workerThreads 40
As for what properties you can set, anything that has a setting in VertxOptions has a corresponding property name: the setter name without the "set" portion.
For example, in code:
options.setClusterPort(5555)
is equivalent to
-Dvertx.options.clusterPort
on the command line.
Be warned that the "vertx.options" part is case-sensitive.
I am new to java RMI, actually I wrote, compiled and started rmic, and also tried to start server but failed due to _stub 'ClassNotFound' exception..... I'm using java 7... I searched a lot on Google but nobody told step by step example that could work...I got some idea about stteing codebase and security policy but not very clear suggestion that how to do it.. please help telling me steps including command-line .......... please... I have everything just tell me how to start server, and required settings like codebase or policy settings etc... Thanks
You don't need to use rmic. Instead, create your server object and call one of the exportObject() method overloads that has the port parameter. For example,
MyRemoteIntf stub = UnicastRemoteObject.exportObject(server, 0);
This will cause RMI to generate the stub automatically. (The documentation is horribly unclear on this point. If you use the version without the port parameter, it will use only the old, rmic-generated stubs instead of generating them automatically.)
Also, make sure that your remote interface is in the codebase of both the registry and any clients. You'll get different errors if you haven't done this properly. This has been answered a bunch of times on Stackoverflow already; search for "rmi ClassNotFoundException".
There is no need to use the rmic command since java (J2SE) 5.0 the stubs are dynamically generated at runtime.
Here is a basic step by step example of how to use RMI.
First define the Remote interface that defines what the client can see and do:
public interface FooService extends Remote {
// Don't forget to add throws RemoteException.
public void bar() throws RemoteException;
}
NOTE: if you do not add throws RemoteException to the method declaration you will encounter the following exception:
java.lang.IllegalArgumentException: illegal remote method encountered: public abstract void RMIExample.FooService.bar()
After you defined what the client can do you must define the concrete implementation of the methods on the server slide:
public class FooServiceImpl extends UnicastRemoteObject implements FooService {
public FooServiceImpl() throws RemoteException {
super();
}
public void bar() {
System.out.println("I was remotely invoked!");
}
}
The implementation class must inherit from UnicastRemoteObject and implement the Remote interface you defined earlier and the class must have a constructor that throws RemoteException.
Now that you have fully defined the remote functionality you must bind the implementation object to a URL on the server side:
// 4000 is the port to listen on.
LocateRegistry.createRegistry(4000);
Naming.rebind("//127.0.0.1:4000/foobar", new FooServiceImpl());
Now that you have your server up and running you need a stub instance A.K.A a proxy in the client side:
FooService fooService =
(FooService)Naming.lookup("//127.0.0.1:4000/foobar");
and finally invoke the remote method:
fooService.bar();
the following should be printed on the server side:
I was remotely invoked!
References:
http://en.wikipedia.org/wiki/Java_remote_method_invocation
http://docs.oracle.com/javase/1.5.0/docs/guide/rmi/relnotes.html
I have successfully configured our app to export Quartz's MBeans into JMX and can view everything in JConsole. I can run the majority of the scheduler operations.
The one I really want to run is 'triggerJob', but that is showing up in JConsole as greyed-out/disabled so I can't run it.
I've scanned the commits that added the JMX code to Quartz but can't see any differences between triggerJob and the other operations that are enabled.
Anyone have a clue what's going on?
EDIT - explanation found
A different StackOverflow issue describes what's going on: Why are some methods on the JConsole disabled
triggerJob (and two other operations) take non-primitive parameters, these complex parameters cannot be provided in JConsole.
I am not clear if the MBean provider might provide a custom editor for JConsole (or simlar), but at least I have my answer.
Thank you for your explanation. I have successfully triggered a job remotely through JMX using the following Groovy code:
def callParams = new Object[3]
callParams[0] = 'com.test.project.TestJob'
callParams[1] = 'DEFAULT_JOB_GROUP'
callParams[2] = new HashMap()
def callSignature = new String[3]
callSignature[0] = 'java.lang.String'
callSignature[1] = 'java.lang.String'
callSignature[2] = 'java.util.Map'
// server is an instance of MBeanServerConnection
server.invoke('triggerJob', callParams, callSignature)