Openshift: Trying to create java.websocket using JBoss Application Server - java

I am new to open shift, so bear with me. I have gotten open shift on my eclipse, and I have set up a JBoss Application Server 7. I want to make a server endpoint java class as described here. How do I do this? I noticed that, first of all, there is no java.websocket jar files in the open shift project that I created in eclipse. So I decided to import it and added to the build path. (This jar file I copied from glassfish's javax.websocket-api.jar). But whenever I would commit it and push my java class, it would give me a bunch of errors and mu open shift domain won't work.
So what am I doing wrong? Is it that the jar file that I imported is wrong (i.e. it isn't compatible with JBoss)?
Another thing that eclipse is telling me is that there is this error: "Faceted Project Problem (Java Version Mismatch) (1 item)"
Here is my server endpoint class:
package serverendpointdemo;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.server.ServerEndpoint;
#ServerEndpoint("/serverendpointdemo")
public class ServerEndPointDemo {
#OnOpen
public void handleOpen () {
System.out.println("JAVA: Client is now connected...");
}
#OnMessage
public String handleMessage (String message) {
System.out.println("JAVA: Received from client: "+ message);
String replyMessage = "echo "+ message;
System.out.println("JAVA: Send to client: "+ replyMessage);
return replyMessage;
}
#OnClose
public void handleClose() {
System.out.println("JAVA: Client is now disconnected...");
}
#OnError
public void handleError (Throwable t) {
t.printStackTrace();
}
}
Thank you for your help!

Related

Why I can't call methods outside of my websocket server in Java?

I have websocket server class where I want to print when user connects. It works without a problem, however if I want to execute a method outside of this websocket server, it doesn't print and closes the client's connection and prints out java RuntimeException error.
package com.rupla.myapp.endpoint;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.sql.SQLException;
import javax.websocket.EncodeException;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import com.rupla.myapp.encoders.MessageEncoder;
import com.rupla.myapp.http.MyOtherClas;
import com.rupla.myapp.encoders.MessageDecoder;
#ServerEndpoint(value = "/chatnow", encoders = MessageEncoder.class, decoders = MessageDecoder.class)
public class WebSocketServer {
#OnOpen
public void onOpen(Session session) {
extracted(session);
}
private void extracted(Session session) {
System.out.println("Session Opened (Client to Server) - ID: " + session.getId());
}
#OnMessage
public void handleTextMessage(String message, Session s) throws IOException {
System.out.println("New Text Message Received: " + message + " - From session: " + s.getId());
//So here we send client's message to the client - This works
s.getBasicRemote().sendText("Server: " + message);
try {
// Here I try to print "hello" but from 'MyOtherClas'.class using printTest() method
MyOtherClas.printTest();
//Right after this code, server closes the connection to the client and doesn't print anything
}
catch(Exception e) {
System.out.println("fail: "+e);
}
}
/**
* If I would call this method below, it would work since it's in the same class (but why?)
*/
private void printInsideClas() {
System.out.println("hello");
}
#OnClose
public void onClose(Session session) throws IOException, EncodeException {
System.out.println("Session Closed");
}
#OnError
public void onError(Throwable t) {
System.out.println("onError: " + t);
}
}
Pretty basic websocket server for java. However when I run this on my tomcat 9.0 and connect with javascript client and try to send message from my client to the server. It closes the connection between server and client and then prints this error.
Session Opened (Client to Server) - ID: 4
New Text Message Received: asd - From session: 4
onError: java.lang.RuntimeException: org/apache/http/Header
Session Closed
I tried to google this for few days for now but still I have no answers, I really need help with this.
The exception is in a server-side webservices stack. The exception is saying that it cannot load a core class that is part of Apache HTTPComponents.
Here are some possible explanations:
Your server-side runtime is missing the JAR file.
The JAR is present, but it is not on the right runtime classpath.
The Header class depends on some other class that has failed class initialization. If this has happened, there should be evidence earlier in the logfile.
The JAR file you should be looking for is "httpcore-.jar" or the equivalent from the old Jakarta project.
Figure out which of the above has happened, and you are close to solving the problem.

External library not available when deploy with fat-jar

I'm using in my program the bluecove library.
While running the program via eclipse, all works smooth. I'm now trying to deploy my program, and following this post i'm using fat-jar.
When i run the jar file (created by fat-jar), the library can't be located, and i'm getting the exception BlueCove libraries not available as result of this line local = LocalDevice.getLocalDevice();.
In the fat-jar window i tried also to add bluecove-2.1.0.jar to the Class-Path place, and also with the path \src\JoJoServer\bluecove-2.1.0.jar.
I tried also to place the bluecove's jar file in different folders, such as the src, or an external folder.
Although i know it's not recommended, i tried the option of One-Jar, nevertheless it didn't help.
To run the jar (the one created by fat jar) i simply double click the file.
What i'm missing?
This is the entire code:
import java.io.IOException;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
#Override
public void run() {
// retrieve the local Bluetooth device object
LocalDevice local = null;
StreamConnectionNotifier notifier;
StreamConnection connection = null;
// setup the server to listen for connection
try {
local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.GIAC);
UUID uuid = new UUID("0000110100001000800000805F9B34FB", false);
System.out.println(uuid.toString());
String url = "btspp://localhost:" + uuid.toString() + ";name=RemoteBluetooth";
notifier = (StreamConnectionNotifier)Connector.open(url);
} catch (BluetoothStateException e) {
System.out.println("Bluetooth is not turned on.");
e.printStackTrace();
return;
}
// ...
}
I have no clue what could be your problem, but I've tried the process and everything works, so just a summary of what I've did. Maybe you will figure it out by following it...
I don't understand how the posted code could be the entire, I see no class definition. :)
So I've modified it to the main method and it works both from the Eclipse and also by running the JAR generated by the FatJar.
The modified code of the BTTest class:
package test;
import java.io.IOException;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
public class BTTest {
public static void main(String[] args) throws Exception{
// retrieve the local Bluetooth device object
LocalDevice local = null;
StreamConnectionNotifier notifier;
StreamConnection connection = null;
// setup the server to listen for connection
try {
local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.GIAC);
UUID uuid = new UUID("0000110100001000800000805F9B34FB", false);
System.out.println(uuid.toString());
String url = "btspp://localhost:" + uuid.toString()
+ ";name=RemoteBluetooth";
notifier = (StreamConnectionNotifier) Connector.open(url);
} catch (BluetoothStateException e) {
System.out.println("Bluetooth is not turned on.");
e.printStackTrace();
return;
}
// ...
}
}
To run or produce it, I have just put the bluecove library in the build path and created the fat jar with a simple way:
http://oi60.tinypic.com/vg1jpt.jpg
Starting the generated jar from command line:
D:\testProjects\bttest>java -jar bttest_fat.jar
BlueCove version 2.1.0 on winsock
0000110100001000800000805f9b34fb
BlueCove stack shutdown completed
Can you post a difference to your process?

WebService client fails: Tomcat, CXF

I am copying the simplest web service example from CXF; it steps through writing an interface, then an implementation file, to say hello to a name provided by the webservice consumer. I changed a package name and the method name because I wanted to see where things showed up; if you name everything HelloWorld you can't see what is method, package, class, etc.
Those instructions include a program to publish the web service. After I do that, putting the URL
http://localhost:9000/helloWorld?wsdl
in a browser displays a wsdl file that contains enough stuff the way I spelled it to convince me that it was generated from my code. I assume, based on this, that both the WSDL generation and the publication worked.
This is the service interface:
package hw;
import javax.jws.WebParam;
import javax.jws.WebService;
#WebService
public interface HelloWorld
{
String sayHi(#WebParam(name="firstName") String firstName);
}
This is the service implementation:
package hwimpl;
import javax.jws.WebService;
#WebService(endpointInterface = "hw.HelloWorld", serviceName = "HelloWorld")
public class HelloWorldImpl
{
static public void say(String msg) { System.out.println(msg); }
public String sayHi(String firstName)
{ say ("sayHi called with " + firstName);
return "Hello " + firstName + " from the World.";
}
}
And this is the publishing program:
package hwimpl;
import javax.xml.ws.Endpoint;
public class PublishHelloWorldService
{
protected PublishHelloWorldService() throws Exception
{
// START SNIPPET: publish
System.out.println("Starting Server");
HelloWorldImpl implementor = new HelloWorldImpl();
String address = "http://localhost:9000/helloWorld";
Endpoint.publish(address, implementor);
// END SNIPPET: publish
}
public static void main(String args[]) throws Exception
{
new PublishHelloWorldService();
System.out.println("Server ready...");
Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting");
System.exit(0);
}
}
Now I compile and run this program:
package client;
import hw.HelloWorld;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
public final class HelloWorldClient
{
private static final QName SERVICE_NAME = new QName("http://server.hw.demo/", "HelloWorld");
private static final QName PORT_NAME = new QName("http://server.hw.demo/", "HelloWorldPort");
private HelloWorldClient()
{
}
public static void main(String args[]) throws Exception
{
Service service = Service.create(SERVICE_NAME);
String endpointAddress = "http://localhost:9000/helloWorld";
// If web service deployed on Tomcat deployment, endpoint should be changed
// to:
// String
// endpointAddress =
// "http://localhost:8080/java_first_jaxws/services/hello_world";
// Add a port to the Service
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
HelloWorld hw = service.getPort(HelloWorld.class);
System.out.println(hw.sayHi("Albert"));
}
}
and I get this error:
Exception in thread "main" javax.xml.ws.WebServiceException: Could not send Message.
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:135)
at com.sun.proxy.$Proxy20.sayHi(Unknown Source)
at client.HelloWorldClient.main(HelloWorldClient.java:37)
Caused by: java.net.MalformedURLException: Invalid address. Endpoint address cannot be null.
at org.apache.cxf.transport.http.HTTPConduit.getURL(HTTPConduit.java:872)
at org.apache.cxf.transport.http.HTTPConduit.getURL(HTTPConduit.java:854)
at org.apache.cxf.transport.http.HTTPConduit.setupURL(HTTPConduit.java:800)
at org.apache.cxf.transport.http.HTTPConduit.prepare(HTTPConduit.java:548)
at org.apache.cxf.interceptor.MessageSenderInterceptor.handleMessage(MessageSenderInterceptor.java:46)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:255)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:516)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:313)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:265)
at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:73)
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:124)
... 2 more
I am running the programs -- both publish and client -- from eclipse. The eclipse is set up with proxies for http and https in Window / Preferences; I removed the one for http before running the client, but it did not change the message.
It is in fact a tomcat server; I tried the alternate URL in the publish program with no change.
I don't run tomcat from within eclipse in this case; I run it by itself on my machine and then run the publish program (from eclipse), verify the url that displays the wsdl works correctly, and then run the client program (from eclipse) and get my error.
Can someone tell me what I'm doing wrong? I've seen other posts on this exact error message, but none of the answers were definitive and I appear to have tried them all.
Not sure this is your problem.
I've sometimes had problems with eclipse not being able to run tomcat applications on a running tomcat as you describe in your example.
What I sometimes have to do when working with tomcat and eclipse is either
have a running tomcat (windows service) and then export my eclipse application to that tomcat
stop the running tomcat on that port from windows services and start the tomcat from inside eclipse when running the program.
For some reason eclipse seems to have problems with an already running tomcat.

Error: javax.wireless.messaging does not exist

I'm not java developer, but I need to test a code. I installed jdk and i tested Hello Word it worked fine but When i try to javac this another script, I'm gettings this error.
My Code:
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
import javax.microedition.io.Connector;
import java.io.IOException;
public class SMSUtility {
public static void sendMessage(String msisdn, String text)
throws IOException {
// Open connection
MessageConnection con = (MessageConnection)
Connector.open("sms://+" + msisdn);
// Create new message
TextMessage message = (TextMessage)
con.newMessage(MessageConnection.TEXT_MESSAGE);
// Set text
message.setPayloadText(text);
// Send message
con.send(message);
// Close connection
con.close();
}
}
javax.wireless is not part of the SDK. Read about the Wireless Messaging API.
javax.messaging is in wma20.jar, but i can't figure out, how to add it to buildpath with ant...

Web Services using jdk 1.6

Hi All
I am new to web services. I have written a java class.
But I am not getting how to deploy it. I mean do i need web server or app server . As this is simple java class i can not make WAR file to deploy it . So what is the method to deploy it and which server should i use. I am using JDK 1.6
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.xml.ws.Endpoint;
#WebService
public class WiseQuoteServer {
#SOAPBinding(style = Style.RPC)
public String getQuote(String category) {
if (category.equals("fun")) {
return "5 is a sufficient approximation of infinity.";
}
if (category.equals("work")) {
return "Remember to enjoy life, even during difficult situatons.";
} else {
return "Becoming a master is relatively easily. Do something well and then continue to do it for the next 20 years";
}
}
public static void main(String[] args) {
WiseQuoteServer server = new WiseQuoteServer();
Endpoint endpoint = Endpoint.publish(
"http://localhost:9191/wisequotes", server);
The best answer to your question would be the tutorial of JAX-WS

Categories

Resources