EJBs can be accessed with RMI or as SOAP-RESTful endpoint. I want to access a remote EJB from another computer/ip address for example in a standalone application. I can reach to EJBs with web services endpoint then i dont know to reach with RMI. How can i implement this idea. I'm using Glassfish 3.1.
Check out the How do I access a Remote EJB component from a stand-alone java client? document. Code snippets rely on EJB 2 (Home interfaces), you should lookup #Remote interfaces directly. Of course they must be available on the client side.
Example
Based on: Creating EJB3 Sessions Beans using Netbeans 6.1 and Glassfish:
jndi.properties:
java.naming.factory.initial = com.sun.enterprise.naming.SerialInitContextFactory
java.naming.factory.url.pkgs = com.sun.enterprise.naming
java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl
org.omg.CORBA.ORBInitialHost = localhost
org.omg.CORBA.ORBInitialPort = 3700
Main.java:
package testclient;
import java.io.FileInputStream;
import java.util.Properties;
import javax.naming.InitialContext;
import stateless.TestEJBRemote;
public class Main {
public static void main(String[] args) {
try {
Properties props = new Properties();
props.load(new FileInputStream("jndi.properties"));
InitialContext ctx = new InitialContext(props);
TestEJBRemote testEJB = (TestEJBRemote) ctx.lookup("stateless.TestEJBRemote");
System.out.println(testEJB.getMessage());
} catch (NamingException nex) {
nex.printStackTrace();
} catch (FileNotFoundException fnfex) {
fnfex.printStackTrace();
} catch (IOException ioex) {
ioex.printStackTrace();
}
}
}
See also
Call EJBs Deployed in GlassFish from the NetBeans Platform
Glassfish V3.x and remote standalone client
Related
I have created a sample project and used EJB 3.1 with a RESTful web service. In the sample I have a class which extends Application. I expect the class works like a servlet and dispatch requests to appropriate classes but it does not. When I use web.xml my sample project works fine. What is wrong with my sample project?
#ApplicationPath("/rest")
public class ApplicationServlet extends Application {
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(UserWS.class);
return classes;
}
}
I use UserWS as a EJB session bean which exposes web service:
#Stateless
#LocalBean
#Path("/user")
public class UserWS {
private int count;
public UserWS() {
this.count=0;
}
#GET
#Path("/name/{username}")
public void getUserName(#PathParam("username") String username) {
count++;
System.out.println("count is:"+ count);
}
}
I'm afraid it won't be possible once JBoss 5.0 supports only Servlet 2.5. For more details, see here.
To avoid the web.xml deployment descriptor, you need a servlet container the supports at least Servlet 3.0.
So, what could you do to solve it?
These are the options that came up to my mind:
You could try upgrading the JBoss Web (Tomcat fork used by JBoss AS) as described here, but try that at you own risk.
Consider using a recent version of JBoss/WildFly.
I'm quite new to the Wildfly server.
Currently I'm trying to expose a simple Stateless Session Bean as web service. I don't want to use the webModule to define servlet mappings for the web services beans. I want to keep that seperated.
I just want to expose a simple Stateless Session Bean as web service.
I used the wildfly-javaee7-webapp-ear-blank-archetype as starting point.
In the ejbModule i added a Stateless Session Bean with #WebService annotation.
The ejbModule is packaged in an .ear file, which is deployed to the WildFly Server 9. The deployment shows now error message.
I now expected to see some endpoints in the admin console under web service endpoints as the documentation(https://docs.jboss.org/author/display/WFLY9/JAX-WS+User+Guide) says. But I can't see any endpoints.
What I'm doing wrong ?
How can i access the generated WSDL file of the web service ?
What is the exact context root when ejb is packaged inside ear file?
Any hints are appreciated.
package eu.sample.testws.service;
import javax.ejb.Stateless;
import javax.jws.WebService;
/**
* Session Bean implementation class TestWSBean
*/
#Stateless
#WebService(serviceName="TestWSService", name="TestWSServiceName", portName="TestWSPortName", targetNamespace="http://sample.eu")
public class TestWSBean {
/**
* Default constructor.
*/
public TestWSBean() {
// TODO Auto-generated constructor stub
}
public String sayHello(){
return "Hello";
}
}
What type of web service is supported by gwt application i have tried using Jersey, RESTful, Restlet, but nothing works with GWT. I want to deploy Web-Service on Tomcat and GWT application on app engine.
You can use RPC and RequestBuilder:
https://developers.google.com/web-toolkit/doc/latest/DevGuideServerCommunication
You can also use RESTful services:
How to call RESTFUL services from GWT?
Thanx all for your suport . . i have got the answer for my question.
i created a restfull web service using Jersey and called it using the following code in my gwt app engine application:
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(UriBuilder.fromUri("http://localhost:8080/de.vogella.jersey.first").build());
String obj=service.path("rest").path("bye").accept(MediaType.TEXT_PLAIN).get(String.class);
and the web application code is :
package de.vogella.jersey.first;
import javax.ws.rs.*;
#Path("/bye")
public class Hello {
// This method is called if TEXT_PLAIN is request
#GET
#Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello it worked";
}
For Web Application Code refer to this link:
http://www.vogella.com/articles/REST/article.html
i have a problem deploying my webservice to Weblogic 11g.
JAVA: JRockit 1.6.x
I need to run a method on webservice deployment and i made this code:
#Singleton
#Startup
public class StartupBean {
Logger logger = Logger.getLogger(StartupBean.class);
#PostConstruct
private void postConstruct() {
logger.error("WS started.");
}
#PreDestroy
private void preDestroy() {
logger.error("WS stoped.");
}
}
without any additional xml config.
It works normally on 12c but i need it on 11g.
What is the workaround?
Thanks
No, you can't do this in pre-3.1-EJB without XML config.
The common practice in EJB 3.0 for implementing a #Startup bean was to instantiate in in a servlet, which is configured to load on startup in web.xml. If you need it just for log4j you can initialize it directly from such servlet.
i have a local client j2se application and backend is derby(javadb) database and dao is jpa eclipselink .
how do i send these database pojo to a remote database which linked with spring ( jsp) application on tomcat server
simply this is a rich client with swing which connects to tomcat deployed web application. The client should receive data and send data through HTTP requests to the server-side of the service,
what would be the best solution ??
01) direct database connection/transaction through socket using Eclipselink
02) web service ??
03) just send post request to spring web application and convert it to POJO and persist to database
how do i achieve this??
DISCLAIMER I am not suggesting you port your app from Spring to EJB. Despite how people like to compare them as exclusively one or the other, you can use them both. Its your app, you can be as pragmatic as you want to be :)
You don't necessarily have to use Web Services if you wanted. You could drop the OpenEJB war file into Tomcat as well and create an Remote EJB to send data back and forth.
Once you drop in OpenEJB you can put a remote #Stateless bean in your app like so:
#Stateless
#Remote
public class MyBean implements MyBeanRemote {
//...
}
public interface MyBeanRemote {
// any methods you want remotely invoked
}
Then look it up and execute it over HTTP from your Swing app like so:
Properties p = new Properties();
p.put("java.naming.factory.initial", "org.apache.openejb.client.RemoteInitialContextFactory");
p.put("java.naming.provider.url", "http://tomcatserver:8080/openejb/ejb");
// user and pass optional
p.put("java.naming.security.principal", "myuser");
p.put("java.naming.security.credentials", "mypass");
InitialContext ctx = new InitialContext(p);
MyBean myBean = (MyBean) ctx.lookup("MyBeanRemote");
Client-side all you need are the openejb-client.jar and javaee-api.jar from the OpenEJB war file and your own classes.
Since it's already a Spring app don't bother trying to use #PersistenceContext to get a reference to the EntityManager so the EJB can use it. Just figure out how to expose the EntityManagerFactory that Spring creates (or you create) to the EJB via any means possible. The direct and ugly, but effective, approach would be a static on the MyBean class and a bit of startup logic that sets it. You'd just be using the EJB for remoting so no need for fancier integration.
If you did really need web services for non-java communication or something, you can add #WebService to the top of your bean and then it will have WSDL and all that generated for it:
#Stateless
#Remote
#WebService(portName = "MyBeanPort",
serviceName = "MyBeanService",
targetNamespace = "http://superbiz.org/wsdl"
endpointInterface = "org.superbiz.MyBeanRemote")
public class MyBean implements MyBeanRemote {
//...
}
public interface MyBeanRemote {
// any methods you want remotely invoked
}
Then you can also use the same bean as a web service like:
Service service = Service.create(
new URL("http://tomcatserver:8080/MyBeanImpl?wsdl"),
new QName("http://superbiz.org/wsdl", "MyBeanService"));
assertNotNull(service);
MyBeanRemote myBean = service.getPort(MyBeanRemote.class);
Both approaches are over http, but the web service approach will be a bit slower as it isn't a binary protocol.