LoginSignup
7
6

More than 5 years have passed since last update.

Apache ServiceMix メモ

Last updated at Posted at 2016-01-28

ServiceMix のメモ

  • Apache ServiceMix について調べたことをまとめます
  • インストール、クイックスタート、サンプルプログラムの実行を試してみます

  • 内容は間違っている可能性があります

ServiceMix について

ServiceMix を構成するコンポーネント

Apache ActiveMQ

Apache Camel

Apache CXF

  • https://cxf.apache.org/
  • ウェブサービスのフレームワーク
  • JAX-WS, JAX-RS のアプリケーションをホストする

Apache Karaf

Apache Zookeeper

関連項目

OSGi

Spring Framework

Blueprint

Maven のインストール

  • ServiceMix のサンプルコードの実行に Maven が必要になるので、ここでインストールします

ダウンロード

インストール

% mkdir ~/sfw/Maven
% mv apache-maven-3.3.9-bin.tar.gz ~/sfw/Maven
% cd ~/sfw/Maven
% tar zxf apache-maven-3.3.9-bin.tar.gz

環境変数の設定

$ vi ~/.bashrc
export PATH=${HOME}/sfw/Maven/apache-maven-3.3.9/bin:${PATH}
$ . ~/.bashrc

動作確認

$ mvn --version
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T01:41:47+09:00)
...

プロキシーの設定(任意)

  • インターネット接続にプロキシーを経由する場合はプロキシーサーバの設定をします
$ vi ~/.m2/settings.xml 
<settings>
  <proxies>
    <proxy>
      <host>proxy.example.com</host>
      <port>80</port>
    </proxy>
  </proxies>
</settings>

ServiceMix のインストール

ユーザの作成

# usearadd smix
# passwd smix
Password: smix

JavaVM の確認

$ java -version
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
  • インストールされていなかったらインストールします

環境変数の設定

$ vi ~/.bashrc
export JAVA_HOME=/usr/lib/jvm/java
$ . ~/.bashrc

ServiceMix のダウンロード

ServiceMix のインストール

$ mkdir -p ~/sfw//ServiceMix
$ cp apache-servicemix-6.1.0.zip ~/sfw/ServiceMix
$ cd ~/sfw/ServiceMix
$ unzip -q apache-servicemix-6.1.0.zip
$ cd apache-servicemix-6.1.0

ServiceMix の起動確認

$ cd ~/sfw/ServiceMix/apache-servicemix-6.1.0
$ ./bin/servicemix
Please wait while Apache ServiceMix is starting...
100% [========================================================================]

Karaf started in 5s. Bundle stats: 236 active, 236 total
 ____                  _          __  __ _      
/ ___|  ___ _ ____   _(_) ___ ___|  \/  (_)_  __
\___ \ / _ \ '__\ \ / / |/ __/ _ \ |\/| | \ \/ /
 ___) |  __/ |   \ V /| | (_|  __/ |  | | |>  < 
|____/ \___|_|    \_/ |_|\___\___|_|  |_|_/_/\_\

  Apache ServiceMix (6.1.0)

Hit '<tab>' for a list of available commands
and '[cmd] --help' for help on a specific command.
Hit '<ctrl-d>' or 'osgi:shutdown' to shutdown ServiceMix.

karaf@root>

Apache ServiceMix のクイックスタートを試す

コンソール

  • ServiceMix の管理コンソールの使い方を確認します

コンソールのヘルプ

  • help <Category> や help <Command> でヘルプを参照できます

  • help <Category>

karaf@root>help bundle
COMMANDS
bundle:capabilities   Displays OSGi capabilities of a given bundles.     
bundle:classes        Displays a list of classes/resources contained in  
bundle:diag           Displays diagnostic information why a bundle is no 
bundle:dynamic-import Enables/disables dynamic-import for a given bundle.
bundle:find-class     Locates a specified class in any deployed bundle   
bundle:headers        Displays OSGi headers of a given bundles.          
...
  • help <Command>
karaf@root>help bundle:list
DESCRIPTION
        bundle:list

    Lists all installed bundles.

SYNTAX
        bundle:list [options]

OPTIONS
        -t
                Specifies the bundle threshold; bundles with a start-level less 
                than this value will not get printed out.
        --help
                Display this help message
...

各種確認コマンド

karaf@root>help | grep list
bundle:classes                    Displays a list of classes/resources c 
bundle:list                       Lists all installed bundles.           
camel:component-list              Lists all Camel components that are in 
camel:context-list                Lists all Camel contexts.              
camel:endpoint-list               Lists Camel endpoints                  
camel:rest-registry-list          Lists all Camel REST services enlisted 
camel:route-list                  List Camel routes.                     
config:list                       Lists existing configurations.         
config:property-list              Lists properties from the currently ed 
feature:list                      Lists all existing features available  
feature:repo-list                 Displays a list of all defined reposit 
feature:version-list              Lists all versions of a feature availa 
http:list                         Lists details for servlets.            
instance:list                     Lists all existing container instances.
jaas:group-list                   List groups in a realm                 
jaas:pending-list                 List the pending modification on the a 
jaas:realm-list                   List JAAS realms                       
jaas:user-list                    List the users of the selected JAAS re 
kar:create                        Create a kar file for a list of featur 
kar:list                          List the installed KAR files.          
obr:deploy                        Deploys a list of bundles using OBR se 
obr:list                          Lists OBR bundles, optionally providin 
obr:start                         Deploys and starts a list of bundles u 
obr:url-add                       Adds a list of repository URLs to the  
obr:url-list                      Displays the repository URLs currently 
obr:url-remove                    Removes a list of repository URLs from 
service:list                      Lists OSGi services.                   
shell:each                        Execute a closure on a list of argumen 
web:list                          Lists details for war bundles.         
karaf@root>help | grep display
log:display                       Displays log entries.                  
log:exception-display             Displays the last occurred exception f 
log:tail                          Continuously display log entries. Use  

バンドルの確認

  • bundle:list
karaf@root>bundle:list | head
START LEVEL 100 , List Threshold: 50
 ID | State  | Lvl | Version                            | Name                                           
---------------------------------------------------------------------------------------------------------
 97 | Active |  50 | 1.1.1                              | geronimo-annotation_1.0_spec                   
 98 | Active |  50 | 1.1.1                              | geronimo-jms_1.1_spec                          
 99 | Active |  50 | 1.0.1                              | geronimo-j2ee-management_1.1_spec              
100 | Active |  50 | 0.6.4                              | JAXB2 Basics - Runtime                         
101 | Active |  50 | 2.2.11.1                           | Apache ServiceMix :: Bundles :: jaxb-impl      
102 | Active |  50 | 2.4.2                              | Apache Commons Pool                            
103 | Active |  50 | 3.3.0                              | Commons Net

パイプと grep が使えます(実際の結果には色が付きます)

karaf@root>bundle:list | grep camel
136 | Active |  50 | 2.16.1                             | camel-core
137 | Active |  50 | 2.16.1                             | camel-catalog
139 | Active |  50 | 2.16.1                             | camel-jms
140 | Active |  50 | 2.16.1                             | camel-spring
141 | Active |  50 | 2.16.1                             | camel-blueprint
142 | Active |  50 | 5.12.1                             | activemq-camel

ログの確認

karaf@root>log:display
2016-01-25 17:20:27,731 | INFO  | FelixStartLevel  | NamespaceHandlerRegisterer       | 173 - org.apache.cxf.cxf-core - 3.1.4 | Registered blueprint namespace handler for http://cxf.apache.org/blueprint/core
2016-01-25 17:20:27,732 | INFO  | FelixStartLevel  | NamespaceHandlerRegisterer       | 173 - org.apache.cxf.cxf-core - 3.1.4 | Registered blueprint namespace handler for http://cxf.apache.org/configuration/beans
2016-01-25 17:20:27,733 | INFO  | FelixStartLevel  | NamespaceHandlerRegisterer       | 173 - org.apache.cxf.cxf-core - 3.1.4 | Registered blueprint namespace handler for http://cxf.apache.org/configuration/parameterized-types
2016-01-25 17:20:27,734 | INFO  | FelixStartLevel  | NamespaceHandlerRegisterer       | 173 - org.apache.cxf.cxf-core - 3.1.4 | Registered blueprint namespace handler for http://cxf.apache.org/configuration/security
2016-01-25 17:20:27,735 | INFO  | FelixStartLevel  | NamespaceHandlerRegisterer       | 173 - org.apache.cxf.cxf-core - 3.1.4 | Registered blueprint namespace handler for http://schemas.xmlsoap.org/wsdl/

execption のみを表示する

karaf@root>log:exception-display

ログの出力レベルを変更する

karaf@root>log:get
Logger | Level
--------------
ROOT   | INFO 
karaf@root>log:set DEBUG
karaf@root>log:display | grep DEBUG | head
2016-01-25 17:58:00,671 | DEBUG | pe-[log:display] | configadmin                      | 6 - org.apache.felix.configadmin - 1.8.4 | getProperties()
2016-01-25 17:58:00,671 | DEBUG | pe-[log:display] | configadmin                      | 6 - org.apache.felix.configadmin - 1.8.4 | getProperties()
2016-01-25 17:58:00,671 | DEBUG | pipe-[head, +5]  | configadmin                      | 6 - org.apache.felix.configadmin - 1.8.4 | getProperties()
2016-01-25 17:58:00,672 | DEBUG | pe-[grep, DEBUG] | configadmin                      | 6 - org.apache.felix.configadmin - 1.8.4 | getProperties()
2016-01-25 17:58:00,672 | DEBUG | pe-[log:display] | configadmin                      | 6 - org.apache.felix.configadmin - 1.8.4 | getProperties()
...

ログレベルを元に戻す

karaf@root>log:get
Logger | Level
--------------
ROOT   | DEBUG
karaf@root>log:set INFO
karaf@root>log:get
Logger | Level
--------------
ROOT   | INFO 

Camel

  • Apache Camel はルールに基づいてルーティングや調停をするコンポーネントです
  • このチュートリアルでは Blueprint XML ファイルを使ってルーティングを定義します

Blueprint XML ファイルの用意

  • この例では camel/input ディレクトリに配置されたファイルを camel/output ディレクトリに自動的に移動するというルーティングルールを設定します
  • ファイル名は quickstart1.xml とし、deploy ディレクトリの下に配置します
  • ルートディレクトリは ServiceMix をインストールしたディレクトリです
deploy/quickstart1.xml
<?xml version="1.0" encoding="UTF-8"?>
<blueprint
    xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
      http://www.osgi.org/xmlns/blueprint/v1.0.0
      http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

    <camelContext xmlns="http://camel.apache.org/schema/blueprint">
      <route>
        <from uri="file:camel/input"/>
        <log message="Moving ${file:name} to the output directory"/>
        <to uri="file:camel/output"/>
      </route>
    </camelContext>

</blueprint>
  • route タグの中がルーティングルールです
  • from uri= で指定されている file:camel/input が移動元のディレクトリ
  • to uri= で指定されている file:camel/output が移動先のディレクトリ
  • log message= でログを出力します

Blueprint XML ファイルの読み込み確認

  • deploy/quickstart1.xml ファイルは自動で読み込まれます
karaf@root>log:display
...
2016-01-26 14:07:23,584 | INFO  | mix-6.1.0/deploy | fileinstall                      | 7 - org.apache.felix.fileinstall - 3.5.0 | Installing bundle quickstart1.xml / 0.0.0
2016-01-26 14:07:24,345 | WARN  | mix-6.1.0/deploy | DefaultTypeConverter             | 136 - org.apache.camel.camel-core - 2.16.1 | Overriding type converter from: StaticMethodTypeConverter: public static org.apache.activemq.command.ActiveMQDestination org.apache.activemq.camel.converter.ActiveMQConverter.toDestination(java.lang.String) to: StaticMethodTypeConverter: public static org.apache.activemq.command.ActiveMQDestination org.apache.activemq.camel.converter.ActiveMQConverter.toDestination(java.lang.String)
2016-01-26 14:07:24,345 | WARN  | mix-6.1.0/deploy | DefaultTypeConverter             | 136 - org.apache.camel.camel-core - 2.16.1 | Overriding type converter from: InstanceMethodTypeConverter: public org.apache.camel.Processor org.apache.activemq.camel.converter.ActiveMQMessageConverter.toProcessor(javax.jms.MessageListener) to: InstanceMethodTypeConverter: public org.apache.camel.Processor org.apache.activemq.camel.converter.ActiveMQMessageConverter.toProcessor(javax.jms.MessageListener)
2016-01-26 14:07:24,345 | WARN  | mix-6.1.0/deploy | DefaultTypeConverter             | 136 - org.apache.camel.camel-core - 2.16.1 | Overriding type converter from: InstanceMethodTypeConverter: public org.apache.activemq.command.ActiveMQMessage org.apache.activemq.camel.converter.ActiveMQMessageConverter.toMessage(org.apache.camel.Exchange) throws javax.jms.JMSException to: InstanceMethodTypeConverter: public org.apache.activemq.command.ActiveMQMessage org.apache.activemq.camel.converter.ActiveMQMessageConverter.toMessage(org.apache.camel.Exchange) throws javax.jms.JMSException
2016-01-26 14:07:24,376 | INFO  | mix-6.1.0/deploy | BlueprintCamelContext            | 136 - org.apache.camel.camel-core - 2.16.1 | Apache Camel 2.16.1 (CamelContext: camel-1) is starting
2016-01-26 14:07:24,377 | INFO  | mix-6.1.0/deploy | ManagedManagementStrategy        | 136 - org.apache.camel.camel-core - 2.16.1 | JMX is enabled
2016-01-26 14:07:24,455 | INFO  | mix-6.1.0/deploy | DefaultRuntimeEndpointRegistry   | 136 - org.apache.camel.camel-core - 2.16.1 | Runtime endpoint registry is in extended mode gathering usage statistics of all incoming and outgoing endpoints (cache limit: 1000)
2016-01-26 14:07:24,520 | INFO  | mix-6.1.0/deploy | BlueprintCamelContext            | 136 - org.apache.camel.camel-core - 2.16.1 | AllowUseOriginalMessage is enabled. If access to the original message is not needed, then its recommended to turn this option off as it may improve performance.
2016-01-26 14:07:24,520 | INFO  | mix-6.1.0/deploy | BlueprintCamelContext            | 136 - org.apache.camel.camel-core - 2.16.1 | StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
2016-01-26 14:07:24,566 | INFO  | mix-6.1.0/deploy | BlueprintCamelContext            | 136 - org.apache.camel.camel-core - 2.16.1 | Route: route1 started and consuming from: Endpoint[file://camel/input]
2016-01-26 14:07:24,567 | INFO  | mix-6.1.0/deploy | BlueprintCamelContext            | 136 - org.apache.camel.camel-core - 2.16.1 | Total 1 routes, of which 1 is started.
2016-01-26 14:07:24,570 | INFO  | mix-6.1.0/deploy | BlueprintCamelContext            | 136 - org.apache.camel.camel-core - 2.16.1 | Apache Camel 2.16.1 (CamelContext: camel-1) started in 0.192 seconds
2016-01-26 14:07:24,580 | INFO  | mix-6.1.0/deploy | fileinstall                      | 7 - org.apache.felix.fileinstall - 3.5.0 | Started bundle: blueprint:file:/root/ServiceMix/apache-servicemix-6.1.0/deploy/quickstart1.xml

ディレクトリが作成されたことの確認

  • 自動で camel/input ディレクトリが作成されます
# ls camel
input

ファイルの移動を確認する

  • camel/input 以下にファイルを配置すると、自動的に camel/output 以下に移動(ルーティング)されます
# touch camel/input/testfile.xml
# ls camel/input/testfile.xml
ls: cannot access camel/input/testfile.xml: No such file or directory
# ls camel/output/
testfile.xml
  • ファイルの移動はログにも出力されます
karaf@root>log:display | tail
...
2016-01-26 14:20:36,967 | INFO  | le://camel/input | route1                           | 136 - org.apache.camel.camel-core - 2.16.1 | Moving testfile.xml to the output directory

バンドルに登録されていることを確認する

  • deploy/quickstart1.xml に配置した Blueprint XML がバンドルとして登録されていることを確認します
karaf@root>bundle:list | grep quickstart1.xml
237 | Active |  80 | 0.0.0                              | quickstart1.xml                                

バンドルを停止する

  • bundle:stop <ID> でバンドルを停止できます
karaf@root>bundle:stop 237
  • ステータスが Resolved に変更されました
karaf@root>bundle:list | grep quickstart1.xml
237 | Resolved |  80 | 0.0.0                              | quickstart1.xml                                
  • ファイルも移動されなくなります
# touch camel/input/testfile2.xml
# ls camel/input/testfile2.xml
camel/input/testfile2.xml
  • 停止したバンドルは bundle:start <ID> で再開できます
karaf@root>bundle:start 237
  • ステータスが再び Active になりました
karaf@root>bundle:list | grep quickstart1.xml
237 | Active |  80 | 0.0.0                              | quickstart1.xml                                

ActiveMQ

  • ActiveMQ は Java Message Service (JMS) のブローカーです
  • 以下の例では、ファイル移動のイベントを ActiveMQ に通知し、ログに出力します

Blueprint XML ファイル を準備する

  • ファイルの移動を ActiveMQ に通知する Blueprint XML ファイルを準備します
  • deploy/quickstart2.xml ファイルを以下のように記述します
deploy/quickstart2.xml
<?xml version="1.0" encoding="UTF-8"?>
<blueprint
    xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
      http://www.osgi.org/xmlns/blueprint/v1.0.0
      http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

    <camelContext xmlns="http://camel.apache.org/schema/blueprint">
      <route>
        <from uri="file:activemq/input"/>
        <to uri="file:activemq/output"/>

        <setBody>
          <simple>
            FileMovedEvent(file: ${file:name}, timestamp: ${date:now:hh:MM:ss.SSS})
          </simple>
        </setBody>
        <to uri="activemq://events" />
      </route>
    </camelContext>
</blueprint>
  • activemq/input に来たファイルを activemq/output に移動します
  • ファイル名とタイムスタンプを含んだメッセージを ActiveMQ のキューに追加します

バンドルの確認

  • bundle:list でバンドルとして登録されたことを確認します
karaf@root>bundle:list | tail
...
238 | Active |  80 | 0.0.0                              | quickstart2.xml                                

ファイルの移動の確認

# ls activemq/input/
# touch activemq/input/testfile.txt
# ls activemq/input/
# ls activemq/output
testfile.txt

ActiveMQ のキューの中身を取り出す Blueprint XML ファイルの準備

  • ActiveMQ のキューに入っているメッセージをログに出力する Blueprint XML ファイルを作成します
  • deploy/quickstart3.xml を以下のように記述します
deploy/quickstart3.xml
<?xml version="1.0" encoding="UTF-8"?>
<blueprint
    xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
      http://www.osgi.org/xmlns/blueprint/v1.0.0
      http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

    <camelContext xmlns="http://camel.apache.org/schema/blueprint">
      <route>
        <from uri="activemq://events"/>
        <to uri="log:events"/>
      </route>
    </camelContext>
</blueprint>

ログの確認

  • log:display コマンドでログに出力されていることを確認します
karaf@root>log:display | tail
...
2016-01-26 16:17:48,800 | INFO  | Consumer[events] | events                           | 136 - org.apache.camel.camel-core - 2.16.1 | Exchange[ExchangePattern: InOnly, BodyType: String, Body: FileMovedEvent(file: testfile.txt, timestamp: 04:01:22.038)]

機能の管理

機能の確認

  • feature:list で機能一覧を表示できます
karaf@root>feature:list | grep webconsole
webconsole                              | 3.0.5            |           | standard-3.0.5              | Base support of the Karaf WebConsole              
  • feature:info で詳細情報を確認できます
karaf@root>feature:info webconsole
Feature webconsole 3.0.5
Description:
  Base support of the Karaf WebConsole
Feature configuration:
  org.apache.karaf.webconsole
Feature has no configuration files
Feature depends on:
  http 0.0.0
Feature contains followed bundles:
  mvn:org.apache.felix/org.apache.felix.metatype/1.0.12 start-level=30
  mvn:org.apache.karaf.webconsole/org.apache.karaf.webconsole.branding/3.0.5 start-level=30
  mvn:org.apache.karaf.webconsole/org.apache.karaf.webconsole.console/3.0.5 start-level=30
Feature contains followed conditionals:
Conditional(eventadmin/0.0.0) has no configuration
Conditional(eventadmin/0.0.0) has no configuration files
Conditional(eventadmin/0.0.0) has no dependencies.
Conditional(eventadmin/0.0.0) contains followed bundles:
  mvn:org.apache.felix/org.apache.felix.webconsole.plugins.event/1.1.2 start-level=30
Conditional(scr/0.0.0) has no configuration
Conditional(scr/0.0.0) has no configuration files
Conditional(scr/0.0.0) has no dependencies.
Conditional(scr/0.0.0) contains followed bundles:
  mvn:org.apache.felix/org.apache.felix.webconsole.plugins.ds/1.0.0 start-level=30

機能の追加

  • feature:install で機能を追加できます
karaf@root>feature:install webconsole
karaf@root>feature:list | grep webconsole
webconsole                              | 3.0.5            | x         | standard-3.0.5              | Base support of the Karaf WebConsole
  • webconsole を追加するには Maven のリポジトリにアクセスできる必要があります

  • webconsole をインストールすると http://localhost:8181/system/console から smx/smx でログインして操作できるようになります

  • 機能の削除は feature:uninstall です

karaf@root>feature:uninstall webconsole
karaf@root>feature:list | grep webconsole
webconsole                              | 3.0.5            |           | standard-3.0.5              | Base support of the Karaf WebConsole

サンプルコードを試す

用意されているサンプルコードの一覧

karaf@root>feature:list | grep example
examples-activemq-camel-blueprint       | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-cxf-osgi                       | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-cxf-jaxrs                      | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-cxf-jaxrs-blueprint            | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-cxf-jaxws-blueprint            | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-camel-sql-derby                | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-camel-sql-pgsql                | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-camel-sql-h2                   | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-camel-osgi                     | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-camel-blueprint                | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-camel-drools                   | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-camel-drools-blueprint         | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-camel-cxf-rest                 | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-camel-cxf-soap                 | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-cxf-ws-addressing              | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-cxf-ws-security-osgi           | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-cxf-ws-security-blueprint      | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-cxf-ws-security-signature      | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-cxf-ws-rm                      | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-cxf-wsn-receive                | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-cxf-wsn-notifier               | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-activiti-camel                 | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-akka-camel                     | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-drools-simple                  | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-drools-camel-blueprint         | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-drools-spring                  | 6.1.0            |           | servicemix-examples-6.1.0   |                                                   
examples-drools-camel-cxf-server        | 6.1.0            |           | servicemix-examples-6.1.0   |

CXF JAX-RS を試す

詳細情報をみる

karaf@root>feature:info examples-cxf-jaxrs
Feature examples-cxf-jaxrs 6.1.0
Feature has no configuration
Feature has no configuration files
Feature depends on:
  cxf 3.1.4
Feature contains followed bundles:
  mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.spring-beans/3.2.14.RELEASE_1
  mvn:commons-codec/commons-codec/1.10
  mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.commons-httpclient/3.1_7
  mvn:org.apache.servicemix.examples/cxf-jaxrs/6.1.0
Feature has no conditionals.

Jetty

  • アプリケーションサーバは Jetty が組み込まれているようです
karaf@root>feature:list | grep -i jetty
cxf-http-jetty                          | 3.1.4            | x         | cxf-3.1.4                   |                                                   
jetty                                   | 8.1.15.v20140411 | x         | standard-3.0.5              |                                                   
pax-jetty                               | 8.1.17.v20150415 | x         | org.ops4j.pax.web-3.2.6     | Provide Jetty engine support                      
camel-jetty                             | 2.16.1           |           | camel-2.16.1                |                                                   
camel-jetty9                            | 2.16.1           |           | camel-2.16.1                |                                                   

サンプルコードをインストールする

karaf@root>feature:install examples-cxf-jaxrs
karaf@root>feature:list | grep examples-cxf-jaxrs
examples-cxf-jaxrs                      | 6.1.0            | x         | servicemix-examples-6.1.0   |
karaf@root>bundle:list | grep Examples
238 | Active |  80 | 6.1.0                              | Apache ServiceMix :: Examples :: CXF JAX-RS OSGI  

WADL を確認する

$ curl 'http://localhost:8181/cxf/crm/customerservice?_wadl&_type=xml'
<application xmlns="http://wadl.dev.java.net/2009/02" xmlns:xs="http://www.w3.org/2001/XMLSchema"><grammars><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="unqualified">
<xs:element name="Customer" type="customer"/>
<xs:element name="Order" type="order"/>
<xs:complexType name="order">
<xs:sequence>
<xs:element minOccurs="0" name="description" type="xs:string"/>
<xs:element name="id" type="xs:long"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="customer">
<xs:sequence>
<xs:element name="id" type="xs:long"/>
<xs:element minOccurs="0" name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</grammars><resources base="http://localhost:8181/cxf/crm"><resource path="/customerservice/"><resource path="customers/"><method name="POST"><request><representation mediaType="*/*"/></request><response><representation mediaType="*/*"/></response></method><method name="PUT"><request><representation mediaType="*/*"/></request><response><representation mediaType="*/*"/></response></method></resource><resource path="customers/{id}/"><param name="id" style="template" type="xs:string"/><method name="DELETE"><request></request><response><representation mediaType="*/*"/></response></method><method name="GET"><request></request><response><representation mediaType="application/xml"/></response></method></resource><!-- Dynamic subresource --><resource path="/orders/{orderId}/"><param name="orderId" style="template" type="xs:string"/></resource></resource></resources></application>

サービスにアクセスする

$ curl 'http://localhost:8181/cxf'
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><HTML><HEAD><LINK type="text/css" rel="stylesheet" href="/cxf/?stylesheet=1"><meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>CXF - Service list</title></head><body><span class="heading">Available SOAP services:</span><br/><table cellpadding="1" cellspacing="1" border="1" width="100%"></table><br/><br/><span class="heading">Available RESTful services:</span><br/><table cellpadding="1" cellspacing="1" border="1" width="100%"><tr><td><span class="field">Endpoint address:</span> <span class="value">http://localhost:8181/cxf/crm</span><br/><span class="field">WADL :</span> <a href="http://localhost:8181/cxf/crm?_wadl">http://localhost:8181/cxf/crm?_wadl</a></td></tr></table></body></html>

Java のクライアントプログラムからアクセスする

  • コンパイル
$ cd ~/sfw/ServiceMix/apache-servicemix-6.1.0/examples/cxf/cxf-jaxrs
$ mvn compile
  • 実行
$ mvn exec:java
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Apache ServiceMix :: Examples :: CXF JAX-RS OSGI 6.1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:1.3.2:java (default-cli) @ cxf-jaxrs ---
[WARNING] Warning: killAfter is now deprecated. Do you need it ? Please comment on MEXEC-6.
Sent HTTP GET request to query customer info
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Customer><id>123</id><name>Mary</name></Customer>


Sent HTTP GET request to query sub resource product info
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Product><description>product 323</description><id>323</id></Product>


Sent HTTP PUT request to update customer info
Response status code: 200
Response body: 



Sent HTTP POST request to add customer
Response status code: 200
Response body: 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Customer><id>126</id><name>Jack</name></Customer>

Curl からアクセスする

カスタマーの追加

$ cat src/main/resources/org/apache/servicemix/examples/cxf/jaxrs/client/add_customer.xml
<!--

    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
<Customer>
  <name>Jack</name>
</Customer> 
$ curl -X POST -T src/main/resources/org/apache/servicemix/examples/cxf/jaxrs/client/add_customer.xml -H "Content-Type: text/xml" http://localhost:8181/cxf/crm/customerservice/customers
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Customer><id>127</id><name>Jack</name></Customer>
$ curl http://localhost:8181/cxf/crm/customerservice/customers/127<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Customer><id>127</id><name>Jack</name></Customer>

カスタマーの修正

$ curl http://localhost:8181/cxf/crm/customerservice/customers/123<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Customer><id>123</id><name>Mary</name></Customer>
$ cat 'src/main/resources/org/apache/servicemix/examples/cxf/jaxrs/client/update_customer.xml'
<!--

    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
<Customer>
  <name>Mary</name>
  <id>123</id>
</Customer> 
$ curl -X PUT -T src/main/resources/org/apache/servicemix/examples/cxf/jaxrs/client/update_customer.xml -H "Content-Type: text/xml" http://localhost:8181/cxf/crm/customerservice/customers 
$ curl http://localhost:8181/cxf/crm/customerservice/customers/123
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Customer><id>123</id><name>Mary</name></Customer>

カスタマーの削除

$ curl -X DELETE http://localhost:8181/cxf/crm/customerservice/customers/127
$ curl http://localhost:8181/cxf/crm/customerservice/customers/127

アクセスパスを変更する

設定ファイルで変更する

$ vi etc/org.apache.cxf.osgi.cfg
org.apache.cxf.servlet.context=/custom

確認

$ curl http://localhost:8181/cxf/crm/customerservice/customers/123
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 404 Not Found</title>
</head>
<body>
<h2>HTTP ERROR: 404</h2>
<p>Problem accessing /cxf/crm/customerservice/customers/123. Reason:
<pre>    Not Found</pre></p>
<hr /><i><small>Powered by Jetty://</small></i>
....
</body>
</html>
$ curl http://localhost:8181/custom/crm/customerservice/customers/123
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Customer><id>123</id><name>John</name></Customer>

コンソールから変更

karaf@root>config:edit org.apache.cxf.osgi
karaf@root>config:property-set org.apache.cxf.servlet.context /super
karaf@root>config:update

確認

$ curl http://localhost:8181/custom/crm/customerservice/customers/123
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 404 Not Found</title>
</head>
<body>
<h2>HTTP ERROR: 404</h2>
<p>Problem accessing /custom/crm/customerservice/customers/123. Reason:
<pre>    Not Found</pre></p>
<hr /><i><small>Powered by Jetty://</small></i>
...
</body>
</html>
$ curl http://localhost:8181/super/crm/customerservice/customers/123
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Customer><id>123</id><name>John</name></Customer>

サンプルプログラムを停止する

karaf@root>bundle:list | grep CXF
238 | Active |  80 | 6.1.0                              | Apache ServiceMix :: Examples :: CXF JAX-RS OSGI  
karaf@root>bundle:stop 238
karaf@root>bundle:list | grep CXF
238 | Resolved |  80 | 6.1.0                              | Apache ServiceMix :: Examples :: CXF JAX-RS OSGI

サンプルプログラムをアンインストールする

アンインストール

karaf@root>feature:uninstall examples-cxf-jaxrs

確認

karaf@root>bundle:list | grep CXF
karaf@root>feature:list | grep examples-cxf-jaxrs
examples-cxf-jaxrs                      | 6.1.0            |           | servicemix-examples-6.1.0   |
7
6
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
7
6