10 things :
   How to enhance
   the power of JRoR
   using Java EE technology

     橋本 吉治
      HASHIMOTO                            Yoshiharu
                                                       Japan Java User Group
                                                                   @hasssie


     JRubyKaigi2010 28th Aug
http://www.flickr.com/photos/shinyai/335249511/                                1
自己紹介
                                              who am I
       Rubyを愛するJava開発者
                                      Java guy loves Ruby
                               JJUG / 卓球.rb
            a member of Japan Java User Groups,
                      TableTennis.rb
                                             某SIer勤務
           working at a big IT service company
http://www.flickr.com/photos/vs/186075406/                  2
Also I am a writer
[How to develop JRuby on Rails]




                                  3
1.    Java App Server
2.    JAX-WS API
3.    JMS API
4.    EJB
5.    JDBC driver
6.    JMX
7.    Spring Framework
8.    Java IDE
9.    Java OSS library
10.   Java build tool like Ant
                                 4
5
エンタープライズ・システム
         で大事なことを
          考えてみた
                 Think about what
                important things for
                 enterprise system
http://www.flickr.com/photos/marcinmoga/4572263486/in/photostream/   6
エンタープライズシステム
  は多くのシステムが
    連動して動く
Lots of systems are connecting
   each other on enterprise
            systems
                                 7
JRoRシステムも
繋がらないと



                                                     JRoR system
                                                must connect with
                                                   other systems
http://www.flickr.com/photos/yngrich/8828592/                       8
繋がるために3つの
                                                       Java EE 技術の
                                                             力を使う


                                                        3 things about
                                                               Java EE
                                             for connecting each other
http://www.flickr.com/photos/marvinkuo/2153147300/                       9
本で書けなかったことにも言及
 Mention to what I couldn’t
  write down on my book
      1. JAX-WS API.
      2. JMS API.
      3. EJB.

                              10
about JAX-WS・・・



http://www.flickr.com/photos/shinyai/335249511/   11
空前の SOAP ブーム
                 ※エンタープライズシステム限定

           The SOAP boom spread
                 like wildfire
       just only on enterprise system
http://www.flickr.com/photos/usfwssoutheast/4642078354/   12
• SOAP Webサービス実装の安定化
• SOAでレガシー再構築という夢
• SOAPで繋げるクラウド基盤多数

• stabilize SOAP Web Service impl
• fantasy of restructuring of legacy system
  using SOA
• SOAP connect with cloud computing
  base
                                              13
Rails+SOAP
       Railsは、SOAPを捨てた
  RubyとXMLの相性が「よい」とはいえない


        Rails dump SOAP;
not get along with Ruby and XML
             REXML
                              14
+
http://http://www.flickr.com/photos/pinksherbet/3038760752/   15
メリット
  JAX-WSは扱いやすく、比較的高速
 NetBeansなどツールサポートが手厚い

      JAX-WS is easy to use,
faster to access and have lots of
  support tools like NetBeans.
                                    16
STEP1: WSDLからコード生成してjarに固める
STEP2: 下記のコードを書く
Step1: generate from WSDL and pack into jar
Step2: write code as follows;
     require 'java'
     require 'jar/UsersManagerWSClient'
     import ‘ws.client.UsersManagerService’
     class UsersManager
         def initialize
          @service = 
             UsersManagerService.new.get_users_manager_port
         end

        def find(user_id); transform(@service.find(user_id.to_s)); end

        private
        def transform(jobj)
         # Java オブジェクト ⇒ Rubyオブジェクトの変換
        end
     end
                                                                         17
Step1: XMLスキーマを設計
Step2: xjcコマンドを使ってJavaクラスの生成
Step3:下記のコードを書く


                         def unmarshal
                           ctx = JAXBContext.newInstance("net.pkg.class") 
                           um = ctx.createUnmarshaller() 
                           gbc = (GeneratedBindingClass) um.unmarshal(file) 
                           xmlVal = gbc.getXMLValueOfInterest() 
                          end




Step1: Design XML schema
Step2: generate Java class using xjc command
Step3: write code above
                                                                               18
about JMS・・・


http://www.flickr.com/photos/shinyai/335249511/   19
RailsからMoMを使いたい
安定した非同期通信基盤を使いたい
            Message oriented Middleware
                Mn   ・・・・   M2   M1




    Rails                                 other system

want to use MoM from Rails,
    want to use stable
  async-messaging base
                                                         20
非同期通信のメリット
      ユーザーにいち早く応答できる
        他システム連携が容易
                           time


        JRoR
        other    message
       systems


the merit of async-messaging
     prompt response to users
  easy to collaborate other systems
                                      21
Rails+非同期通信
AP4R、ActiveMessagingなどがあるが、WMQ
     など安定した基盤を使いたい


  Rails + async-messaging
 AP4R, ActiveMessaging and so on...
But want to use stable base like WMQ

                                       22
+
http://http://www.flickr.com/photos/pinksherbet/3038760752/   23
メリット
JMSをサポートするMoMが多い
トランザクション処理が容易に書ける
ActiveMessagingもJMSをサポート


               merit
Many MoMs support JMS
Easy to develop for transaction function
Also ActiveMessaging support JMS
                                           24
例)メッセージ送信
ex) sending messages

 Rails    JMS client
Web App




     JRoR              EJB/Web Service Container
    Mongrel               Java Application Server   other systems


                                                                    25
STEP1.下記のコード or ActiveMessagingを使う
         import javax.jms.Session;
         import javax.naming.InitialContext;
         class UsersRegister
           def initialize
            @context = InitialContext.new
           end

          def regist(user, ut)
           begin
             cf = @context .lookup "jms/UsersRegisterQConFactory"
             que = @context .lookup "jms/UsersRegisterQueue"
             con = cf.create_queue_connection
             qs =
               con.create_queue_session(
                false,
                Session.AUTO_ACKNOWLEDGE
             )        
             sender = qs.create_sender(que)            
             msg = qs.create_text_message
             msg.set_text(create_msg(user))
             sender.send(txt);
           rescue


Step1: Use this code above or ActiveMessaging
                                                                    26
では、メッセージを
           受け取るには?
                         Then how to receive
                             messages?

http://www.flickr.com/photos/marcinmoga/4240686102/   27
MDBで受信、RESTでJRoRへ
                        Web
       SOAP client     Service Session Bean

  Rails 
 Web App

           REST I/F
                               Message-driven
                                   Bean
      JRoR            EJB/Web Service Container
     Mongrel             Java Application Server   other systems


 MDB receive messages
 and use REST to JRoR
                                                                   28
about EJB ・・・

                                                  29
http://www.flickr.com/photos/shinyai/335249511/        29
Railsから2pcを使いたい!
             2 phase commit = prepare(1) + commit(2)


             process1         process1‐1


                                           other sytem1
 Rails App
                              process1‐2


                                           other sytem2


want to use 2pc from Rails?
                                                          30
+
http://http://www.flickr.com/photos/pinksherbet/3038760752/   31
メリット
 XAを使った分散トランザクションが可能
トランザクション処理コードの複雑さが緩和


               merit
can develop a distributed transaction
system using XA without any
complicated messy transaction code
                                        32
典型的な使い方
                              RDB



                EJB                   MoM
                              Mn    ・・・・   M2   M1
Rails App
            Java App Server                          other system



            typical usage
                                                                    33
でも、
but,
       34
こうやりたいよね
            JTA         DataSource

 war                                 RDB

  ActionController
       same transaction

         ActiveRecord                        MoM
             JMS                     Mn    ・・・・   M2   M1



       web container
                                                            other systems
  Java App Server

want to do like this way
                                                                            35
send queue with user transaction.
     # POST /users
     # POST /users.xml
     def create
      ut = InitialContext.new.lookup "UserTransaction“
      @user = User.new(params[:user])
      respond_to do |format|
       begin
          ut.begin                             # transaction begin
          @user.save!                          # persist to RDB
          send_queue(@user)           # send queue

               ・・・

         ut.commit;                            # transaction commit
        end
       rescue
        ut.rollback                            # transaction rollback

         ・・・

                                                                        36
やってみよう!

 Try it!
           37
失敗
                 failure... orz
http://www.flickr.com/photos/proimos/4199675334/        38
理由(調査中)
               『リソースマネージャが
          グローバルトランザクションの外で作業中』
                   コネクション管理が別なのが原因か

                      Why?(consumption)
         “The resource manager is doing work
             outside a global transaction”
          separate connection management
http://http://www.flickr.com/photos/anitakhart/4626206362/   39
まとめ

conclusion
             40
繋がるために3つの
                                                       Java EE 技術の
                                                         力を使おう!



                                                     Let’s use Java EE
                                                connecting each other!
http://www.flickr.com/photos/marvinkuo/2153147300/                   41
大事なのは
お互いが
繋がること
                                        The most important thing
                                                               is
                                         “Connecting each other”
http://www.flickr.com/photos/yngrich/8828592/                   42
10 things :
   How to enhance
   the power of JRoR
   using Java EE technology

     橋本 吉治
      HASHIMOTO                            Yoshiharu
                                                       Japan Java User Group
                                                                   @hasssie


     JRubyKaigi2010 28th Aug
http://www.flickr.com/photos/shinyai/335249511/                                43

Public 20100828 j_ruby_kaigi_10things_jror_with_javaee