Remove Webservice from Windows machine - java

I am following a little tutorial for JAX-WS mkyong - jax-ws
I have published this little example with the following code on my Windows 7 machine.
But how can I update or remove this webservice?
public class HelloWorldPublisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:9999/ws/hello", new HelloWorldImpl());
}
}

Remove : If you want to remove the web service, just use the Endpoint.stop() (read here) methods for stopping it accept the requests.
Update : Just change the code in the HelloWorldImpl class. It will automaticaly call the newly updated code.
Endpoint.publish() simply tells the server that the requests in the given URL should be processed using HelloWorldImpl

Related

Converting a Java program with main method into a servlet

I am new to servlets. I have a query processor java program and now, I want to use it in a Web Application. I have an interface(HTML) which generates the query and I want to run the program on a button click in the interface. For this, I want to convert the java program into a java servlet. I am working in Net Beans.
Following is the structure of my Java program :
public class ABC
{
//code
public ABC() //constructor
{
//code
}
public static void main(String[] args)
{
//code
}
}
I want to convert this into a servlet. Following is the structure of a default servlet in Net Beans.
public class Demo extends Httpservlet
{
/*----
----
----
----*/
public void processRequest(HttpServletRequest request, Httpservlet response)
throws ServletException,IOException
{
/*code*/
}
/*HttpServlet methods - doGet(), doSet() etc.*/
}
Is there any alternative for the main function in the servlet? Which method is executed first when the sevlet starts running? Can I run the Java Program on a button click on a HTML page so that I can eliminate the use of servlet?
use get or post method in servlet depend on your action. There are doGet , doPost and so many HTTP methods you need to determine in which you write code
To use your query processor on the web you will have to build a Java Web Application.
Try the tutorial below and then call your ABC class from a Servlet.
Introduction to Developing Web Applications
Create a Dynamic web project, add new servlet usee doGet method or doPost method refer this link for the same.
servlet example
Hope this helps.
Please have in mind that the purpose of use is different in those two cases. While the main method of a class is invoked when you compile and run it as part of an application (run on a machine), the doGet and doPost methods are invoked in a servlet after a GET/POST request is made by client side to the server side, on which the Servlet lives.
On the first case, usually everything occurs on a specified machine, following the logic "do something, then done", and on the second case, you have a Request/Response Model between Clients and a Server (following the logic "do something when asked, then wait for being asked again"). You need to have a Server (e.g. Tomcat) set up to use the servlets.

Websocket through Servlet in tomcat

I am trying to implement a web socket through servlet. My app server is tomcat 7.
I could find examples, where WebSocketServlet class is used. But this class is deprecated and removed in tomcat 8.
I see another alternative, which is to annotate the class with the following
#ServerEndpoint(value = "/websocket/test")
I need helpin understanding,
How will I use this annotation in servlets? Are servlets irrelevent in case of web sockets?
If I create a normal class with the above annotation, and other annotation like onOpen,onClose etc, should I need to put the entry for
that class in web.xml? Or are web.xmls are irrelevant too?
Any hello world link will also be very helpful.
Thank you.
============Edited====================
I have tried the chat example found in this link
http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/
But when I try to invoke the socket through javascript, the events are not reaching my server at all....
Finally I figured this out. So I am answering here for others to refer.
1)How will I use this annotation in servlets? Are servlets irrelevent in case of web sockets?
Apparently yes, we don't need servlets for web sockets.
2)If I create a normal class with the above annotation, and other annotation like onOpen,onClose etc, should I need to put the entry for that class in web.xml? Or are web.xmls are irrelevant too?
No entry needed in web.xml either.
Following, is a sample server side code.
#ServerEndpoint(value = "/echo")
public class Echo {
#OnOpen
public void start(Session session) {
//TODO
}
#OnClose
public void end() {
//TODO
}
#OnMessage
public void incoming(String message) {
//TODO
}
#OnError
public void onError(Throwable t) throws Throwable {
//TODO
}
}
For client, either you can use Javascript is you have a HTML5 compatible browser.
Else you write java clients using the tyrus library. Refer here

Java web service client + Axis + Eclipse + Tomcat

I'm new to java web services.
Now i'm trying to create a web service client to access a WSDL based web service. So using eclipse i generated the required client stubs/Binding stubs/Port/Port proxy/ServiceLocator etc.
According to my understanding next step is to create a class with the main method to invoke it. Can anyone help me to write that piece of code or at least some links to refer?
EDITED
Thank you so much for the hint #pavan-kumar. Finally i come up with following code and it works. Thanks again.
package clients;
import requiredClasses;
public class TestClient {
public static void main(String args[]) throws Exception
{
TestPortProxy tProxy = new TestPortProxy();
RequestEntity rEntity = new RequestEntity();
rEntity.setAttribute1(100);
rEntity.setAttribute2("value1");
tProxy.webServiceAction(rEntity);
}
}
You already generated required client files with the WSDL ,so next create your own class in that create an object for proxy class which is generated by WSDL, by using that object you can call web service methods in your application.

Camel only sends the message on startup, then stops

I have hopefully a trivial problem. I wrote super short 'program' for Apache Camel to read the context.xml and then do as it is told:
public class CamelBridge {
public static void main(String[] args) throws Exception {
ApplicationContext context = new FileSystemXmlApplicationContext("camelContext.xml");
}
}
I connect between two JMS queues. The program works, but just when I start it. Then it stops sending messages. If I restart- it sends them all again. Is there something oviously wrong that I am missing here?
Edit:
I have updated my Main, but it does not help:
public class Bridge {
private Main main;
public static void main(String[] args) throws Exception {
Bridge bridge = new Bridge ();
bridge.boot();
}
public void boot() throws Exception{
main = new Main();
main.enableHangupSupport();
main.setApplicationContextUri("camelContext.xml");
main.run();
}
}
Edit 2
I think I found the issue (not the solution). After enabling tracing, I found the error message which reads:
jms cannot find object in dispatcher with id --some id--
And after some more digging I found that this is connected clientLeasePeriod in the remoting file. Any idea if it is possible to fix this kind of problem on Camel side?
You have to prevent JVM from finishing
Check this example: http://camel.apache.org/running-camel-standalone-and-have-it-keep-running.html
Provided you app contains only Main and xml file which configures Camel's context then context will be destroyed (so your routes destroyed as well). Even if different context run JMS implementation on same JVM. Sergey link should help you.
If you want just make it work to test things, add while(true) as a last line of your main. Note this is not the best approach :).
I realised that the problem was with the server on which the program was installed. The server thought that it resides on a public network, rather than private network (Windows Server 2012). After changing the network to private, the process worked correctly.
Note- the Camel did not give any errors regarding this, so this can be difficult to spot.

How can I implement the following?

I have a java program that has some number of classes. Three methods taken input A and give output B. I need to make these methods available as a web service so that I can ask something like http://test.com/method?input=A and the result B is returned. I don't want to re-write my existing code. Is there something which is available such as a web service framework for JAVA that can allow me to create a web service interface for these three methods. What is the easiest way?
I have ran into many acronyms and other stuff during my research such as dynamic project, JAVA EE, Glassfish etc... What can implement my requirement? Thanks!
You will probably need some sort of web framework -- Glassfish is one example. Basically, your application is not built to receive web requests, so you need some sort of container (e.g. a Servlet Container like Tomcat http://en.wikipedia.org/wiki/Web_container ).
I think "restlet" is a little servlet container that might suit your needs.
Check it out: http://www.restlet.org/
If you're running on a Java EE 6 server, you can use JAX-RS: http://docs.oracle.com/javaee/6/tutorial/doc/gilik.html
The easiest way to do quick Java services I've found is Restlet.
You can use their tutorials to get a webserver up and running like literally 20 minutes from scratch. The Restlet below should work right out of the box as a skeleton framework. You'll replace the call of String b = ... of course, and replace it with your own library.
public class Main extends Application {
public static void main(String[] args) {
Main main = new Main();
main.start();
}
private void start() {
Component c = new Component();
c.getServers().add(Protocol.HTTP, 80);
Application app = new Main();
c.getDefaultHost().attach(app);
c.start();
}
public Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("/method/{input}", new Restlet(getContext()) {
public void handle(Request request, Response response) {
String a = request.getAttributes().get("input").toString();
String b = MyLibraries.compute(a);
response.setEntity(b, MediaType.TEXT_HTML);
}
});
return router;
}
}

Categories

Resources