`

业务层模式:Service Locator—服务定位器模式

阅读更多

Context
Service lookup and creation involves complex interfaces and network operations.
Problem
J2EE clients interact with service components, such as Enterprise JavaBeans (EJB)
and Java Message Service (JMS) components, which provide business services and
persistence capabilities. To interact with these components, clients must either locate the
service component (referred to as a lookup operation) or create a new component. For
instance, an EJB client must locate the enterprise bean's home object, which the client then
uses either to find an object or to create or remove one or more enterprise beans. Similarly,
a JMS client must first locate the JMS Connection Factory to obtain a JMS Connection or a
JMS Session.
All Java 2 Platform, Enterprise Edition (J2EE) application clients use the JNDI
common facility to look up and create EJB and JMS components. The JNDI API enables
clients to obtain an initial context object that holds the component name to object bindings.
The client begins by obtaining the initial context for a bean's home object. The initial
context remains valid while the client session is valid. The client provides the JNDI
registered name for the required object to obtain a reference to an administered object. In
the context of an EJB application, a typical administered object is an enterprise bean's home
object. For JMS applications, the administered object can be a JMS Connection Factory (for
a Topic or a Queue) or a JMS Destination (a Topic or a Queue).
So, locating a JNDI-administered service object is common to all clients that need to
access that service object. That being the case, it is easy to see that many types of clients
repeatedly use the JNDI service, and the JNDI code appears multiple times across these
clients. This results in an unnecessary duplication of code in the clients that need to look up
services.
Also, creating a JNDI initial context object and performing a lookup on an EJB home
object utilizes significant resources. If multiple clients repeatedly require the same bean
home object, such duplicate effort can negatively impact application performance.
Let us examine the lookup and creation process for various J2EE components.
The lookup and creation of enterprise beans relies upon the following:
A correct setup of the JNDI environment so that it connects to the naming and
directory service used by the application. Setup entails providing the location of the naming
service and the necessary authentication credentials to access that service.
The JNDI service can then provide the client with an initial context that acts as a
placeholder for the component name-to-object bindings. The client requests this initial
context to look up the EJBHome object for the required enterprise bean by providing the
JNDI name for that EJBHome object.
Find the EJBHome object using the initial context's lookup mechanism.
After obtaining the EJBHome object, create, remove, or find the enterprise bean,
using the EJBHome object's create, move, and find (for entity beans only).
The lookup and creation of JMS components (Topic, Queue, QueueConnection,
QueueSession, TopicConnection, TopicSession, and so forth) involves the following steps.
Note that in these steps, Topic refers to the publish/subscribe messaging model and Queue
refers to the point-to-point messaging model.
Set up the JNDI environment to the naming service used by the application. Setup
entails providing the location of the naming service and the necessary authentication
credentials to access that service.
Obtain the initial context for the JMS service provider from the JNDI naming service.
Use the initial context to obtain a Topic or a Queue by supplying the JNDI name for
the topic or the queue. Topic and Queue are JMSDestination objects.
Use the initial context to obtain a TopicConnectionFactory or a
QueueConnectionFactory by supplying the JNDI name for the topic or queue connection
factory.
Use the TopicConnectionFactory to obtain a TopicConnection or
QueueConnectionFactory to obtain a QueueConnection.
Use the TopicConnection to obtain a TopicSession or a QueueConnection to obtain a
QueueSession.
Use the TopicSession to obtain a TopicSubscriber or a TopicPublisher for the required
Topic. Use the QueueSession to obtain a QueueReceiver or a QueueSender for the required
Queue.
The process to look up and create components involves a vendor-supplied context
factory implementation. This introduces vendor dependency in the application clients that
need to use the JNDI lookup facility to locate the enterprise beans and JMS components,
such as topics, queues, and connection factory objects.
Forces
EJB clients need to use the JNDI API to look up EJBHome objects by using the
enterprise bean's registered JNDI name.
JMS clients need to use JNDI API to look up JMS components by using the JNDI
names registered for JMS components, such as connection factories, queues, and topics.
The context factory to use for the initial JNDI context creation is provided by the
service provider vendor and is therefore vendor- dependent. The context factory is also
dependent on the type of object being looked up. The context for JMS is different from the
context for EJB, with different providers.
Lookup and creation of service components could be complex and may be used
repeatedly in multiple clients in the application.
Initial context creation and service object lookups, if frequently required, can be
resource-intensive and may impact application performance. This is especially true if the
clients and the services are located in different tiers.
EJB clients may need to reestablish connection to a previously accessed enterprise
bean instance, having only its Handle object.
Solution
Use a Service Locator object to abstract all JNDI usage and to hide the complexities
of initial context creation, EJB home object lookup, and EJB object re-creation. Multiple
clients can reuse the Service Locator object to reduce code complexity, provide a single
point of control, and improve performance by providing a caching facility.
This pattern reduces the client complexity that results from the client's dependency on
and need to perform lookup and creation processes, which are resource-intensive. To
eliminate these problems, this pattern provides a mechanism to abstract all dependencies
and network details into the Service Locator.

分享到:
评论

相关推荐

    Android-SimpleLocator是一个用于Android轻量级和快速的ServiceLocator

    服务定位器(Service Locator)是一种全局性的依赖查找容器,它存储了应用中的各种服务实例,当需要某个服务时,可以通过服务定位器来获取。相比于传统的构造函数注入或setter注入,服务定位器在某些场景下可以提供...

    用Java实现23种设计模式

    用Java实现23种设计模式 1. 创建型模式 工厂模式(Factory Pattern) ... 服务定位器模式(Service Locator Pattern) 传输对象模式(Transfer Object Pattern) 生产者消费者模式(Producer Consumer Pattern)

    J2EE核心模式.pdf

    5. 服务定位器模式(Service Locator):服务定位器模式提供了一个查找服务对象的途径。在J2EE中,可以使用服务定位器来封装对JNDI(Java Naming and Directory Interface)查找的逻辑。 6. 业务外观模式(Business...

    23种JAVA设计模式和15种J2EE设计模式

    4. **Service Locator模式**:提供服务发现和定位的功能,简化了组件间的依赖。 5. **Front Controller模式**:作为Web应用的单一入口点,负责请求分发。 6. **Model-View-Controller (MVC)模式**:将业务逻辑、...

    33种JAVA设计模式DEMO

    创建型模式 这些设计模式提供了一种在创建对象的同时隐藏创建逻辑的方式,而不是使用 new 运算符直接实例化对象。...服务定位器模式(Service Locator Pattern) 传输对象模式(Transfer Object Pattern)

    J2EE核心模式电子版

    6. **Service Locator模式**:服务定位器帮助应用找到所需的资源和服务,如JNDI(Java Naming and Directory Interface)就是一种常见的实现,提供查找和绑定服务的能力。 7. **Factory模式**:用于创建对象,隐藏...

    Flex轻量级开发框架-Cairngorm介绍

    - `ServiceLocator`:服务定位器,用于定位和管理业务逻辑服务,采用了单例模式。 2. **Commands(命令部分)** - `ICommand`:命令接口,定义了`execute`方法,用于处理特定事件。 - `Command`:实现了`...

    Flex MVC框架 Cairngorm 学习笔记

    Service Locator是用于定位服务和数据访问对象的组件,它定义了与数据源(如HTTPService、WebService、RemoteObject等)的接口。开发者可以通过Service Locator获取到数据访问服务,进而进行数据的读取、写入和更新...

    AJAX 设计模式

    5. **服务定位器模式** - **Service Locator**:提供一个服务定位器对象,用于查找和管理 AJAX 相关的服务,如请求工厂、数据解析器等,减少依赖关系,提高代码可扩展性。 6. **模板模式** - **客户端渲染**:...

    J2EE核心模式

    11. **服务定位器(Service Locator)模式**:用于查找和管理应用程序中的服务,简化服务的查找和使用。 12. **工厂模式**:用于创建对象,隐藏实例化过程,使代码更具灵活性和可扩展性。 13. **代理模式**:在...

    Java Service Locator Information Manager-开源

    服务定位器模式是一种设计模式,用于解耦应用代码和服务的查找逻辑,使得应用程序可以在运行时动态发现和使用服务。 **服务定位器模式** 服务定位器模式是面向服务架构(SOA)中常见的一种模式。在J2EE环境中,它...

    j2ee 核心模式

    - **ServiceLocator**:通过隐藏业务服务查找和创建的复杂性,简化了业务服务工厂的定位。 ##### 3. 集成层模式 集成层模式主要涉及JMS和JDBC技术,用于处理与其他系统或数据库的交互。这部分模式聚焦于如何高效地...

    J2EE的MVC体系结构及其设计模式

    5. 服务定位器(Service Locator):提供一种查找服务的机制,用于解耦服务的消费者和服务提供者。 6. 单例(Singleton):确保一个类只有一个实例,并提供全局访问点,常用于配置对象和缓存服务。 7. 工厂方法...

    IoC 容器和Dependency Injection 模式

    服务定位器模式是一种对象查找机制,对象通过服务定位器获取所需的服务。虽然它也能解决依赖问题,但相比DI,服务定位器增加了额外的查找步骤,降低了代码的透明度,且容易隐藏依赖关系,使得代码更难以理解和测试。...

    flex的mvc模式

    服务定位器允许解耦服务调用和实际的服务实现,使得应用程序更易于维护和测试。 **Cairngorm框架** Cairngorm是Adobe社区开发的一个轻量级MVC框架,特别适合Flex和ActionScript项目。除了上述组件,Cairngorm还包括...

    设计模式.docx

    #### 服务定位器模式(Service Locator Pattern) 服务定位器模式用于查找和返回一个服务对象。这种模式减少了对象之间的耦合,并提高了系统的可配置性。 #### 传输对象模式(Transfer Object Pattern) 传输对象...

    Flex+J2EE实例(cairngorm+blazeDS+hibernate+spring) part4.pdf(完)

    - **`Services.mxml`**:这是一个Flex的MXML文件,用于配置服务定位器,即`ServiceLocator`。通过这个定位器可以方便地管理和访问各种服务对象。 ```xml <cairngorm:ServiceLocator xmlns:fx=...

    j2ee核心模式

    7. 服务定位器模式(Service Locator):提供统一的接口来查找和管理服务,简化依赖注入。 8. 工厂模式(Factory):用于创建对象,隐藏对象创建的复杂性。 三、J2EE架构模式 J2EE架构模式是基于设计模式的更高级别...

Global site tag (gtag.js) - Google Analytics