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.
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.
Actully I am trying to test my Jersey web service with the jersey test framework. the web server I am using is Websphere 7 and java version 6. This is my project requirement I cannot upgrade the java version .
My issue is how to build the unit test for my web service . I want to test them on WebSphere but I am not sure how do I setup the environment for unit testing like junit .
more specifically I just need to call an URL from the test class and check the response. But how to call the URL from the test class on the websphere I am not getting direction for it.
Have a look at the documentation for Jersey Test Framework.
First thing you need is one of the Supported Containers dependencies. Any of those will pull in the core framework e.g.
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>2.19</version>
<scope>test</scope>
</dependency>
Then you need a test class that extends JerseyTest. You can override Application configure() to provide a ResourceConfig as well as any other providers or properties. For example
#Path("/test")
public class TestResource {
#GET
public String get() { return "hello"; }
}
public class TestResourceTest extends JerseyTest {
#Override
public Application configure() {
ResourceConfig config = new ResourceConfig();
config.register(TestResource.class);
}
#Test
public void doTest() {
Response response = target("test").request().get();
assertEquals(200, response.getStatus());
assertEquals("hello", response.readEntity(String.class));
}
}
You should visit the link provided to learn more and see more examples.
My application should show a list of stuff over RESTful WebServices.
This list of stuff is generated via an EJB which is deployed in JBoss AS 6 in a .jar.
My question is, how do I get this list of stuff in order to show it via Web Services?
If I try to inject #EJB in the stateless bean annotated with #WebService, and try to invoke the method that generates the list, I get an error saying that #EJB cannot be resolved (or imported). (fixed it, needed to modify maven dependencies).
Now I get a NullPointerException when I call the supposedly injected EJB...
I am using the webapp archetype from Maven, which contains an EAR, a JAR and a WAR.
I am at a total loss here, been trying to figure this for almost a week.
Thanks in advance!
--EDIT-- code and typo fixed
This is my stateless bean which SHOULD be exposed as a WebService (according to:
http://www.adam-bien.com/roller/abien/entry/restful_calculator_with_javascript_and )
#Stateless
#Path("/MyRESTApplication")
public class HelloWorldResource {
#EJB
private TestBean testBean;
#GET()
#Produces("text/plain")
public String sayHello(String name) {
return testBean.doMoreStuff();
}
}
the doMoreStuff() function simply returns "HELLO!".
I want to deploy an EJB 3.0 stateless bean to WAS7 so I can access it as an EJB through a local interface and also as a jax-ws web service.
My bean looks as following:
#Stateless
#WebService
public class UserManagerImpl implements UserManager {
public UserManagerImpl() {
}
#WebMethod
public String getName(){
return "UserName";
}
}
The problem is that if I package it into an EJB-JAR and deploy, it doesn't work as a web service on WAS-7.
The only working configuration for me is if I put the EJB-JAR into a EAR and put this EJB-JAR to a WAR that is also in the EAR, like this:
EAR/
|--EJB-JAR
|--WAR/
|WEB-INF/lib/
|EJB-JAR
So my bean is duplicated.
Is there any problem with this design? If so, is there a better solution?
If your application contains #WebService annotated EJBs, then you need to process the EAR with the endptEnabler tool shipped with WebSphere before deploying it. Note that this doesn't apply to #WebService annotated classes in Web modules.
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.