I am trying to test Cassandra with JUnit External resources. I have two classes CassandraExternalResource and TestCassandra. Here is snippet from TestCassandra:
public class TestCassandra {
#Rule
public CassandraExternalResource cassandraExternalResource = new CassandraExternalResource();
#Test
public void test() throws InterruptedException {
System.out.println("During Test");
}
}
In before method of CassandraExternalResource I start Cassandra Server with a separate thread. What I want is to make test cases wait until cassandra server is up and running.
Since server takes some time to start, test cases start to run before server is ready.
Any help is appreciated.
You can extend the rule's before method by pinging the cassandra server until it is ready.
Related
I am trying to start a mock a server from Java and keep it running to receive incoming requests from other sources (Postman, CURL, etc).
I have tried the Junit approach, but when the unit tests finishes, the server is shutdown.
On the other hand, running the standalone version
http://www.mock-server.com/mock_server/running_mock_server.html#running_from_command_line
keeps the mock server running.
I would like to achieve the same thing, but from the Java code.
The question is, how may I make it run and stay running?
Thanks
So you need an HTTP server for non-testing purposes? I'd try with Spring, something like:
#RestController
public class CatchAllController {
#RequestMapping("/**")
public String catchAll(HttpServletRequest request) {
return request.getRequestURI();
}
}
There is an example on that same page (paragraph "Client API - starting and stopping"). This code works for me:
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
import org.mockserver.integration.ClientAndServer;
public class Checker {
public static void main(String[] args) {
ClientAndServer mockServer = startClientAndServer(1080);
}
}
You have to call
mockServer.stop();
later to stop it.
You will need the following maven dependency:
<!-- mockserver -->
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
<version>5.5.1</version>
</dependency>
I have written a small spring-boot application without embedded servers. It is intended to run from command line and stay running until the VM gets signalled. What is the intended way of in the spring-boot framework (v2.0) to keep the application alive as a service? Should I have a Thread.currentThread().wait(); as last statement in my run(ApplicationArguments args) method? Is there an enabling annotation?
from org.springframework.boot.web.embedded.netty.NettyWebServer, Official.
private void startDaemonAwaitThread(DisposableServer disposableServer) {
Thread awaitThread = new Thread("server") {
#Override
public void run() {
disposableServer.onDispose().block();
}
};
awaitThread.setContextClassLoader(getClass().getClassLoader());
awaitThread.setDaemon(false);
awaitThread.start();
}
I want to able to have a client/server application which can perform the following:
Have client send "java task instructions" (ex. name of method, class to run or app to execute) to a remote server
server receives the task execution details and launches a new JVM and loads the classpath etc...
server executes the task
server stores the task response in a cache
shuts down the new JVM
server continues to wait for further executable task details which could reference object in cache by name
What is the best APIs to implement this in Java if I don't want to use RMI?
Do I want to establish a connection by using a socket or is there an easier API?
From what I know, correct me if im wrong, I cannot use ProcessBuilder to execute a static method or to get a response.
thanks in advance
UPDATE
I am new to threads and RMI, so how would I then make it so that when client gets the factory stub to creates a new object which will contain the behavior actually needed to execute a task on the server, that the object code is executing in its own thread?
I want this behavior so that two different clients are executing two different tasks at the same time in different threads without reusing the same remote object instance. Here is some simple sample code to help explain what I want
Factor Interface
public interface Factory extends Remote
{
public Executor create() throws RemoteException;
}
Factory implementation
public class FactoryImpl implements Factory
{
public FactoryImpl()
{}
#Override
public Executor create() throws RemoteException
{
//create a new executor in another thread
}
}
Executor interface
public interface Executor extends Runnable, Remote
{
public String executeTask(String pClass, String pMethod, List<String> arguments) throws RemoteException;
}
Do I just want to create/start a new Runnable class that instantiates the executor object?
I have a simple API that my clients use in a standalone application. Behind the scenes my API uses Ehcache for performance.
Everything works fine except that my client needs to invoke a shutdown() method in my API to invoke CacheManager.shutdown() without which Ehcache continues to run in the background even though the main thread is completed.
Is there a way I can avoid this extra shutdown() call for my client?
I tried using #PreDestroy Spring annotation to invoke this call, but it didn't work?
Here I am adding a sample client program.
public class ClientApp{
#Autowired
private IClientService service;
public static void main(String[] args){
try{
service.getClients();
...
} finally {
service.shutdown(); // to shutdown the cache background thread
}
}
}
In ClientServiceImpl.java, I have the following lines
public void shutdown(){
LOGGER.info("Shutting the cache down...");
ehcacheManager.shutdown();
}
Your example confirms the standalone application setup.
Ehcache should not prevent the JVM from shutting down when the main thread terminates.
If it does, you will need to add thread dumps to this issue so we can analyse further the issue and its cause.
Adding the following line does what I was looking for.
System.setProperty(CacheManager.ENABLE_SHUTDOWN_HOOK_PROPERTY, "true");
I'm new to Vert.x and I'm a little bit confused with how to run\deploy Vert.x app.
I'm used to write a server by having a main class with static main() method which there performs all my initial startup code, like: connecting to DB, reading configuration files, initializing internal services and eventually creating the listening socket for accepting new connections.
For example:
public class Server {
public static void main(String args[]) {
Server server = new Server();
server.run();
}
public void run() {
// load configuration
....
// Connect to DB
....
// Initialize internal services
....
// Create listening socket on server port
...
// and more...
}
now to my question:
Vert.x forces me to make my main class extends Verticle class and override start() method
to initialize the listening socket.
so now all my initialization process must be done inside this start() method.
Does this make sense??
and I can never run my application via command line like I'm used to but rather use the "vertex" app
Am I missing something??
Yes, you are correct. A vertx app is nothing but a set of verticles running inside vertx instances.
If you want your app to have main method as usual then you can use vertx as embedded mode i.e inside your main method you start a vertx instance using the API and then start verticles inside that instance.
Check out embedding guide at: https://vertx.io/vertx2/embedding_manual.html