How Apache Mina SignalListner works? - java

I want to terminate the Apache Mina Ssh Server on the basis of Ctrl+c that are basically SigInt I searched on the google and have looked there are SignalListener but not find any good example of it.
Please share any good example and use in ssh server.
A listener will only trigger if any Sigint or a Signal is sent, Am I right ?

To get access to signal handling you have to use sun's private classes, which makes your code not portable any mode. Anyway...
import sun.misc.Signal;
public static void main(String[] args) {
registerSignalHandler();
}
#SuppressWarnings("restriction")
private static void registerSignalHandler() {
Signal.handle(new Signal("HUP"), (Signal signal) -> {
System.out.println("Hello signal!");
});
}

Related

Hawkular And Vertx: Why the new installed Hawkular Metric Server does not receive any metrics?

my goal is to visualize vertx metrics - like for example the counts of messages, which are sent over eventbus and so on.
(please consider thath the following tools are all together on my localhost, nothing resides on separated machines)
Therefor I have used the Hawkular implementation of vertx: http://vertx.io/docs/vertx-hawkular-metrics/java/
First I installed CassandraDB with default settings:
http://www.planetcassandra.org/cassandra/
To build the Hawkular Metrics-alone Server I took an WildFly 10 and deployed the Metrics-warFile from here: https://github.com/hawkular/hawkular-metrics/releases/
When I now start the wildfly server I got under the url :http://localhost:8080/hawkular/metrics the starter screen with "Metrics Service started".
Until now all seem to be running fine....
Now I want to insert metric data - for that i programmed an short verticle with HawkularAPI:
public class Sender extends AbstractVerticle {
public static void main(String[] args) {
VertxOptions options = new VertxOptions();
VertxHawkularOptions hawkularOptions = new VertxHawkularOptions().setEnabled(true).setTenant("hawkular").setHost("localhost").setPort(8080);
options.setMetricsOptions(hawkularOptions);
Vertx.clusteredVertx(options, res -> {
Vertx vertx = res.result();
vertx.deployVerticle(new Sender());
});
}
#Override
public void start() throws Exception {
vertx.setPeriodic(1500, id -> {
vertx.eventBus().send("test", "testSend");
System.out.println("SEND!");
});
}
}
But nothing happens - have i missed something? My opinion was that I perhaps can see some charts for example by hitting :http://localhost:8080/hawkular/metrics /counter/vertx etc....instead of graphics I get following ErrorMessage:
{"errorMsg":"Tenant is not specified. Use 'Hawkular-Tenant' header."}
Hawkular Metrics does not provide charts. You need to start a graphical client like Grafana.
See http://www.hawkular.org/hawkular-clients/grafana/docs/quickstart-guide/

How to unblock an app blocked due to publishing events on rabbitmq server having disk space issues?

I am using rabbitmq java client to publish events from my java application.
When the server goes out of space, all the publish messages are blocked.
In my application, I am ok with events not being published but, I want the application to continue to work. To that end I looked at the documentation and found BlockedListener on the connection[1]
I was able to use this. But, the problem is unless I throw a runtime exception in the handleBlocked method, my app/publisher is still blocked. I tried to close/abort the connection but, nothing worked.
Is there any other graceful way to unblock my app
This is my BlockedListener code
private class BlockedConnectionHandler implements BlockedListener {
#Override
public void handleBlocked(String reason) throws IOException {
s_logger.error("rabbitmq connection is blocked with reason: " + reason);
throw new RuntimeException("unblocking the parent thread");
}
#Override
public void handleUnblocked() throws IOException {
s_logger.info("rabbitmq connection in unblocked");
}
}
[1] https://www.rabbitmq.com/connection-blocked.html

Apache Camel, send message when server starts and stops

I have a simply camel MINA server using the JAVA DSL, and I am running like the example documented here:
Running Camel standalone and have it keep running in JAVA
MINA 2 Component
Currently this server receives reports from a queue, updates them, and then sends them away to the next server. A very simple code:
public class MyApp_B {
private Main main;
public static void main(String... args) throws Exception {
MyApp_B loadbalancer = new MyApp_B();
loadbalancer.boot();
}
public void boot() throws Exception {
main = new Main();
main.enableHangupSupport();
main.addRouteBuilder(
new RouteBuilder(){
#Override
public void configure() throws Exception {
from("mina:tcp://localhost:9991")
.setHeader("minaServer", constant("localhost:9991"))
.beanRef("service.Reporting", "updateReport")
.to("direct:messageSender1");
from("direct:messageSender1")
.to("mina:tcp://localhost:9993")
.log("${body}");
}
}
);
System.out.println("Starting Camel MyApp_B. Use ctrl + c to terminate the JVM.\n");
main.run();
}
}
Now, I would like to know if it is possible to do two things:
Make this server send a message to a master server when it starts running. This is an "Hello" message with this server's information basically.
Tell the master server to forget him when I shut it down pressing CTRL+C or doing something else.
I have also read this:
http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/support/ServiceSupport.html#doStart%28%29
technically, by overriding the doStart and doStop methods I should get the intended behavior, however, those methods (specially the doStop method) don't work at all.
Is there a way to do this ? If yes how? If not, what are my options?
Thanks in advance, Pedro.
The code does work properly after all. The problem is my IDE, Eclipse. When using the Terminate button, Eclipse simply kills the process instead of send the CTRL+C signal to it. Furthermore it looks like Eclipse has no way of being able to send a CTRL+C signal to a process running on its console.
I have also created a discussion on Eclipse's official forums:
http://www.eclipse.org/forums/index.php/m/1176961/#msg_1176961
And may it some day help some one in a situation similar to mine.

How can I poll FTP location to trigger the changes in it?

I am trying to poll the ftp location.
I'm using Jenkins for continuous integration of the projects.So, it would be helpful if anyone can suggest me with a plugin in Jenkins or any other method to watch over the changes in FTP location.
I need to monitor the changes in FTP location and as the changes are found I have to build another job.
Not sure how to do it with Jenkins, but if you want to monitor an FTP location for changes (i.e. receive notifications when files added/removed/modified in a directory) using plain java, then the following library can help you with the actual polling/notification mechanism: https://github.com/drapostolos/rdp4j (Remote Directory Poller for Java).
Simple usage example of the API:
package example
import java.util.concurrent.TimeUnit;
import com.github.drapostolos.rdp4j.DirectoryPoller;
import com.github.drapostolos.rdp4j.spi.PolledDirectory;
public class FtpExample {
public static void main(String[] args) throws Exception {
String host = "ftp.mozilla.org";
String workingDirectory = "pub/addons";
String username = "anonymous";
String password = "anonymous";
PolledDirectory polledDirectory = new FtpDirectory(host, workingDirectory, username, password);
DirectoryPoller dp = DirectoryPoller.newBuilder()
.addPolledDirectory(polledDirectory)
.addListener(new MyListener())
.setPollingInterval(10, TimeUnit.MINUTES)
.start();
TimeUnit.HOURS.sleep(2);
dp.stop();
}
}
The RDP4J User Guide:
provides an example of FtpDirectory class which lists files in an FTP location using appache commons FTPClient
describes what events MyListener can listen for
How to configure the DirectoryPoller
Not sure how you can achieve this in Jenkins. If I were to just answer monitoring the FTP location part here is how you can do this.
Determine what programming language you want to use. (Java, .NET etc). Write code to
monitor the FTP server (assuming it is a specific remote directory you want to monitor)
and execute the job that needs to be executed. Both the monitoring and the executing the
job needs to be done in the programming language.
I am also assuming that you need a timer of some sort to do the monitoring, this can
also be done using a programming language such as Java.

Signal handling using "TERM"

I have a standalone application in which I have to prompt the user with an confirm dialog box to save the changes made by him when he tries to shutdown the system by start-->shutdown.
I came to know that by using signalhandlers we can do it.
Can some one help me how to use signal handlers
Update May 2012 (2 and half years later)
Trejkaz comments:
On current versions of Java this signal handling code fails because the "INT" signal is "reserved by the VM or the OS".
Additionally, none of the other valid signal names actually fire when something requests the application to close (I just painstakingly tested all of the ones I could find out about...)
The shutdown hook mostly works but we find that in our case it isn't firing, so the next step is obviously to resort to registering a handler behind the JVM's back
The chapter "Integrating Signal and Exception Handling" of the "Troubleshooting Guide for HotSpot VM" mentions the signals "SIGTERM, SIGINT, SIGHUP" only for Solaris OS and Linux.
Only Exception Handling on Windows are mentioned.
Original answer (Sept 2009)
a ShutdownHook should be able to handle that case
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
// what you want to do
}
}));
(with caveats)
See also:
Java signal handling and termination (link is dead, see archive or mirror)
java exit signal handling:
as an illustration of simple signal handling:
public class Aaarggh {
public static void main(String[] args) throws Exception {
Signal.handle(new Signal("INT"), new SignalHandler () {
public void handle(Signal sig) {
System.out.println(
"Aaarggh, a user is trying to interrupt me!!");
System.out.println(
"(throw garlic at user, say `shoo, go away')");
}
});
for(int i=0; i<100; i++) {
Thread.sleep(1000);
System.out.print('.');
}
}
}

Categories