0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Resource Adapter を Liberty で をためす

Posted at

目的

Resource AdapterアプリケーションをLibertyでためします。

Eclipse の Wizard に任せてみる

メニューで File > New > Connector Project を選択します。

image.png

  • Project name
  • Target Runtime
  • JCA module version

を入力(デフォルトで設定されるものもある)して、プロジェクトを作成します。

この画像では Add project to an EAR をチェックしましたが、あとでやめました。

ra.xml を書いて必要なクラスを確認

Outbound(DataSourceのような)の Resource Adapter を想定して ra.xml を書きます。

ra.xml
<?xml version="1.0" encoding="UTF-8"?>
<connector id="Connector_ID" version="1.7"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/connector_1_7.xsd">
	<description></description>
	<display-name>Pdprof Resource Adapter</display-name>
	<vendor-name>PDPROF</vendor-name>
	<eis-type></eis-type>
	<resourceadapter-version>20251028</resourceadapter-version>
	<resourceadapter>
		<resourceadapter-class>pdprof.ra.PdprofResourceAdapter</resourceadapter-class>
		<outbound-resourceadapter>
			<connection-definition>
				<managedconnectionfactory-class>pdprof.ra.PdprofManagedConnectionFactory</managedconnectionfactory-class>
				<connectionfactory-interface>pdprof.ra.PdprofConnectionFactory</connectionfactory-interface>
				<connectionfactory-impl-class>pdprof.ra.PdprofConnectionFactoryImpl</connectionfactory-impl-class>
				<connection-interface>pdprof.ra.PdprofConnection</connection-interface>
				<connection-impl-class>pdprof.ra.PdprofConnectionImpl</connection-impl-class>
			</connection-definition>
		</outbound-resourceadapter>
	</resourceadapter>
</connector>

Resource Adapterのクラスを作成

を参考に作成します。Reousrce Adapter として使用するのに以下が不足していました。

  • pdprof.ra.PdprofConnectionFactory が ConnectionFactory を extends
  • pdprof.ra.PdprofConnection が Connectio を extends
PdprofConnectionFactory.java
package pdprof.ra;

import java.io.Serializable;

import javax.resource.Referenceable;
import javax.resource.ResourceException;
import javax.resource.cci.Connection;
import javax.resource.cci.ConnectionFactory;

public interface PdprofConnectionFactory extends Serializable, Referenceable, ConnectionFactory {
	public Connection getConnection() throws ResourceException;
}
PdprofConnection.java
package pdprof.ra;

import javax.resource.cci.Connection;

public interface PdprofConnection extends Connection {
	public String hello();
	public String hello(String msg);
	public void close();
}

それ以外についても method の内容などは変更しています。パッケージするのに必要なクラス一覧の参考にしました。

クラスについては以下を参照してください。

以下のクラスを作成します。

PdprofConnectionFactoryImpl.java
PdprofConnectionFactory.java
PdprofConnectionImpl.java
PdprofConnection.java
PdprofManagedConnectionFactory.java
PdprofManagedConnection.java
PdprofManagedConnectionMetaData.java
PdprofResourceAdapter.java

server.xml

Libertyの構成をします。構成のポイントは

  • resourceAdapter id="PDPROFRA" で rar の場所を指定
  • connectionFactory で jndiName を指定、properties.PDPROFRA でリソースをポイント
  • enterpriseApplication の classloader classProviderRef で PDPROFRA を指定してRAR中のクラスをWARから参照
server.xml
<server description="new server">

    <!-- Enable features -->
    <featureManager>
        <feature>jsp-2.3</feature>
        <feature>jca-1.7</feature>
        <feature>jndi-1.0</feature>
        <feature>localConnector-1.0</feature>
    </featureManager>

    <!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" -->
    <httpEndpoint httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint"/>

    <!-- Automatically expand WAR files and EAR files -->
    <applicationManager autoExpand="true"/>

	<resourceAdapter id="PDPROFRA" location="/opt/wlp/usr/servers/resource-adapter-svr/resources/pdprof-resource-adapter.rar"/>
	
	<connectionFactory jndiName="ra/pdprofcf">
		<properties.PDPROFRA/>
	</connectionFactory>
	
    <!-- Default SSL configuration enables trust for default certificates from the Java runtime --> 
    <ssl id="defaultSSLConfig" trustDefaultCerts="true"/>

    <applicationMonitor updateTrigger="mbean"/>

    <enterpriseApplication id="resource-ear" location="resource-ear.ear" name="resource-ear">
    	<classloader classProviderRef="PDPROFRA" />
    </enterpriseApplication>
</server>

Helloサーブレット

サーブレットのポイント

  • Resource アノテーションで JNDIを参照してConnectionFactoryを取得
  • cf.getConnection で Connectionを取得
  • Connection を Resource Adapter中のクラス PdprofConnection に cast して実行します。
HelloServlet.java
package pdprof.ra;

import java.io.IOException;

import javax.annotation.Resource;
import javax.resource.ResourceException;
import javax.resource.cci.ConnectionFactory;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloServlet
 */
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	@Resource(name="ra/pdprofcf")
	ConnectionFactory cf;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			PdprofConnection con = (PdprofConnection)cf.getConnection();
			response.getWriter().append("Message from Resource Adapter : ").append(con.hello());
		} catch (ResourceException e) {
			e.printStackTrace();
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

実行結果

https://github.com/pdprof/resource-adapter/tree/main/resource-adapter-docker/apps
に RAR, EARがあります。

RARとEARをデプロイしたLibertyを起動したら、Helloサーブレットにアクセスします。

以下のような画面が出たら成功です。

image.png

ログには以下が出力されます。

console.log
= PdprofConnectionImpl.hello()
= PdprofConnectionImpl.close()

まとめ

リソースアダプターを実装して、Libertyで実行しました。Docker環境での実行は処理内容を増やしてから作成する予定デス。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?