Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.このコンテンツは選択した言語では利用できません。
Chapter 8. Service Implementations
8.1. Bean リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
8.1.1. Bean Service Component リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
The Bean Component is a pluggable container in SwitchYard which allows Java classes (or beans) to provide and consume services. This means that you can implement a service by simply annotating a Java class. It also means you can consume a service by injecting a reference to that service directly into your Java class.
Note
In a SwitchYard Beans service implementation
java:comp/BeanManager
lookup via JNDI is not supported. You can use @InjectBeanManager
, which is supported.
8.1.2. Bean Services リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
Bean Services are standard CDI beans with a few extra annotations. This also opens up the possibilities of how SwitchYard is used; you can now expose existing CDI-based beans in your application as services to the outside world or consume services within your bean.
8.1.3. Create a Bean Service リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
Prerequisites
- Name: the name of the Java class for your bean service.
- Service Name: the name of the service your bean provides.
- Interface: the contract for the service being provided. Java is the only valid interface type for bean services.
Procedure 8.1. Task
- Create a new Bean Service Class in the SwitchYard Editor JBoss Developer Studio plug-in.NoteIf you provide the interface value first, it automatically generates default names for Name and Service Name.
Figure 8.1. Creating a New Bean Service
The example above showsExampleBean
as the name of the class andcom.example.switchyard.docs.Example
as the interface. - Click.Your new class looks like this:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The @Service annotation allows the SwitchYard CDI Extension to discover your bean at runtime and register it as a service. The value of the annotation (Example.class
in the above example) represents the service contract for the service. Every bean service must have an @Service annotation with a value identifying the service interface for the service. - After creating a bean service, complete the service definition by adding one or more operations to the service interface and a corresponding implementation in your bean:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
8.1.4. Providing a Service リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
Procedure 8.2. Task
- In order to provide a service with the Bean component, add an @Service annotation to your bean:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow TheSimpleService
interface represents the Service Interface that defines the service operations exposed by SwitchYard.
8.1.5. @Service リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
The @Service annotation allows the SwitchYard CDI Extension to discover your bean at runtime and register it as a service. The value of the annotation (SimpleService.class in the above example) represents the service contract for the service. Every bean service must have an @Service annotation with a value identifying the service interface for the service.
The
META-INF/beans.xml
file in your deployed application tells the JBoss application server to look for beans in this application and to activate the CDI. The SwitchYardCDIServiceDiscovery
CDI extension picks up the @Service beans and makes them available to the application deployer. The service can now be invoked from other services within SwitchYard or bound to a wire protocol through SwitchYard gateways.
8.1.6. Consuming a Service リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
Procedure 8.3. Task
- In order to consume a SwitchYard service from within a CDI bean, add a @Reference annotation to your bean:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow By default, SwitchYard expects a service reference to be declared with a name which matches the Java type used for the reference. In the above example, the SimpleService type expects a service reference called "SimpleService" in your SwitchYard configuration. However, the @Reference annotation also accepts a service name if the service reference name does not match the Java type name of the contract. For example:@Reference("urn:myservices:purchasing:OrderService") private OrderService orders;
@Reference("urn:myservices:purchasing:OrderService") private OrderService orders;
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
8.1.7. @Reference リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
The @Reference annotation enables CDI beans to consume other services. The reference can point to services provided in the same application by other implementations, or to a service that is hosted outside of SwitchYard and exposed over JMS, SOAP, or FTP. The JBoss application server routes the invocations made through this reference through the SwitchYard exchange mechanism.
8.1.8. ReferenceInvoker リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
Although the @Reference annotation injects a reference using the Java interface of the reference contract, it does not allow you to use SwitchYard API constructs like the
Message
and Context
interfaces.
When invoking a reference from a Bean service, the ReferenceInvoker enables you to access an attachment or a context property.
To use a ReferenceInvoker, replace the service contract interface type with a ReferenceInvoker type. It allows SwitchYard to inject the correct instance automatically.
Note
Red Hat recommends you create a new instance of ReferenceInvocation each time you want to invoke a service using ReferenceInvoker.
For example, here is an instance which uses a ReferenceInvoker to invoke SimpleService.
8.1.9. Invocation Properties リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
While it is a best practice to write your service logic to the data that is defined in the contract (the input and output message types), there can be situations where you need to access contextual information like message headers such as received file name in your implementation. To facilitate this, the Bean component allows you to access the SwitchYard Exchange Context instance associated with a given Bean Service Operation invocation.
Invocation properties represent the contextual information (like message headers) in your bean implementation.
8.1.10. Accessing Invocation Properties リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
Procedure 8.4. Task
- To enable access to the invocation properties, add a
Context
property to your bean and annotate it with the CDI @Inject annotation:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Here, the Context interface allows your bean logic to get and set properties in the context.NoteYou can invoke the Context instance only within the scope of one of the Service Operation methods. If you invoke it outside this scope, it results in an UnsupportedOperationException error.
8.1.11. @Inject リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
The @Inject annotation lets you define an injection point that is injected during bean instantiation. Once your beans are registered as Services, they can be injected as CDI beans using @Inject annotation.
8.1.12. Implementation Properties リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
Implementation properties represent environmental properties that are defined in the SwitchYard application descriptor (
switchyard.xml
) for your bean implementation.
8.1.13. Accessing Implementation Properties リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
Implementation properties represent environmental properties that you have defined in the SwitchYard application descriptor (
switchyard.xml
) for your bean implementation. Implementation properties in SwitchYard are the properties that you can configure on a specific service implementation. That is, you can make the property value available to service logic executing inside an implementation container. Here is an example:
Procedure 8.5. Task
- To access the Implementation Properties, add an @Property annotation to your bean class identifying the property you want to inject:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Here, the @Property annotation is used for injecting theuser.name
property.
8.1.14. @Property リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
The @Property annotation enables you to identify the implementation property that you want to inject in a bean.