Not able to enable JMX for Grizzly - java

I tried enabling the JMX for grizzly server. I added the gmbal-api-only-3.1.0 jar file to the project and wrote the following simple code :
public class Main {
public static void main(String[] args) {
HttpServer gws = new HttpServer();
NetworkListener listener1 = new NetworkListener("listener1", "localhost", 19080);
gws.addListener(listener1);
try {
gws.start();
gws.getServerConfiguration().setJmxEnabled(true);
System.in.read();
} catch (IOException ioe) {
ioe.printStackTrace();
System.exit(1);
} finally {
try {
gws.stop();
} catch (Exception ignored) {}
}
}
}
I ran this program and opened it up in the JConsole, but the JConsole do not show any MBean for the grizzly server in spite of using setJMXEnabled(true).
Please tell me what could be missing here or what is wrong with the code? Else, please suggest how to enable JMX for grizzly and how to verify it. I tried using the "Grizzly HTTP JMX Server Monitoring" approach given on the link : https://grizzly.java.net/monitoring.html

Related

Create InitialContext from context.xml in plain java application

As the question implies, I have a simple java app (aka a "simple main") that needs to initialize a hibernate connection whose information is in context.xml.
After lots of searching and with some hacks from here I concluded to this:
public static void main(String[] args) {
//JNDI provider is needed and RMI registry has one...
try {
java.rmi.registry.LocateRegistry.createRegistry(1099);
System.out.println("RMI registry ready.");
} catch (Exception e) {
System.out.println("Exception starting RMI registry:");
e.printStackTrace();
}
new InitialContext().readContextXml("context.xml");//of course there is nothing like that. But is there an equivalent?
String result = thatWillConnectWithHibernate();
System.out.println(result);
}
private static String thatWillConnectWithHibernate() {
//does stuff
}
So is there a simple way to create an InitialContext from an xml file? I'm not interested in parsing the file manually, I can do that my self.

how to run java tcp server in window azure?

how to run java tcp server in window azure?
can window azure do it?
I find so many article about java application for window azure,they is that open a JSP web project in eclipse, and than use worker role publish it in window azure, but my tcp server is general java project, so how to publish it to window azure?
my tcp server:
public class test {
private static int serverport = 12345;
private static ServerSocket serverSocket;
public static void main(String[] args) {
try {
serverSocket = new ServerSocket(serverport);
System.out.println("Server is start.");
while (!serverSocket.isClosed()) {
System.out.println("Wait new clinet connect!");
waitNewPlayer();
}
} catch (IOException e) {
System.out.println("Server Socket ERROR");
}
}
public static void waitNewPlayer() {
try {
Socket socket = serverSocket.accept();
System.out.println(socket.getInetAddress().getHostAddress()+"'s socket is connected now!");
createNewUser(socket);
} catch (IOException e) {
}
}
public static void createNewUser(final Socket socket) {
Thread t = new Thread(new Runnable() {
#Override
public void run() {
try {
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
out.println("nangnang");
} catch (IOException e) {
System.out.println("Socket is closed!");
}
System.out.println("This socket is removed form the player array!");
}
});
t.start();
}
}
You should be able to run an app like this in Azure, but you'll need to take care of a couple of things:
Open an Input Endpoint for your worker role - this opens the port to the outside world. You then need to either map it to the exact same port internally, or let Azure pick a port for you (and then you can ask the role environment which port you've been assigned, and open that port in your code instead of 12345)
For all your println's, you'd need to remote-desktop to see them, or you need to push them to diagnostics logging so you can see those debug statements via an external tool like Cerebrata's Diagnostics Manager.
As far as publishing: It's the same as the jsp examples you've seen: you build an Azure project to go along with your Java project, you set up the role size and instance count, create input endpoints, optionally create a cache, set up configuration settings for storage accounts, create a package to run in emulator or in the cloud, etc.
You might also want to try AzureRunMe which also supports Azure Java Project. http://azurerunme.codeplex.com/

How to create a "FTPS" Mock Server to unit test File Transfer in Java

I have a CreateFTPConnection class which create a FTPS connection. Using this connection, files are transferred. Here is the code of TransferFile class
public class TransferFile
{
private CreateFTPConnection ftpConnection;
private FTPSClient client;
public TransferFile(CreateFTPConnection ftpConnection) {
this.ftpConnection = ftpConnection;
this.client = ftpConnection.getClient();
}
public void transfer(Message<?> msg)
{
InputStream inputStream = null;
try
{
if(!client.isConnected()){
ftpConnection.init();
client = ftpConnection.getClient();
}
File file = (File) msg.getPayload();
inputStream = new FileInputStream(file);
client.storeFile(file.getName(), inputStream);
client.sendNoOp();
} catch (Exception e) {
try
{
client.disconnect();
}
catch (IOException e1) {
e1.printStackTrace();
}
}
finally
{
try {
inputStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
I have to write jUnit Testcase for this class. For this, I have to create a FTPS Mock Server connection and have to use that connection to test the File Transfer. So can anyone plz give me any idea of how to make FTPS Mock Server and do the test case. I googled on this, but what I get is on FTP or SFTP, not FTPS. Please help me.
You might find this useful MockFTPServer
The issue is that these mock servers don't implement the TLS portion from what I can see. You may need to do a little work to allow connections via TLS.
You should be able to search around and find some articles here on SO about dealing with certificates, (or in some cases, bypassing them) for the sake of your testing.
Here's another Article that goes through the steps of creating a basic FTP server Test.
Short of a full blown FTP server (Apache http w/ mod_ftp add on), there doesn't seem to be anything useful to do this.

Running selenium 2 tests within Jenkins against a certain selenium instance

I'm trying to run headless tests from Jenkins. This works fine for HTML tests when I specify the HTML test suite. But now I want to run selenium-2 tests against the same selenium server.
I tried this:
Execute shell:
export DISPLAY=":99" && java -jar /var/lib/selenium/selenium-server.jar
But this seems to be hang until I stopped the server manually. How do I start the selenium server in such a way that my selenium RC tests invoked through grails ?
There is no special method to "start" selenium server to be used by any particular language. When you start selenium server it will start listening on a port for incoming requests. You should be having a line of code inside your tests to point your tests to the selenium server. I don't know grails. In java it would be
Selenium sel = new DefaultSelenium("host","port","browsername","baseurl")
> host - IP of the machine where server is started
> port - port number on which selenium server is listening. This is
usually 4444 if you don't specify anything
> browsername-Browser on which you want the tests to be
> run baseURL- base URL of the web app you need to test.
The equivalent method for this in grails should get you working.
EDIT - JAVA code to start selenium server:
Selenium sel;
int port=9999;
public static SeleniumServer server;
public void startSeleniumServer() throws Exception {
try {
ServerSocket serverSocket = new ServerSocket(port);
serverSocket.close();
//Server not up, start it
try {
RemoteControlConfiguration rcc = new RemoteControlConfiguration();
rcc.setPort(port);
server = new SeleniumServer(false, rcc);
} catch (Exception e) {
System.err.println("Could not create Selenium Server because of: "
+ e.getMessage());
e.printStackTrace();
}
try {
server.start();
System.out.println("Server started");
} catch (Exception e) {
System.err.println("Could not start Selenium Server because of: "
+ e.getMessage());
e.printStackTrace();
}
} catch (BindException e) {
System.out.println("Selenium server already up, will reuse...");
}
}
public void stopSeleniumServer(){
if (server != null)
{
try
{
server.stop();
}
catch (Exception e)
{
e.printStackTrace();
}
}
System.out.println("Selenium server stopped..");
}
public void startSeleniumRC() throws Exception{
sel=new DefaultSelenium("localhost",
port,
"*firefox",
"http://www.google.com");
sel.start();
}
public void stopSeleniumRC()
{
sel.shutDownSeleniumServer();
}

Stopping Running Server with Java Code?

I want to stop a server running on port 8080. In my java application, whenever application is closed, also this server needs to be stopped. But I could not find any solution except stopping the server manually. Is there any way to stop a server with codes ? By the way, I am using Windows 7
How are you starting SymmetricDs? As a windows service, as a WAR or embedded in you application?
Looking at the user guide it seems that if you could embed it in your code you ought to be able to start and stop it directly. More details in the user guide along with the following example code.
import org.jumpmind.symmetric.SymmetricWebServer;
public class StartSymmetricEngine {
/**
* Start an engine that is configured by two properties files. One is
* packaged with the application and contains overridden properties that are
* specific to the application. The other is found in the application's
* working directory. It can be used to setup environment specific
* properties.
*/
public static void main(String[] args) throws Exception {
SymmetricWebServer node = new SymmetricWebServer(
"classpath://my-application.properties");
// this will create the database, sync triggers, start jobs running
node.start(8080);
// this will stop the node
node.stop();
}
}
try {
// Execute a command to terminate your server
String command = "command to stop your server";
Process child = Runtime.getRuntime().exec(command);
} catch (IOException e) {
}
Why don't you check Cargo? It provides a Java API to start/stop Java containers. You can find the list of supported containers in the home page.
Since i did not see the code , how are creating server and accepting connection ,below
i have given you the following ooptions.you should try to implement the firstway ,
rest of the option wont guarantee whether correct process will be killed.
public void stopServer()
{
threadReference.interrupt();
}
while(!Thread.interrupted())
{
// Accept Server Connection
try
{
Thread.sleep(1000);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
Runtime.getRunTime().addShutDownHook(new Thread()
{
public void run()
{
try
{
ref.stop();
} catch (IOException e)
{
// close server socket
// other clean up
e.printStackTrace();
}
}
});
you need to specify this in Runtime.getRuntime() TASKKILL /F /IM or there is a jps which is better to kill relevant process.
try
{
Process p = Runtime.getRuntime().exec("TASKKILL /F /IM communicator*");
BufferedReader in =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String result = null;
while ((result= in.readLine()) != null) {
if ( "SUCCESS".equals(result.substring(0,7))
{
break;
}
}
} catch (IOException e)
{
e.printStackTrace();
}

Categories