Last few days i was playing with GranideDS tutorials (using Spring server and AIR client)
https://github.com/graniteds-tutorials/graniteds-tutorial-data
"This tutorial shows how to build a simple data application that
manages a database of user accounts. All connected clients are
notified and synchronized with data updates using a GraniteDS long
polling channel."
Unfortunately i cannot find any GraniteDS javascript client library or example.
I created an HttpServlet to manage (add Entity for example) persistense context using http (ajax) requests.
my TestServlet.java
#WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
AccountService srvObject = (AccountService) wac.getBean("testService");
//testService mean spring service annotation parameter
Account emp = new Account();
emp.setName(request.getParameter("name"));
emp.setEmail(request.getParameter("email"));
srvObject.save(emp);
response.getWriter().println("OK");
}
}
This method adds an entity correctly but connected client's data are NOT syncronized. How can i notify all clients about new changes?
UPDATE:
I was trying to change DataEnabled's publish to PublishMode.ON_COMMIT
#DataEnabled(topic="dataTopic", publish=DataEnabled.PublishMode.ON_COMMIT, useInterceptor=true)
add to application-context.xml
<graniteds:tide-data-publishing-advice/>
In this case both air application and servlet causing server error:
SEVERE: Could not register synchronization for ON_COMMIT publish mode,
check that the Spring PlatformTransactionManager supports it and that
the order of the TransactionInterceptor is lower than the order of
TideDataPublishingInterceptor
And <graniteds:tide-data-publishing-advice order="-1"/> does not helps.
You can try the mode ON_SUCCESS with useInterceptor=true
"useInterceptor=true" is necessary because it's what will make GraniteDS aware of your service which is called completely outside its control
Related
I'm super new to java web application development. Working on basic Todos app. So far app is working fine. I wish to add dynamic url routes like updateTodos, deleteTodos to existing path.
Expected behavior as shown below
/todos
*render todos list*
/todos/update
*render updateTodos.jsp*
/todos/delete
*render deleteTodos.jsp*
Below is my code
#WebServlet(urlPatterns = "/todos")
public class UserTodos extends HttpServlet {
private TodoService todoService = new TodoService();
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
if(request.isRequestedSessionIdValid()){
request.setAttribute("userName", request.getParameter("userName"));
request.setAttribute("allTodos", todoService.retrieveTodos());
request.getRequestDispatcher("WEB-INF/views/todos.jsp").forward(request, response);
}else{
response.sendRedirect("/login");
}
}
}
My understanding is so far that the annotation #WebServlet triggers the class file based on route defined. Can I achieve the above with a single class file?
Something like being done in JS web-framework Express-JS
If I sum-up my whole query is - Is it possible to multiple doGet and doPost methods in a single class file which will be executed based on user's URL accessed?
Update
I was able to achieve this, not sure whether this is correct implementation or not :/
I simply need to attach query parameters onto an outgoing request. (Java EE 7.0, JAX-RS 2.0)
In specifics, I currently using the RESTeasy Client ver 3.0.14, so I make my calls using the fancy interface-proxy system. I was attempting to produce something like this:
myapplication/api/path?timestamp=000
with:
#Provider
public class MyRequestFilter implements ClientRequestFilter {
#Context
private HttpServletRequest servletRequest;
public void filter(ClientRequestContext requestContext) throws IOException {
servletRequest.getParameterMap().put("timestamp", new String[]{
String.valueOf(new Date().getTime())
});
}
}
I made sure I was registering it with client.register(MyRequestFilter.class) as well. Feel free to ask questions. Thanks!
Credit to #peeskillet --
Rebuild the URI from the requestContext like this:
requestContext.setUri(UriBuilder.fromUri(requestContext.getUri()).queryParam("key", value).build());
You can now see the new query parameter with
requestContext.getUri().toString();
Again, verify that you register it when making the REST Client
client.register(MyRequestFilter.class);
I have to develop a Java web service that get a request and sends immediately an acknowledgment (synchronous), so that far, it is simple.
Next, the web service has to do multiple checks on the request, then send a response according to that (synchronous too, because i don't have a callback endpoint from the client).
The problem is that i can send the ackowledgment, and i launch the multiple checks in another thread, but when the checks are done, the client already recieved his response, and i can't send another one.
Here's what i did for now:
#WebService
public class Configuration {
#Resource WebServiceContext context;
#WebMethod
public ReqAckType configure(#XmlElement(required = true) #WebParam(name = "reqType")
ReqType req) {
ReqAckType ack = new ReqAckType();
ack.setReceptionTime(Calendar.getInstance());
ChecksScheduler cs = ChecksScheduler.getInstance();
Checks checks = cs.schedule(req);
ack.setInternalId(checks.getId());
return ack;
}
}
If anyone can help me figure out how to send two separate message (ack and response), knowing that i have to send them separately and the checks take too much time (it's because of that, that i have to send and ack), i would be thankful.
I am using Oracle Fusion Middleware (Weblogic, JDeveloper, ..)
I have 2 java classes and I want to transfer data between them.
I take user id as parameter in a previous jsp form, and in a java class, using setAttribute I create a atribute named st_id.
then in another java clas I want to retrieve this data, but I get null.pointer exception.
first java file;
public class Signin implements Action {
public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {
Student stu = new StDAO().getUser(request.getParameter("st_id").toString());
request.setAttribute("st_id", request.getParameter("st_id").toString());
...
second;
public class addCourseStu implements Action{
#Override
public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {
TakeCourseDAO pf = new TakeCourseDAO();
String s= (String) request.getAttribute("st_id");
So s is null, it's not my intention.
A request exists from the time the web browser sends it to the web server until the web server (via the servlet) has made its response.Every request for a servlet has its own accessibilty scope. From a servlet, you can:
add new attributes to the request's scope
obtain exisiting attributes from the request's scope
remove exisiting attributes from the request's scope
As you are getting null it is quite obvious that the attribute is not accessed within the scope.
You can try alternatives like Session scope or Application scopes which ever suits you
It is not entirely clear what you want to do but I gather that you want to maintain some state on the server between two requests right?
Look into sessions & cookies for this.
What you do here is weird as it seems you are setting an attribute on an incoming request in the first file.
I have an existing Web Application (Servlet) that remotely accesses an EJB currently written to the EJB 2.1 Specification (EJBHome, EJBObject, SessionBean, all configuration in ejb-jar.xml). The servlet access the EJB via JNDI lookup, the JNDI name being specified in weblogic-ejb-jar.xml.
I would like to update the EJB to EJB3.0, which will (hopefully) make it easier to add to the API.
My issue is that I need to do it with a minimum of change to the existing servlet code. In other words, I still need to:
Access the EJB with a simple, global JNDI name (it is stored in a database, and the servlet can can change the name it looks up on the fly), and
Be able to use the 2.1 style:
String jndiName = getJndiName(); // e.g. "BeanV2.0"
BeanHome home = (BeanHome) PortableRemoteObject.narrow(
jndiContext.lookup(jndiName), BeanHome.class);
BeanRemote remote = home.create();
remote.doBusiness(); // call business method
I have tried a stripped down version, applying #RemoteHome, but I keep getting errors during deployment.
I am deploying (for development/production) on Weblogic, mostly 10.3.5 (11gR1), and am limited to EJB 3.0. I am using Eclipse (with the Oracle Weblogic Pack) for development.
Thanks.
I would suggest you to first understand the architectural difference between EJB2.1 and EJB3.x (refer here)
You will have some major/minor changes in code based on your bean implementation and client side invocation, because of the removal of Home and Deployment Descriptor in EJB3.x
Refer this example for accessing EJB3.x from a client, in your case Servlet
/**
* Servlet implementation class SampleServlet
*/
public class SampleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public SampleServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Context ctx=getInitialContext();
String jndiName=getJndiName(); // eg. java:global/BeanProject/Bean!demo.BeanRemote
BeanRemote beanRemote = (BeanRemote)ctx.lookup(jndiName);
remote.doBusiness();
} catch(Exception exception) {
System.out.println(exception.getMessage());
}
}
private static Context getInitialContext() throws Exception
{
Properties properties=new Properties();
properties.put("java.naming.factory.initial","org.jboss.naming.remote.client.InitialContextFactory");
properties.put("java.naming.provider.url","remote://localhost:4447");
return new InitialContext(properties);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
Hope this helps!