As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I am trying to make a servlet thread-safe with sessions. I have read up on different techniques such as a synchronized block, AtomicReference or ConcurrentHashMap. What are the trade-offs for each technique, if any?
The first aim for a servlet is to achieve thread safety by virtue of no shared state. Any shared state will fail to be that when the servlet is deployed into a load-balancing cluster. So if your shared state is not of a cache flavor, meaning it can always be rebuilt from a durable store, you shouldn't have it in the first place.
But, apart from these concerns, you cannot get a one-size-fits-all answer without giving any details on the problem you are trying to solve with shared state. All the techniques you mention have merit, that's why they are still around with us after 15 years of experience with Java.
A servlet should not have state, i.e. instance or static variables that are affected by its request processing methods anyway (additionally its request processing methods should not affect the state of any shared objects). There is only one servlet instance per servlet container and each request is processed using a new Thread that runs the appropriate Servlet method (more commonly doGet() or doPost()).
However, the servlet API provides all the functionality required to store data with respect to a specific user session in a thread-safe manner out of the box. For instance, you could get the session by HttpServletRequest#getSession() and use its setAttribute() method to store objects in the specific session and getAttribute() to get them back on another request of the same session.
Hope this helps.
First of all, servlets should be stateless, which is good for the scalability of application. Actucally, Session Object based on Servlet API is highly extendable, you can write your own HttpSession implementation to make the Session access thread-safe.
IMHO, you should provide some more details of your scenario, because in those data structures you mentioned above, the last two seems have nothing to do with the thread-safe access of Session.
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I just started learning a functional language (Scala) and one of the claims/recommendations made is, "you should try to use react instead of recieve" method while doing multithreading. To make it clear, react doesn't return any value but recieve does. They have their own reasons to support this recommendation. As Scala works on the JVM. It is making me curious to think if using Callable is a more costly affair than using Runnable in Java?
Does anyone has any experience with the same or comments on this?
Runnable and Callback have the same "performance" as they are just Interfaces.
The two interfaces have slight API differences - a type compatible with the consuming API must be used; that is all.
This has nothing to do with Scala or react vs. recieve in Actors; the question boxes itself into the wrong corner.
Wellll, you're really mixing different concepts here.
The reason to use react instead of receive is that each actor with a receive requires its own thread. So you've got one thread per actor. react on the other hand is handled by a pool of threads that will run that message on that actor and then go on to the next actor and message. (This really only permits you to be reactive--you can't wait for a certain amount of time.)
On the other hand, the Runnable and Callable interfaces are just ways to package up code in Java depending on whether you just want it to do stuff (Runnable) or return a value (Callable). The interfaces themselves don't have any difference in performance, but in order to get a Callable return value back to you there is additional stuff that needs to happen, so if you could write it either way you'd possibly be better off using something that only requires a Runnable. (In practice, this means starting a thread instead of a future, probably.) But the implementation details matter so much that you can't really make any general recommendations on the basis of the interface alone. You need to know how the interface is actually being used in the actual class you're calling.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm about to write a JPA based web application in Java and since it's my first time seriously working with persistent classes and databases in general, I would like to learn if I have to make specific considertions f.i. while designing the overall class structures like building it as flat as possible to keep the count of persistent classes low and the database structure simple. I really don't expect long explanations but would like to get a short overview of general considerations which might not appear to be that obvious to one who is unexperienced with database applications.
Thanks in advance.
general considerations which might not appear to be that obvious to
one who is unexperienced with database applications
Using an ORM is superficially easy and transparent, but there are many small issues to consider:
Object identity. Understand the root mismatch that relational database work with rows and keys, while in the object world, each object has one identity. There various ways to handle identity correctly, or incorrectly.
Mapping Data. First understand how you map simple data types (string, number, URL, etc.). See and understand techniques to handle convertion problems (URL or string, byte or blob).
Mapping Relations. Understand how you map relations (uni- and bi-directional), how to maintain the invariants with proper encapsulation, the issue of "orphans" objects who lost their parent, and how to map several relations to the same class differently, how to load eagerly or lazily and the link with the "N+1" select issue.
Mapping Inheritance. Understand the mismatch between object, class, and tables. Understand how far you can support inheritance with ORM.
Working with Sessions. Understand working with sessions: when an object is attached or detached, what's the impact on lazy loading, how to deal with optimistic locking, the link between transaction and session.
There is an abundant literature on JPA and ORM in general. The list above is just what came to my mind.
Ideally, ORM should not impose constraints on your design. However, my advice would be keep it flat and simple.
You should go through this first
http://www.oracle.com/technetwork/articles/marx-jpa-087268.html
There is no need to make your database simpler because you're using JPA.
One of the main benefits of JPA is that it manages relationships for you.
It would be a pretty crappy API if you had to dumb down your database just to use it!
(espcially since it's typically used in a JavaEE environment)
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
There are a lot books/online resources about using patterns. But I didn't find any tasks for using it. But for good understanding of patterns it's need practice. Maybe someone faced with some resources where there are tasks for using patterns.
For example. Mediator pattern:1)write chat application where...
Thanks in advance.
UPDATE:
I found:
http://www.cs.sjsu.edu/~pearce/modules/labs/patterns/
How to study design patterns?
I'll give you five, with easy and/or moderate difficulty:
Singleton
easy: single database access class for the entire application.
Factory
easy: English-to-another-language translator. I need to be able to add and then access a new language translator with minimal code changes.
Observer
easy: Central data structure that has several copies within the application that need to be updated automatically when a change to the main DS occurs.
moderate: Make this work over a network with cooperating processes updating a central data structure.
Memento
easy: A simple game with the ability to save/load.
Decorator
easy: A simple persistence class with read/write ability. I want to be able to dynamically switch between XML or database persistence.
I know only of one such resource, and it is not formulated as you have specified, but maybe it'll help a bit: In the last chapters of the Head First Design Patterns book, the MVC pattern is explained as a compound pattern, involving several others : Composite, Strategy, Adapter etc.
It is explained with the help of a small application. You could look up the chapter and build the described to practice.
Ever use an iterator? Pattern. My guess is you use a lot of patterns without even really realizing you're using them. Created a buffered reader out of a file reader? Decorator; pattern. Don't set out trying to use patterns--let the problem discover them. They're everywhere, that's why they're patterns.
Things like facades, decorators, iterators, factories, etc. crop up in every single domain. Pick anything you're interested in writing, and discover the patterns already present. Refactor mercilessly--patterns.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
Can any one list out the tips to tune JSF WebApp # its best.
JSF RichFace
Never put logic into your getters.
They are called multiple times and
should only return something already
populated by another method. For
example if you are chaining drop-downs
together use an a4j:support tag on the
first one with an action attribute
that loads the data which is then
retrieved when you reRender the second
one.
Use the ajaxSingle="true" unless
you actually want to send the whole
form back to the server.
Don't use a
rich component if you only need a
normal one. For example don't use
rich:dataTable unless you are making
use of some of the features that it
has over and above h:dataTable.
Consider using immediate=true
attributes on elements where you do
not need validation Avoid displaying
large tables to user.
Use pagination
Do not over complicate EL expressions,
code them in Java in backing bean
JSF BestPractices
Performance Tuning
Moving to Stateless JSF would offer a great performance boost. Now it's possible to use JSF entirely stateless. See this blog & this issue. A payoff is that you can't create views dynamically (e.g. by binding, JSTL tags, etc), nor manipulate it after creation.
A Stateless JSF operation mode
would be incredibly useful for high-load applications and
architectures:
http://industrieit.com/blog/2011/11/stateless-jsf-high-performance-zero-per-request-memory-overhead/#comment-4
This has previously been suggested by Jacob:
http://weblogs.java.net/blog/jhook/archive/2006/01/experiment_goin.html
This would help JSF ditch the stigma of "slow and memory hog," and
help keep up with current tech trends (stateless architectures.)
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm going to develop my own message queue implementation in Java and I need to distribute the queue content across multiple servers so that it would provide reliability and redundancy.
In addition to that I need to persist the queue content into the file system.
Can somebody tell me what is the most suitable distributed data structure implementation to hold my queue content?
Note: That data structure must provide me the message ordering facility. That means I need to receive messages according to the order they arrived. Also while reading a message, it should be in a 'locked' state so that other consumers cant read it until the first consumer completes the reading process
Have you looked at any of the many existing message queue implementations for java? Wikipedia lists many open source implementations. It seems to me that an existing, thoroughly tested message queue is the best place to hold your queue content :)
If you absolutely want to write your own, then starting with the open source solution that most fits your needs would probably answer most of your questions about what data structures work well.