Enterprise Applications Flashcards

1
Q

Component

A
  • A component is a self-contained, reusable software unit that can be integrated into an application.
  • Clients interact with components via a well-defined contract.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

components on enterprise space

A

In the enterprise space, components focus more on implementing business services, with the contract of the component defined in terms of the business operations that can be carried out by that component.

The traditional component model for Java EE has always been the EJB model, which defines ways to package, deploy, and interact with self-contained business services.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

benefits that a well-defined set of business services brings to an application

A
  1. Loose coupling
  2. Dependency management
  3. Life cycle management
  4. Declarative container services
  5. Portability
  6. Scalability and reliability
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Loose coupling

A

Using components to implement services encourages loose coupling between layers of an application. The implementation of a component can change without any impact to the clients or other components that depend on it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Dependency management

A

Dependencies for a component can be declared in metadata and automatically resolved by the container.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Life cycle management

A

The lifecycle of components is well defined and managed by the application server. Component implementations can participate in lifecycle operations to acquire and release resources, or perform other initialization and shutdown behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Declarative container services

A

Declarative container services: Business methods for components are intercepted by the application server in order to apply services such as concurrency, transaction management, security, and remoting.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Portability

A

Components that comply to Java EE standards and that are deployed to standards-based servers can be more easily ported from one compliant server to another

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Scalability and reliability

A

Application servers are designed to ensure that components are managed efficiently with an eye to scalability. Depending on the component type and server configuration, business operations implemented using components can retry failed.
method calls or even fail over to another server in a cluster.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Session beans

A
  • The client accessible operations supported by the service may be defined using a Java interface, or in the absence of an interface, just the set of public methods on the bean implementation class.
  • the bean has access to a wide array of container services
  • The significance of the name “session bean” has to do with the way in which clients access and interact with them. Once a client acquires a reference to a session bean from the server, it starts a session with that bean and can invoke business operations on it.
  • There are three types of session beans: stateless, stateful, and singleton.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

stateless session beans

A

a stateless session bean sets out to complete an operation within the
lifetime of a single method.

There is no state that carries over from one business operation to the other.

Stateless beans can implement many business operations, but each method cannot assume that any other was invoked before it.

Advantage:
Stateless session beans can scale to large numbers of clients with minimal impact to overall server resources

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Singleton session beans

A

Singleton session beans can be considered a hybrid of stateless and stateful session beans.

All clients share the same singleton bean instance, so it becomes possible to share state across method invocations, but singleton session beans lack the conversational contract and mobility of stateful session beans.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Stateful session beans

A

a conversation that begins from the moment the client acquires a reference to the session bean and ends when the client explicitly releases it back to the server.

Business operations on a stateful session bean can maintain state on the bean instance across calls.

The only difference with the stateful session bean is that the server manages the actual object instance and the client interacts with that instance indirectly through a proxy object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Defining a stateless session bean

A

A session bean is defined in two parts:

  • Zero or more business interfaces that define what methods a client can invoke on the bean. When no interface is defined, the set of public methods on the bean implementation class forms a logical client interface.
  • A class that implements these interfaces, called the bean class, which is marked with the @Stateless annotation.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Should we define interfaces for session beans?

A

Whether you want to front your session bean with an actual interface or not is a
matter of preference.

In terms of API design, using an interface is probably the best way to expose a
session bean’s operations, since it separates the interface from the implementation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Session bean with interface

A

public interface HelloService {
public String sayHello(String name);
}

@Stateless
public class HelloServiceBean implements HelloService {
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}

This is a regular class that just happens to be an EJB.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Session bean with no interface

A
@Stateless
public class HelloService {
    public String sayHello(String name) {
        return “Hello, “ + name;
    }
}

Under the covers, the client will be interacting with a proxy that extends the bean class and overrides the business methods to provide the standard container services.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

EJB lifecycle callbacks

A

Problem :

The bean might have to acquire a resource, such as a JDBC data source, before business methods can be used. However, in order for the bean to acquire a resource, the server must first have completed initializing its services for the bean.
This limits the usefulness of the constructor for the class because the bean won’t have access to any resources until server initialization has completed.

To allow both the server and the bean to achieve their initialization requirements,
EJBs support lifecycle callback methods that are invoked by the server at various points in the bean’s lifecycle

19
Q

Stateless session beans callbacks

A

there are two lifecycle callbacks: PostConstruct and PreDestroy.

20
Q

PostConstruct

A

The server will invoke the PostConstruct callback as soon as it has completed initializing all the container services for the bean. In effect, this replaces the constructor as the location for initialization logic because it is only here that container services are guaranteed to be available.

21
Q

Predestroy

A

The server invokes the PreDestroy callback immediately before the server releases the bean instance to be garbage-collected.

Any resources acquired during PostConstruct that require explicit shutdown
should be released during PreDestroy.

22
Q

Shopping Cart Using a Stateful Session Bean

A
@Stateful
public class ShoppingCart {

private HashMap items = new HashMap();

public void addItem(String item, int quantity) {
Integer orderQuantity = items.get(item);
if (orderQuantity == null) {
orderQuantity = 0;
}

orderQuantity += quantity;
items.put(item, orderQuantity); }
public void removeItem(String item, int quantity) {
    // ...
}
public Map getItems() {
    // ...
}
@Remove
public void checkout(int paymentId) {
    // store items to database
    // ...
}

@Remove
public void cancel() {}

}

The first difference is that the bean class has state fields that are modified by the
business methods of the bean. This is allowed because the client that uses the bean effectively has access to a private instance of the session bean on which to make changes.

The second difference is that there are methods marked with the @Remove annotation.

23
Q

@Remove

A

These are the methods that the client will use to end the conversation with the bean. After one of these methods has been called, the server will destroy the instance, and the client reference will throw an exception if any further attempt is made to invoke business methods.

Every stateful session bean must define at least one method marked with the @Remove annotation, even if the method doesn’t do anything other than serve as an end to the conversation.

24
Q

Passivation and activation

A

Passivation is the process by which the server serializes the bean instance so
that it can either be stored offline to free up resources or replicated to another server in a cluster.

Activation is the process of deserializing a passivated session bean instance
and making it active in the server once again.

25
Q

Why passivation exists?

A

Because stateful session beans hold state on behalf of a client and are not removed until the client invokes one of the remove methods on the bean, the server cannot destroy a bean instance to free up resources.

Passivation allows the server to temporarily reclaim resources while preserving session state.

26
Q

Stateful lifecycle

A

supports two additional callbacks to allow the bean to gracefully handle passivation and activation of the bean instance.

Before a bean is passivated, the server will invoke the PrePassivate callback. The bean uses this callback to prepare the bean for serialization, usually by closing any live connections to other server resources.

After a bean has been activated, the server will invoke the PostActivate callback. With the serialized instance restored, the bean must then reacquire any connections to other resources that the business methods of the bean might be depending on.

27
Q

Stateless season beans

A

the singleton can be declared to be created eagerly during application initialization and exist until the application shuts down.

Once created, it will continue to exist until the container removes it, regardless of any exceptions that occur during business method execution.

The lifecycle callbacks for singleton session beans are the same as for stateless session beans

The key difference here with respect to stateless session beans is that PreDestroy is invoked only when the application shuts down as a whole

28
Q

Implementing a Singleton Session Bean

A
@Singleton
public class HitCounter {
int count;

public void increment() { ++count; }

public void getCount() { return count; }

public void reset() { count = 0; }

}

Unlike the stateless session bean, there is state in the form of a count field

unlike the stateful session bean, there is no @Remove annotation to identify the business method that will complete the session

29
Q

@Startup

A

In singleton session beans the container determines the point when the singleton instance gets created unless the bean class is annotated with the @Startup annotation to force eager initialization when the application starts.

30
Q

Servlets

A

Servlets are the oldest and most popular technology introduced as part of the Java EE platform.

31
Q

HTTP session

A

the HTTP session is a map of data associated with a session ID

The getSession() call on the HttpServletRequest object will either return the
active session or create a new one if one does not exist. Once obtained, the session acts like a map, with key/value pairs set and retrieved with the setAttribute() and getAttribute() methods
32
Q

What is a reference

A

Java EE components support the notion of references to resources. A reference is a named link to a resource that can be resolved dynamically at runtime from within application code or resolved automatically by the container when the component instance is created.

A reference consists of two parts: a name and a target. The name is used by
application code to resolve the reference dynamically, whereas the server uses target information to find the resource the application is looking for.

A reference is declared using one of the resource reference annotations: @Resource, @EJB, @PersistenceContext, or @PersistenceUnit

The choice of location determines the default name of the reference, and whether or not the server resolves the reference automatically.

33
Q

Dependency lookup

A

This is the traditional form of dependency management in Java EE, in which the application code is responsible for looking up a named reference using the Java Naming and Directory Interface (JNDI).

Every Java EE application server supports JNDI, even though it is less frequently used by applications since the advent of dependency injection

The name of the reference is bound into the environment naming context, and when it is looked up using the JNDI API, the server resolves the reference and returns the target of the reference.

34
Q

JNDI lookup()

A

The Context and InitialContext interfaces are both defined by the JNDI API. The lookup() method of the Context interface is the traditional way to retrieve objects from a JNDI context.

35
Q

EJBContext interface

A

EJBs also support an alternative syntax using the lookup() method of the EJBContext interface. The EJBContext interface (and subinterfaces such as SessionContext) is available to any EJB and provides the bean with access to runtime services such as the timer service.

36
Q

EJBContext advantages over JNDI API

A

The EJBContext lookup() method has two advantages over the JNDI API.

The first is that the argument to the method is the name exactly as it was specified in the resource reference.

The second is that only runtime exceptions are thrown from the lookup() method so the checked exception handling of the JNDI API can be avoided.

Behind the scenes, the exact same sequence of JNDI API calls is being made, but the JNDI exceptions are handled automatically.

37
Q

Environment naming context

A

Each Java EE component has its own locally scoped JNDI naming context called the environment naming context.

38
Q

Dependency Injection

A

The process of automatically looking up a resource and setting it into the class is called dependency injection because the server is said to inject the resolved dependency into the class.

39
Q

Dependency Injection advantages

A

Dependency injection is considered a best practice for application development, not only because it reduces the need for JNDI lookups but also because it simplifies testing

40
Q

Field injection

A

after the server looks up the dependency in the environment naming context, it assigns the result directly into the annotated field of the class.

@Stateless
public class DeptService {
    @EJB AuditService audit;
    // ...
}

Consider package scope for field injection if you want to unit test without having to add a setter

41
Q

Setter injection

A

involves annotating a setter method instead of a class field.

When the server resolves the reference, it will invoke the annotated setter method with the result of the lookup.

private AuditService audit;

@EJB
public void setAuditService(AuditService audit) {
this.audit = audit;
}

This style of injection allows for private fields, yet also works well with unit testing. Each test can simply instantiate the bean class and manually perform the dependency injection by invoking the setter method, usually by providing an implementation of the required resource that is tailored to the test.

42
Q

@PersistenceContext

A

declares a dependency on a persistence context and have the entity manager for that persistence context acquired automatically.

The value injected into the bean is a container-managed proxy that acquires and releases persistence contexts on behalf of the application code.

@PersistenceContext(unitName=”EmployeeService”)
EntityManager em;

If the unitName element is omitted, it is vendor-specific how the unit name for the persistence context is determined. Some vendors can provide a default value if there is only one persistence unit for an application, whereas others might require that the unit name be specified in a vendor-specific configuration file.

43
Q

@PersistenceContext in stateless season bean

A

The value injected into the bean is a container-managed proxy that acquires and releases persistence contexts on behalf of the application code.

44
Q

@PersistenceUnit

A

The EntityManagerFactory for a persistence unit can be referenced using this annotation