ivansmuletutorial-111120030338-phpapp01

354
8/13/2019 ivansmuletutorial-111120030338-phpapp01 http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 1/354 Ivan's Mule Examples and Recipes by Ivan A Krizsan Version: April 23, 2!2 "opyri#$% 2!!&2!2 Ivan A Krizsan All Ri#$%s Reserved !

Upload: flavio-barbosa

Post on 04-Jun-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 1/354

Ivan's Mule Examples and Recipes

by

Ivan A Krizsan

Version: April 23, 2 !2

"opyri#$% 2 !!&2 !2 Ivan A Krizsan All Ri#$%s Reserved

!

Page 2: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 2/354

Table of Contents(able o) "on%en%s 2*urpose +

%ruc%ure-icensin# .isclaimers ($an/s +*rere0uisi%es *ar% 1ne ($e Examples

! %andalone Mule Exposin# a eb ervice ! ! "rea%e %$e *ro4ec% !2 ! 2 "rea%e %$e eb ervice Implemen%a%ion "lass ! 3 "rea%e %$e %ar%er "lass !3 ! 5 "rea%e %$e Mule "on)i#ura%ion 6iles !5

"rea%e %$e Mule 2 x "on)i#ura%ion 6ile !5

"rea%e %$e Mule 3 x "on)i#ura%ion 6ile !7 ! 8 Run %$e Example %ar%er *ro#ram !+ Run %$e Mule 3 x Version Run %$e Mule 2 x Version

! 7 Run %$e Mule "on)i#ura%ion 6iles 23 2 Mule in eb Applica%ions 25

2 ! *repare (omca% )or Mule .evelopmen% Add %$e 9ecessary -ibraries %o %$e (omca% erver "on)i#ure a (omca% erver in Eclipse "on)i#urin# a %andalone (omca% erver

2 2 (omca% Mule Example eb Applica%ion 32

"rea%e %$e *ro4ec% "rea%e %$e ervice Implemen%a%ion "lass "rea%e %$e Mule "on)i#ura%ion 6iles "rea%e %$e .eploymen% .escrip%or .eploy %$e eb Applica%ion (es% %$e ervice

3 Modular Mule "on)i#ura%ion 5 3 ! "rea%e %$e *ro4ec% 5 3 2 "rea%e %$e ervice Implemen%a%ion "lass 5! 3 3 "rea%e %$e "us%om Excep%ion <andler=-is%ener "lasses 52

"rea%e %$e Mule 2 x "us%om Excep%ion -is%ener 52 "rea%e %$e Mule 3 x "us%om Excep%ion <andler

3 5 "rea%e %$e Mule "on)i#ura%ion 6iles 57 "rea%e %$e Mule 2 x "on)i#ura%ion 6iles "rea%e %$e Mule 3 x "on)i#ura%ion 6iles

3 8 Run %$e Example *ro#ram 83 5 Inser% 6ile .a%a in%o a .a%abase 87

5 ! "rea%e %$e *ro4ec% 8 5 2 Add .ependencies 5 3 .a%abase (able "rea%ion crip% 5 5 "rea%e %$e prin# >ean "on)i#ura%ion 6ile 72 5 8 "rea%e %$e Mule "on)i#ura%ion 6iles 73

"rea%e %$e Mule 6ile "onnec%or "on)i#ura%ion 6iles 73

2

Page 3: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 3/354

"rea%e %$e Mule ?.>" "onnec%or "on)i#ura%ion 6iles 78 "rea%e %$e Main Mule "on)i#ura%ion 6iles 7

5 7 Run %$e Example *ro#ram ! 5 Mule 3 x Version @i%$ 6lo@

8 Valida%e M- .a%a 8 8 ! "rea%e %$e *ro4ec% 7

8 2 "rea%e %$e M- c$emas 7 8 3 "rea%e M- .a%a 6iles ; 8 5 "rea%e %$e Mule "on)i#ura%ion 6iles +

"rea%e %$e Mule 6ile "onnec%or "on)i#ura%ion 6iles + "rea%e %$e Main Mule "on)i#ura%ion 6iles ;

($e Mule 2 x "on)i#ura%ion 6ile ($e Mule 3 x "on)i#ura%ion 6ile

8 8 Run %$e Example *ro#ram ;; 8 7 Valida%ion and M- c$ema Impor%s +

Modi)y %$e M- c$ema Implemen% a Resource Resolver +! Modi)y %$e c$ema Valida%ors +3 Run %$e Example *ro#ram

7 Ex%rac% M- Messa#e *ayload @i%$ *a%$ 7 ! "rea%e %$e *ro4ec% ++ 7 2 "rea%e M- .a%a 6iles ++ 7 3 "rea%e %$e Mule "on)i#ura%ion 6iles !

"rea%e %$e Mule 6ile "onnec%or "on)i#ura%ion 6iles ! "rea%e %$e Main Mule "on)i#ura%ion 6iles ! !

($e Mule 2 x "on)i#ura%ion 6ile ($e Mule 3 x "on)i#ura%ion 6ile

($e *a%$ Expression ! 7 7 5 Run %$e Example *ro#ram ! 7 8 Exercises Moni%orin# Mule ! +

! "rea%e %$e *ro4ec% ! + 2 "rea%e %$e eb ervice Implemen%a%ion "lass 3 "rea%e %$e Mule "on)i#ura%ion 6iles 5 Run %$e Example *ro#ram Run %$e Mule 2 x Example *ro#ram Run %$e Mule 3 x Example *ro#ram (es% %$e Runnin# Example *ro#ram

8 Mana#in# a Mule Ins%ance Bsin# ?M Run ?"onsole Cenera%e ome %a%is%ics

%ar%in# and %oppin# a Mule Ins%ance Bsin# ?M 7 M 5? and Mule

Moni%orin# Mule in eb Applica%ions ; Mule 9o%i)ica%ions !2

; ! "rea%e %$e *ro4ec% !2 ; 2 "rea%e %$e ervice Implemen%a%ion "lass !2 ; 3 "rea%e %$e 9o%i)ica%ion -is%eners !2!

"rea%e %$e "ommon 9o%i)ica%ion -is%ener >ase "lass !2!

"rea%e %$e Mule 2 x 9o%i)ica%ion -is%ener !25

3

Page 4: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 4/354

"rea%e %$e Mule 3 x 9o%i)ica%ion -is%ener !28 ; 5 "rea%e %$e Mule "on)i#ura%ion 6iles !2

"rea%e %$e Mule 2 x "on)i#ura%ion 6ile !2 "rea%e %$e Mule 3 x "on)i#ura%ion 6ile !3

; 8 Run %$e Example *ro#ram !33 Run %$e Mule 2 x Example *ro#ram

"rea%e a soapBI "lien% !35 Examine %$e Mule 2 x Example *ro#ram Resul% Run %$e Mule 3 x Example *ro#ram

Examine %$e Mule 3 x Example *ro#ram Resul% ; 7 Addi%ional Exercises !3+

+ Excep%ion <andlin# in Mule !5 + ! Mule 2 x "on)i#ura%ion %ruc%ure !5! + 2 Mule 3 x "on)i#ura%ion %ruc%ure !52 + 3 "rea%e %$e *ro4ec% !52 + 5 "rea%e %$e ervice Implemen%a%ion "lasses !53

"rea%e %$e Excep%ion ervice Implemen%a%ion "lass !53 "rea%e %$e <ello ervice Implemen%a%ion "lass !55 "rea%e %$e -o##in# ervice Implemen%a%ion "lass !58

+ 8 ($e "allable In%er)ace !5 + 7 "rea%e %$e %ar%er "lasses !5;

"rea%e %$e Mule 2 x %ar%er "lass !5; "rea%e %$e Mule 3 x %ar%er "lass !8

+ "rea%e %$e Excep%ion -is%eners "rea%e %$e Mule 2 x Excep%ion -is%ener "rea%e %$e Mule 3 x Excep%ion -is%ener

+ ; "rea%e %$e Mule "on)i#ura%ion 6iles !8 "rea%e %$e Mule 2 x "on)i#ura%ion 6iles "rea%e %$e Mule 3 x "on)i#ura%ion 6ile !72

+ + Run %$e Example *ro#ram !77 Run %$e Mule 2 x Version o) %$e Example *ro#ram !77

end a Messa#e %o %$e 6irs% Endpoin% Examine %$e 1u%pu% !77

endin# Messa#e %o %$e econd Endpoin% !7+ Examine %$e 1u%pu% !7+

Run %$e Mule 3 x Version o) %$e Example *ro#ram ! 2 end a Messa#e %o %$e 6irs% Endpoin%

Examine %$e 1u%pu% ! 3

endin# Messa#e %o %$e econd Endpoin% ! 5 Examine %$e 1u%pu% ! 5 + ! Exercises

! Mule *ro#ramma%ic Bse and Messa#e *roper%ies ! ! In%roduc%ion %o Messa#e *roper%ies ! 2 "rea%e %$e *ro4ec% ! ; ! 3 "rea%e %$e Mule "on)i#ura%ion 6iles ! ;

Mule 2 x "on)i#ura%ion 6iles ! ; Mule 3 x "on)i#ura%ion 6iles !;

! 5 "rea%e %$e %ar%er "lasses !;2 "rea%e %$e Mule 2 x %ar%er "lass !;2

"rea%e %$e Mule 3 x %ar%er "lass !;7

5

Page 5: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 5/354

! 8 Run %$e Example *ro#ram !+! Run %$e Mule 2 x Version o) %$e Example *ro#ram !+! Runnin# %$e Mule 3 x Version o) %$e Example *ro#ram !+3

! 7 Exercises !! (es%in# Mule "on)i#ura%ions !+8

!! ! "rea%e %$e *ro4ec%

!! 2 "rea%e %$e (es%s "rea%e %$e "ommon (es% "lass "rea%e %$e Mule 2 x (es% !+ "rea%e %$e Mule 3 x (es% !+;

!! 3 "rea%e %$e Mule "on)i#ura%ion 6iles "rea%e %$e Mule 2 x "on)i#ura%ion 6ile !++ "rea%e %$e Mule 3 x "on)i#ura%ion 6ile 2

!! 5 "rea%e %$e ervice Implemen%a%ion "lasses "rea%e %$e Mule 2 x ervice Implemen%a%ion "lass "rea%e %$e Mule 3 x ervice Implemen%a%ion "lass

!! 8 Run %$e Example *ro#ram Run %$e Mule 2 x Example *ro#ram Run %$e Mule 3 x Example *ro#ram

!! 7 Addi%ional Exercises !2 "rea%e Mule *ro4ec%s @i%$ Maven

!2 ! *rere0uisi%es !2 2 "rea%e %$e *ro4ec% 2 !2 3 Impor% *ro4ec% In%o Eclipse !2 5 "on)i#ure *ro4ec% in Eclipse !2 8 Bse Maven in Eclipse 2!2

Maven Coals 2!2 "rea%e an Eclipse Maven Run "on)i#ura%ion 2!3

!3 Mule "on)i#ura%ion *a%%erns 2!; !3 ! "rea%e %$e *ro4ec% 2!; !3 2 ($e >rid#e *a%%ern

ync$ronous >rid#e "rea%e Inbound and 1u%bound ervices >rid#e %$e ervices Run %$e ync$ronous >rid#e

Async$ronous >rid#e 228 "rea%e %$e 1u%bound ervice 228 >rid#e %$e ervices

Run %$e Async$ronous >rid#e !3 3 ($e imple ervice *a%%ern ?A &R imple ervice

"rea%e %$e "omponen% Implemen%a%ion "lass 22+ Modi)y %$e Mule "on)i#ura%ion 6ile Run %$e ?A &R imple ervice 23!

imple ervices and In$eri%ance Modi)y %$e Mule "on)i#ura%ion 6ile Run %$e imple ervice @i%$ In$eri%ance 233

?A & imple ervice 238 Modi)y %$e Mule "on)i#ura%ion 6ile

Run %$e ?A & imple ervice 237

8

Page 6: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 6/354

?A > imple ervice 23 "rea%e ?A > "lasses "rea%e Example .a%a "rea%e %$e "omponen% Implemen%a%ion "lass 23; Modi)y %$e Mule "on)i#ura%ion 6ile Run %$e ?A > imple ervice 23+

*a%$ imple ervice 25! "rea%e %$e "omponen% Implemen%a%ion "lass 25! Modi)y %$e Mule "on)i#ura%ion 6ile Run %$e *a%$ imple ervice 253

!3 5 ($e Valida%or *a%%ern 6irs% Valida%or Example

Modi)y %$e Mule "on)i#ura%ion 6ile Run %$e 6irs% Valida%or Example

econd Valida%or Example Modi)y %$e Mule "on)i#ura%ion 6ile Run %$e econd Valida%or Example

($ird Valida%or Example 28! Modi)y %$e Mule "on)i#ura%ion 6ile Run %$e ($ird Valida%or Example 283

!3 8 ($e eb ervice *roxy *a%%ern 285 Example *repara%ions

"rea%e %$e Mule "on)i#ura%ion 6ile Implemen% a "us%om -o##in# (rans)ormer 288 "rea%e %$e ervice .- 287 "rea%e %$e Moc/ ervice in soapBI 28;

6irs% eb ervice *roxy Example 27 Modi)y %$e Mule "on)i#ura%ion 6ile Run %$e eb ervice *roxy Example 272

econd eb ervice *roxy Example 278 "rea%e %$e .ouble um - (rans)orm 277 Modi)y %$e Mule "on)i#ura%ion 6ile Modi)y %$e .- 6ile 27+ Run %$e eb ervice *roxy Example 27+

*ar% (@o Recipes and Re)erence ! Messa#e Rou%in#

! ! elec%in# 1u%bound Endpoin% .ependin# on %$e Messa#e 2 2 ! 2 Rou%in# a Messa#e .ependin# on a in#le 6il%er 2 3

! 3 Excep%ion&.ependen% Messa#e Rou%in# ($e Excep%ion&>ased Rou%er 2 5 ($e 6irs%& uccess)ul Messa#e *rocessor

2 6il%erin# 2 ! Valida%in# M- Messa#e *ayload 2 2 "ombinin# 6il%ers

($e A9.&)il%er 2 ; ($e 1R&)il%er ($e 91(&)il%er

2 3 Implemen%in# "us%om 6il%ers 2 + 3 (rans)ormin#

3 ! Ex%rac% *ar% o) an M- Messa#e @i%$ *a%$ 2;

7

Page 7: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 7/354

3 2 (rans)orm M- .a%a Bsin# - 2;! 3 3 *ac/ or Bnpac/ Messa#e *ayload .a%a 2;2 3 5 "us%om (rans)ormers 2;3

Mule 2 x "us%om (rans)ormers 2;3 Mule 3 x "us%om (rans)ormers 2;5

5 Messa#e *roper%ies

5 ! Re%rievin# Messa#e *roper%ies 5 2 e%%in# Messa#e *roper%ies 2;8 5 3 Removin# Messa#e *roper%ies 2;8 5 5 Renamin# Messa#e *roper%ies 2;7 5 8 Readin# and ri%in# Messa#e *roper%ies %o .i))eren% copes

8 Expressions 2; 8 ! Evalua%ors

A%%ac$men% Evalua%or A%%ac$men%s Evalua%or 2; A%%ac$men%s&-is% Evalua%or >ean Evalua%or 2;; Endpoin% Evalua%or Excep%ion&(ype Evalua%or 2;+ 6unc%ion Evalua%or 2+ Croovy Evalua%or 2+ <eader Evalua%or 2+! <eaders Evalua%or <eaders&-is% Evalua%or ? 19 Evalua%or ? 19&9ode Evalua%or 2+2 ? *a%$ Evalua%or 2+2 Map&*ayload Evalua%or 2+3 Messa#e Evalua%or 1C9- Evalua%or *ayload Evalua%or 2+5 *ayload&(ype Evalua%or *rocessor Evalua%or 2+5 Re#ex Evalua%or 2+8

%rin# Evalua%or 2+8 Variable Evalua%or

ildcard Evalua%or 2+7 *a%$ Evalua%or

*a%$&9ode Evalua%or 7 9o%i)ica%ions 7 ! 9o%i)ica%ion Even% (ypes 2+; 7 2 9o%i)ica%ion -is%ener In%er)aces 7 3 9o%i)ica%ion Even%s 3 ! 7 5 -is%enin# %o 9o%i)ica%ions 7 8 -is%enin# %o 9o%i)ica%ions )rom a peci)ic "omponen% 7 7 .isablin# 9o%i)ica%ions 7 Re#is%erin# a 9o%i)ica%ion -is%ener *ro#ramma%ically 3 8 Mule ?M Mana#emen% 3 7

Mule 2 x ?M Mana#emen% 3 7

Mule 2 x erver Clobal "on)i#ura%ion 3 7

Page 8: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 8/354

Mule 2 x "onnec%ors "on)i#ura%ion Mule 2 x Endpoin% "on)i#ura%ion 3 + Mule 2 x Model "on)i#ura%ion Mule 2 x "on%ex% "on)i#ura%ion 3!2 Mule 2 x 9o%i)ica%ion "on)i#ura%ion 3!5 Mule 2 x ervice "on)i#ura%ion 3!7

Mule 2 x %a%is%ics "on)i#ura%ion Mule 3 x ?M Mana#emen% 32 Mule 3 x Applica%ion %a%is%ics 32! Mule 3 x erver Clobal "on)i#ura%ion 322 Mule 3 x "onnec%ors "on)i#ura%ion Mule 3 x Endpoin% "on)i#ura%ion 325 Mule 3 x 6lo@ "on)i#ura%ion 328 Mule 3 x Model "on)i#ura%ion Mule 3 x "on%ex% "on)i#ura%ion 32 Mule 3 x %a%is%ics "on)i#ura%ion Mule 3 x 9o%i)ica%ion "on)i#ura%ion 32;

; *ac/a#e a Mule Applica%ion ; ! *ac/a#e Mule 2 x Applica%ions 32+ ; 2 *ac/a#e Mule 3 x Applica%ions 32+

+ (es%in# + ! Excep%ion "omponen% 33 + 2 Re%urn Moc/ .a%a )rom a "omponen% 332 + 3 -o##in# Messa#e .e%ails 332 + 5 Re%ain a Messa#e <is%ory 333 + 8 In%roduce a .elay 335 + 7 Append (ex% %o Received Messa#es

Appendix A *repare )or Mule .evelopmen% ! .o@nload and Ins%all Mule 2 Ins%all %$e Eclipse Mule *lu#in 3 "on)i#ure %$e Mule *lu#in in Eclipse

Appendix > "rea%e a Mule *ro4ec% ! "rea%e %$e *ro4ec% 2 @i%c$ o)) %$e Mule 3 <o% .eploymen% >uilder 33+ 3 "rea%e Mule "on)i#ura%ion 6iles 35 5 "rea%e %$e -o#5? "on)i#ura%ion 6ile 8 "$an#e %$e Mule .is%ribu%ion o) a *ro4ec%

Appendix " Enablin# Maven .ependency Mana#emen% )or an Eclipse *ro4ec%

Appendix . Mule %andalone erver 35; ! Mule %andalone erver on 1 35; 2 Mule %andalone erver >asic Mana#emen% 35+

2 ! %ar% and %op a Mule erver 2 2 .eploy and Bndeploy a Mule Applica%ion 38

Mule 2 x .eploymen% Mule 3 x .eploymen%

2 3 Bndeploy a Mule Applica%ion Appendix E .a%abase Access )rom @i%$in Eclipse 38!

! .a%a ource "rea%ion 38! 2 .a%a Access 388

;

Page 9: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 9/354

Purpose($is boo/ aims %o )ill %@o purposes: )irs% %o #ive an in%roduc%ion %o usin# %$e communi%y version o)Mule versions 2 x and 3 x, @$ic$, as o) @ri%in#, are versions 2 2 ! and 3 2

econd, %$is boo/ also aim %o serve as a re)erence con%ainin# small examples %o serve as solu%ions %ospeci)ic problems.i))erences be%@een %$e %@o versions o) Mule @ill also be poin%ed ou% and discussed

Structure($e )irs% par% o) %$is boo/ con%ains comple%e examples o) $o@ %o use Mule @i%$ brie) explana%ions($e second par% o) %$e boo/ con%ains smaller, incomple%e, examples o) $o@ %o accomplis$ par%icular%as/s I $ave called %$ese examples recipes *ar% %@o also con%ains some re)erence in)orma%ion oncer%ain )ea%ures I $ave )ound use)ul6inally %$ere are %$e appendices %$a% con%ains in)orma%ion %$a% didn'% )i% in ei%$er o) %$e )irs% %@o

par%s, suc$ as $o@ %o se% up %$e I.E )or Mule developmen%, $o@ %o crea%e I.E pro4ec%s )or Muledevelopmen% e%c

Licensing($is boo/ is licensed under a "rea%ive "ommons A%%ribu%ion&9oncommercial&9o .eriva%ive or/s3 license In s$or% %$is means %$a%:

• Dou may s$are %$is boo/ @i%$ o%$ers• Dou may no% use %$is boo/ )or commercial purposes• Dou may no% crea%e deriva%e @or/s )rom %$is boo/

Disclaimers($ou#$ I $ave done my bes% %o avoid i%, %$is boo/ mi#$% con%ain errors I canno% be $eldresponsible )or any e))ec%s caused, direc%ly or indirec%ly, by %$e in)orma%ion in %$is boo/ youare usin# i% on your o@n ris/ I canno% ma/e any #uaran%ees concernin# %$e comple%eness o) %$ein)orma%ion con%ained in %$is boo/

ubmi%%in# any su##es%ions rela%ed %o %$is boo/ %$e in)orma%ion submi%%ed becomes my proper%yand you #ive me %$e ri#$% %o use %$e in)orma%ion in @$a%ever @ay I )ind sui%able, @i%$ou%compensa%in# you in any @ay i%$ %$a% said, I @ill $appily credi% con%ribu%orsAll %rademar/s are proper%ies o) %$eir respec%ive o@ner and do no% imply endorsemen% o) any /ind($is boo/ $as been @ri%%en in my spare %ime and $as no connec%ion @$a%soever @i%$ myemployer

ThanksMany %$an/s %o %$e people on %$e Mule Bsers )orum )or pos%in# and ans@erin# 0ues%ionsI am also very #ra%e)ul %o all %$e people be$ind -ibre1))ice, @$ic$ I exclusively use @$en @ri%in#my boo/s ($an/s also %o %$e people pu%%in# e))or% in developin# Cimp my %ool o) c$oice )oredi%in# #rap$ics in %$is boo/

+

Page 10: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 10/354

Prerequisites($is boo/ assumes %$e )ollo@in# prere0uisi%es:

• ($e ?ava ! 7 ?.K=?RE or la%er• Experience @i%$ %$e Eclipse I.E or %$e prin# ource (ool ui%e

All %$e examples in %$is boo/ $ave been developed in %$e prin# ource (ool ui%e , %$ou#$ arecen% version o) Eclipse s$ould su))ice

• ome experience @i%$ %$e prin# dependency in4ec%ion )rame@or/ is assumed• ome experience @i%$ @eb services in #eneral and ?A & in par%icular is bene)icial• >asic experience o) %es%in# 1A* @eb services usin# soapBI

($is boo/ also assumes %$a% you $ave prepared )or Mule developmen% as described in %$e appendix*reparin# )or Mule .evelopmen%

!

Page 11: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 11/354

Page 12: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 12/354

1. Standalone Mule Exposing a Web ServiceIn %$is )irs% example, @e'll crea%e a very simple Mule con)i#ura%ion %$a% exposes a 1A* @ebservice usin# %$e Apac$e " 6 @eb service s%ac/ Mule @ill be run embedded in a s%andalone ?ava

pro#rame @ill %a/e a )irs% loo/ a% Mule con)i#ura%ion )iles )or bo%$ %$e 2 x and 3 x versions o) Mule

1 1 Create the Pro!ect"rea%e %$e pro4ec% as described in %$e appendix"rea%e a Mule *ro4ec%, namin# i%F 1A* eb erviceInMuleG ($e Mule 3 $o% deploymen% can be s@i%c$ed o)) )rom %$e s%ar%, as %$is)ea%ure is no% o) use @$en runnin# Mule embedded

1 " Create the #eb Ser$ice %mplementation Class($e @eb service endpoin% implemen%a%ion class implemen%s a service %$a% ex%ends #ree%in#s

In %$e source roo%, crea%e %$e pac/a#ecom.ivan.mule• In %$e ne@ pac/a#e, crea%e %$e class HelloService @i%$ %$e )ollo@in# con%en%s:

package com.ivan.mule;

import java.util.Date;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;

/** * SOAP web service endpoint implementation class that implements * a service that extends greetings. */@WebService

public class HelloService{ /** * Default constructor. Logs creation of service instances. */ public HelloService() { System.out.println("***** HelloService instance created."); }

/** * Greets the person with the supplied name. *

* @param inName Name of person to greet. * @return Greeting. */ @WebResult(name="greeting") public String greet(@WebParam(name="name") String inName) { return "Hello " + inName + ", the time is now " + new Date(); }}

9o%e %$a%:• ($e HelloService class con%ains a de)aul% cons%ruc%or

($is is used %o s$o@ us $o@ many ins%ances o) %$e class is crea%ed and no% necessary• ($e HelloService class con%ains a number o) ?A & anno%a%ions

($ese are used exac%ly as @$en developin# ?A & @eb services deployed ou%side o)Mule

!2

Page 13: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 13/354

1 & Create the Starter Class($e s%ar%er class s$o@s $o@ %o pro#ramma%ically s%ar% an embedded ins%ance o) MuleIn our case i% is no% s%ric%ly necessary, as i% is possible %o launc$ %$e Mule con)i#ura%ion )ilesdirec%ly, as @e @ill see in subse0uen% c$ap%ers

In %$e pac/a#e com.ivan.mule , crea%e a class named SOAPWebServiceInMuleStarter @i%$ %$e)ollo@in# con%en%s:package com.ivan.mule;

import org.mule.api.MuleContext;import org.mule.config.spring.SpringXmlConfigurationBuilder;import org.mule.context.DefaultMuleContextFactory;

/** * Reads Mule configuration file and starts an instance of Mule that is * configured accordingly. *

* @author Ivan A Krizsan */public class SOAPWebServiceInMuleStarter

{ public final static String[] MULE2_CONFIG_FILES = { "mule-config2.xml" }; public final static String[] MULE3_CONFIG_FILES = { "mule-config3.xml" };

public static void main(String[] args) throws Exception { /*

* Change here to use different configuration file for the * Mule context. * Remember to change the Mule libraries accordingly! */ String[] theConfigFiles = MULE3_CONFIG_FILES;

DefaultMuleContextFactory theMuleContextFactory = new DefaultMuleContextFactory(); SpringXmlConfigurationBuilder theSpringConfigBuilder = new SpringXmlConfigurationBuilder(theConfigFiles); MuleContext theMuleContext = theMuleContextFactory.createMuleContext(theSpringConfigBuilder); theMuleContext.start(); }}

9o%e %$a%:• ($ere are %@o cons%an%s speci)yin# Mule con)i#ura%ion )iles

As indica%ed by %$e names, one is )or %$e 2 x version o) Mule and %$e o%$er )or %$e 3 xversion

• ($e crea%ion o) a Mule con%ex% )ollo@s %$ese s%eps:& "rea%e a con)i#ura%ion builder($e con)i#ura%ion builder is responsible )rom readin# %$e Mule con)i#ura%ion )ileHs needed@$en crea%in# %$e Mule con%ex%& "rea%e a Mule con%ex% )ac%ory& "rea%e a Mule con%ex%($e con%ex% )ac%ory can, usin# %$e con)i#ura%ion builder, crea%e %$e con%ex%

!3

Page 14: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 14/354

1 ' Create the (ule Configuration )ilesMule con)i#ura%ion )iles are )iles in @$ic$ Mule is %old $o@ %o receive messa#es, $o@ %o rou%e %$em%o di))eren% componen%s and $o@ %o %rans)orm messa#es e%c($e main di))erence be%@een %$e %@o versions o) Mule @e use in %$is example is %$e Mulecon)i#ura%ion )ilesJ one )or eac$ version o) Mule

Create the Mule 2.x Configuration File

($e Mule 2 x con)i#ura%ion )ile is %o be loca%ed in %$e roo% o) %$e source direc%ory and is %o benamed Fmule&con)i#2 xmlG I% $as %$e )ollo@in# con%en%s:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://www.mulesource.org/schema/mule/cxf/2.2" xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.2" xsi:schemaLocation=" http://www.mulesource.org/schema/mule/cxf/2.2 http://www.mulesource.org/schema/mule/cxf/2.2/mule-cxf.xsd

http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd

http://www.mulesource.org/schema/mule/stdio/2.2 http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd">

<!-- A model contains a set of services. --> <model name="GreetingModel"> <!-- A service specifies how messages are received, to which component the messages are delivered and how the result is handled. --> <service name="GreetingService"> <!-- The <inbound> element specifies how the service receives messages. In this case, we use Apache CXF on the address specified. --> <inbound> <cxf:inbound-endpoint address="http://localhost:8182/services/GreetingService"/> </inbound>

<!-- Specifies the component that will be invoked when there is an incoming message. The component can be one of the following types: singleton-object: one single instance handles all messages. prototype-object: each instance handles a single message.

spring-object: messages handled by specified Spring bean. --> <component> <prototype-object class="com.ivan.mule.HelloService"/> </component>

<!-- Specifies where to direct responses. Responses are results of incoming messages having been processed by the above component. --> <outbound> <pass-through-router> <!-- Logs messages to the console. --> <stdio:outbound-endpoint system="OUT"/> </pass-through-router>

!5

Page 15: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 15/354

</outbound> </service> </model></mule>

9o%e %$a%:•

($e muleL elemen% con%ains several M- namespace pre)ix declara%ions($ese are ex%ensions %o %$e s%andard Mule M- sc$ema %$a% allo@s us %o declare, in %$iscase, @eb service endpoin%s implemen%ed usin# Apac$e " 6 and endpoin%s usin# s%andardI1 Hconsole

• ($e muleL elemen% con%ains a schemaLocation a%%ribu%e in @$ic$ %$e lin/s be%@een namespaces and ac%ual M- sc$emas are speci)ied($e namespaces and=or %$e M- sc$ema loca%ions depend on %$e version o) Mule used I)you mi#ra%e a Mule con)i#ura%ion )ile )rom one version o) Mule %o ano%$er, you @ill need %omodi)y %$is sec%ion Hand per$aps more

• ($e muleL elemen% bein# %$e roo% o) %$e Mule con)i#uraion con%ains a modelL elemen%

($e modelL elemen% con%ains a se% o) services In %$is example, i% only con%ains a sin#leservice

• ($e modelL elemen% con%ains a serviceL elemen%($e serviceL elemen% speci)ies $o@ messa#es are received, a componen% %$a% is %o processmessa#es and $o@ any resul%s are %o be $andled($e service in %$is example is %o receive messa#es usin# an Apac$e " 6 @eb service, send%$e messa#es %o an ins%ance o) %$e HelloService class )or processin# and )inally send %$eresul% %o %$e console ($e resul% )rom %$e HelloService ins%ance @ill also become %$e resul%o) %$e @eb service re0ues%, despi%e no explici% con)i#ura%ion s%a%in# %$is

• ($e inboundL elemen% in %$e serviceL elemen% speci)ies $o@ %$e service receivesmessa#es

• ($e cx):inbound&endpoin%L elemen% in %$e inboundL elemen% causes a ?A & H 1A*@eb service %o be exposed by %$e Apac$e " 6 @eb service s%ac/($e address a% @$ic$ %$e service is exposed is speci)ied usin# %$e address a%%ribu%e($e de)aul% value o) %$e frontend a%%ribu%e is F4ax@sG, so @e need no% speci)y %$a% %$is is %$eone @e @an% %o use

• ($e componen%L elemen% in %$e serviceL elemen% speci)ies %$e componen% %$a% @ill process received messa#es and i%s ins%an%ia%ion&policy($e componen% can be one o) %$ree %ypes:& sin#le%on&ob4ec%L & one sin#le ob4ec% processes all messa#es& pro%o%ype&ob4ec%L & eac$ messa#e is processed by a dedica%ed ins%ance& sprin#&ob4ec%L & messa#es are processed by speci)ied prin# bean

• ($e ou%boundL elemen% in %$e serviceL elemen% speci)ies @$ere processed messa#es aresen%In %$is example processed messa#es are prin%ed on %$e console

• ($e ou%boundL elemen% con%ains a pass&%$rou#$&rou%erL elemen%As @e sa@ earlier, %$e inboundL elemen% did no% con%ain a rou%er elemen% I% may, bu% is no%re0uired %o ($e ou%boundL elemen% mus%, $o@ever, con%ain a% leas% one rou%er($e pass&%$rou#$&rou%erL rou%es all messa#es i% receives %o %$e ou%bound endpoin%

con)i#ured inside %$e elemen%

!8

Page 16: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 16/354

• ($e s%dio:ou%bound&endpoin%L elemen% direc%s processed messa#es %o %$e s%andard I=1console

A )i#ure describin# %$e above Mule con)i#ura%ion may loo/ li/e %$is:

Visual represen%a%ion o) %$e Mule 2 x con)i#ura%ion )ile in %$is example

Messa#es are received by %$e @eb service endpoin%, passed on %o %$e componen% )or processin# ($eresul% is %$en passed on %o %$e console, passin# %$rou#$ an ou%bound rou%er

Create the Mule 3.x Configuration File

($e Mule 3 x con)i#ura%ion )ile is %o be loca%ed in %$e roo% o) %$e source direc%ory and is %o benamed Fmule&con)i#3 xmlG I% $as %$e )ollo@in# con%en%s:<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns:stdio="http://www.mulesoft.org/schema/mule/stdio" xmlns:spring="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/3.2/mule-cxf.xsd

http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd

http://www.mulesoft.org/schema/mule/stdio http://www.mulesoft.org/schema/mule/stdio/3.2/mule-stdio.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<flow name="GreetingFlow"> <!-- An inbound endpoint receives incoming messages on a specified address, using the the specified exchange pattern. --> <inbound-endpoint address="http://localhost:8182/services/GreetingService" exchange-pattern="request-response"/> <!-- Incoming requests are received using a JAX-WS web service using the Apache CXF web service stack. The CXF jaxws-service honors JAX-WS annotations in the service class.

Note that the serviceClass attribute must specify the

!7

Page 17: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 17/354

class implementing the service. --> <cxf:jaxws-service serviceClass="com.ivan.mule.HelloService"/>

<!-- Despite having specified the service class in the above element, we must supply a <component> specifying the object to be invoked when having received a message. -->

<component> <!-- The component implementation object can be specified either using a Spring bean, a singleton object or a prototype object. The component can be one of the following types: singleton-object: one single instance handles all requests. prototype-object: each instance handles a single request. spring-object: requests handled by specified Spring bean. --> <spring-object bean="helloService"/> </component>

<!-- The log component can be inserted in a flow to log the content of a message being passed through the log component. --> <log-component/> </flow>

<!-- In the <spring:beans> element we can declare Spring beans and other things that can be done in a regular Spring configuration file. --> <spring:beans> <!-- This Spring bean supplies the implementation of the Hello service. --> <spring:bean id="helloService" class="com.ivan.mule.HelloService"/> </spring:beans></mule>

9o%e %$a%:• ($e muleL elemen% con%ains several M- namespace pre)ix declara%ions

($ese are ex%ensions %o %$e s%andard Mule M- sc$ema %$a% allo@s us %o declare, in %$iscase, @eb service endpoin%s implemen%ed usin# Apac$e " 6, endpoin%s usin# s%andard I1Hconsole and prin# beans

• ($e muleL elemen% con%ains a schemaLocation a%%ribu%e in @$ic$ %$e lin/s be%@een namespaces and ac%ual M- sc$emas are speci)ied($e M- sc$ema loca%ions depend on %$e version o) Mule used I) you mi#ra%e a Mulecon)i#ura%ion )ile )rom one version o) Mule %o ano%$er, you @ill need %o modi)y %$is sec%ionHand per$aps more

• ($e muleL elemen% bein# %$e roo% o) %$e Mule con)i#ura%ion con%ains a )lo@L elemen%A )lo@L elemen% describes a messa#e processin# )lo@J an op%ional source )rom @$ic$ %$e)lo@ receives messa#es, a number o) messa#e processors and )inally an op%ional excep%ion$andlin# s%ra%e#y Hno% presen% in %$is example

• ($e inbound&endpoin%L elemen% speci)ies $o@ %$e )lo@ receives messa#es($e address a%%ribu%e o) %$e inbound&endpoin%L elemen% con%ains an BRI %$a% speci)ies %$e

pro%ocol and address o) %$e endpoin%

($e exchange-pattern a%%ribu%e speci)ies %$e exc$an#e pa%%ern used @$en in%erac%in# @i%$

!

Page 18: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 18/354

%$e endpoin%J ei%$er re0ues%&response or one&@ay• ($e cx):4ax@s&serviceL is used %o speci)y proper%ies o) a messa#e processor %$a% is a ?A &

@eb service usin# %$e Apac$e " 6 @eb service s%ac/In %$is example, @e $ave only used %$e service lass a%%ribu%e %o speci)y %$e anno%a%ed ?A &

endpoin% implemen%a%ion class, bu% i% is also possible %o, )or ins%ance, con)i#ure %$e useo) M(1M, add one or more in%ercep%ors e%c

• ($e componen%L elemen% speci)ies a messa#e processor.espi%e $avin# speci)ied an endpoin% implemen%a%ion class, @e mus% speci)y a componen%%$a% processes messa#es($e cx):4ax@s&serviceL elemen% speci)ies %$e class %$a% de)ines %$e ?A & endpoin% and)rom @$ic$ a .- is #enera%ed, bu% %$e componen%L elemen% speci)ies %$e class, @$ic$in %$is example is %$e same class as %$e service class o) %$e ?A & endpoin%, %$a% processesmessa#es

• ($e sprin#&ob4ec%L elemen% in %$e componen%L elemen% speci)ies %$e prin# bean %$a% is

%o process messa#es• A)%er %$e end o) %$e )lo@L elemen% %$ere is a sprin#:beansL elemen%

($is elemen% enables us %o embed prin# con)i#ura%ion in a Mule con)i#ura%ion )ile, as isdone @i%$ %$e $ello ervice bean

A )i#ure visualizin# %$e above Mule 3 x con)i#ura%ion )ile may loo/ li/e %$is:

Visual represen%a%ion o) %$e Mule 3 x con)i#ura%ion )ile in %$is example

!;

Page 19: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 19/354

1 * +un the Example Starter Programi%$ %$e %@o con)i#ura%ion )iles in place, @e are no@ ready %o run %$e example s%ar%er pro#ram In

i%s ori#inal con)i#ura%ion, @e @ill run %$e example @i%$ Mule 3 x

Run the Mule 3.x ersion($e pro4ec% in i%s ori#inal incarna%ion s$ould be con)i#ured )or usin# Mule 3 x (o ma/e sure youcan use %$e )ollo@in# c$ec/lis%:

• ($e Mule 3 x libraries s$ould be on %$e pro4ec%'s ?ava >uild *a%$• ($e s%ar%er classSOAPWebServiceInMuleStarter s$ould use %$e Mule 3 x con)i#ura%ion )ile,

as s$o@n in %$e )ollo@in# code snippe% H$i#$li#$%ed in red :

... public static void main(String[] args) throws Exception { /*

* Change here to use different configuration file for the

* Mule context. * Remember to change the Mule libraries accordingly! */ String[] theConfigFiles = MULE3_CONFIG_FILES;

DefaultMuleContextFactory theMuleContextFactory = new DefaultMuleContextFactory();...

(o s%ar% %$e pro#ram, simply run %$eSOAPWebServiceInMuleStarter class as a ?ava Applica%ion inEclipse -o# ou%pu% similar %o %$e )ollo@in# s$ould appear in %$e console Hpor%ions $ave beenexcluded %o conserve space :

...[10-10 06:45:33] INFO FlowConstructLifecycleManager [main]: Initialising flow:GreetingFlow[10-10 06:45:33] INFO DefaultMessagingExceptionStrategy [main]: Initialising exceptionlistener: org.mule.exception.DefaultMessagingExceptionStrategy@c4a3158[10-10 06:45:33] INFO SedaStageLifecycleManager [main]: Initialising service:GreetingFlow.stage1[10-10 06:45:33] INFO WebServiceFactoryBean [main]: Built CXF Inbound MessageProcessorfor service class com.ivan.mule.HelloService[10-10 06:45:34] INFO ComponentLifecycleManager [main]: Initialising component:commponent.1753593456[10-10 06:45:34] INFO ComponentLifecycleManager [main]: Initialising component:commponent.2142386190***** HelloService instance created.[10-10 06:45:34] INFO AutoConfigurationBuilder [main]: Configured Mule using"org.mule.config.spring.SpringXmlConfigurationBuilder" with configuration resource(s):"[ConfigResource{resourceName='mule-config3.xml'}]"

...[10-10 06:45:34] INFO HttpConnector [main]: Registering listener: GreetingFlow onendpointUri: http://localhost:8182/services/GreetingService...[10-10 06:45:35] INFO DefaultMuleContext [main]:*********************************************************************** Mule ESB and Integration Platform ** Version: 3.2.0 Build: 22917 ** MuleSoft, Inc....

!+

Page 20: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 20/354

($ere is a lo% %$a% can be seen )rom %$e above lo#, bu% i%ems o) main in%eres% )or us are:• An ins%ance o) %$e HelloService bean is crea%ed

($e correspondin# lo# ou%pu% $as been $i#$li#$%ed in #reenince no scope $as been supplied )or %$e prin# bean, i% de)aul%s %o sin#le%on scope %$us

one sin#le ins%ance o) %$e bean @ill process all messa#es received•

($e Mule ins%ance @as con)i#ured usin# %$e Spring!ml onfiguration"uilder and %$econ)i#ura%ion )ile Fmule&con)i#3 xmlG($e correspondin# lo# ou%pu% $as been $i#$li#$%ed in oran#e

• An endpoin% o) %$e Cree%in#6lo@ is re#is%ered on %$e BRI$%%p:==local$os%:;!;2=services=Cree%in# ervice($us, %$e .- o) %$e Cree%in# ervice endpoin% can be )ound a%$%%p:==local$os%:;!;2=services=Cree%in# ervice @sdl ($e correspondin# lo# ou%pu% $as been $i#$li#$%ed in blue

Impor%an% no%e$en accessin# %$e .- o) a @eb service exposed usin# Mule, %$e F@sdlG par% o) %$e BR-, a)%er %$e 0ues%ion mar/, mus% al@ays be @ri%%en usin#lo@ercase only

I) @e use soapBI %o send a re0ues% %o %$e endpoin% o) %$e Cree%in# ervice, a response similar %o %$e)ollo@in# s$ould be received:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns2:greetResponse xmlns:ns2="http://mule.ivan.com/"> <greeting>Hello Ivan, the time is now Thu Mar 03 07:08:34 CET 2011</greeting> </ns2:greetResponse> </soap:Body></soap:Envelope>

In %$e lo#, ou%pu% similar %o %$e )ollo@in# s$ould appear:

...[03-03 07:08:34] INFO LogComponent [connector.http.0.receiver.2]:********************************************************************************* Message received in service: GreetingFlow. Content is: 'Hello Ivan, the ** time is now Thu Mar 03 07:08:34 CET 2011' *

ubse0uen% re0ues%s causes ou%pu% similar %o %$e above %o be ou%pu% %o %$e console e no%e %$a% no

ne@ ins%ances o) %$e HelloService are crea%ed ($is is due %o %$e prin# bean implemen%in# %$eservice bein# scoped as sin#le%on

e can see %$a% Mule 3 x does @$a% i% is con)i#ured %o doIn %$e nex% sec%ion @e @ill recon)i#ure %$e pro4ec% %o use Mule 2 x and run %$e example pro#ram

2

Page 21: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 21/354

Page 22: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 22/354

($e in)orma%ion o) main in%eres% )or us in %$e above lo# is:• ($e Mule ins%ance @as con)i#ured usin# %$e Spring!ml onfiguration"uilder and %$e

con)i#ura%ion )ile Fmule&con)i#2 xmlG($e correspondin# lo# ou%pu% $as been $i#$li#$%ed in oran#e

• An endpoin% o) %$e Cree%in#6lo@ is re#is%ered on %$e BRI

$%%p:==local$os%:;!;2=services=Cree%in# ervice($us, %$e .- o) %$e Cree%in# ervice endpoin% can be )ound a%$%%p:==local$os%:;!;2=services=Cree%in# ervice @sdl ($e correspondin# lo# ou%pu% $as been $i#$li#$%ed in blueI) @e compare %$e .- o) %$e v3 x and v2 x versions o) %$e exposed service, @e can see%$a% %$ey are )unc%ionally iden%ical

• An ins%ance o) %$e HelloService bean is crea%ed($e correspondin# lo# ou%pu% $as been $i#$li#$%ed in #reen

I) @e use soapBI %o send a re0ues% %o %$e endpoin% o) %$e Cree%in# ervice, a response similar %o %$e)ollo@in# s$ould be received:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns2:greetResponse xmlns:ns2="http://mule.ivan.com/"> <greeting>Hello Ivan, the time is now Thu Mar 03 17:35:38 CET 2011</greeting> </ns2:greetResponse> </soap:Body></soap:Envelope>

In %$e lo#, ou%pu% similar %o %$e )ollo@in# s$ould appear:

...***** HelloService instance created.[03-03 17:35:38] INFO StdioMessageDispatcher [connector.stdio.0.dispatcher.1]:Connected: endpoint.outbound.stdio://system.outHello Ivan, the time is now Thu Mar 03 17:35:38 CET 2011

ubse0uen% re0ues%s @ill cause ne@ ins%ances o) HelloService %o be crea%ed and ou%pu% similar %o %$eabove %o be ou%pu% %o %$e console ince @e use a pro%o%ype&ob4ec% )or %$e componen% processin#messa#es, a ne@ ins%ance @ill be crea%ed )or eac$ messa#e received

e can see %$a% Mule 2 x also does @$a% i% is con)i#ured %o do

22

Page 23: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 23/354

1 , +un the (ule Configuration )ilesi%$ %$e Mule I.E Eclipse plu#in ins%alled, @e do no% need a s%ar%er pro#ram i% is possible %o

FrunG %$e Mule con)i#ura%ion )iles direc%ly in Eclipse• .ependin# on %$e Mule dis%ribu%ion on %$e pro4ec%'s classpa%$, selec% a Mule con)i#ura%ion

)ile($a% is, i) you $ave Mule 3 x libraries on %$e classpa%$, %$en selec% %$e mule&con)i#3 xmlcon)i#ura%ion )ile, i) you $ave Mule 2 x libraries on %$e classpa%$, %$en use %$e mule&con)i#2 xml )ile

• Ri#$%&clic/ %$e selec%ed Mule con)i#ura%ion )ile and selec% Run As &L Mule erver

Runnin# a Mule con)i#ura%ion )ile in Eclipse @i%$ou% a s%ar%er pro#ram

• 1bserve %$e console($ere s$ould be console ou%pu% similar %o %$a% @e sa@ @$en s%ar%in# Mule usin# %$e s%ar%er

pro#ram• (ry %$e @eb service usin# soapBI

($e 1A* @eb service s$ould, o) course, be up and runnin# as earlier

($is concludes %$e example s$o@in# $o@ %o run Mule in a s%andalone pro#ram As a bonus @e alsosa@ $o@ %o run Mule in %$e Eclipse developmen% environmen%

23

Page 24: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 24/354

2. Mule in Web !pplicationsIn %$is c$ap%er @e @ill loo/ a% $o@ %o run a Mule ins%ance embedded in %$e (omca% @eb con%ainer

ince I be#an @ri%in# %$is c$ap%er 4us% be)ore %$e release o) Mule 3 2 communi%y version, I %ried%$e examples on %$ree versions o) MuleJ 2 2 !, 3 ! and 3 2 ince Mule 3 ! proved %o re0uirespecial %rea%men%, I decided %o /eep %$e no%es abou% Mule 3 ! in %$is c$ap%er

($ere are %$ree @ays Mule can be used in a con%ainer:• Embedded in a (omca% ins%ance

All Mule con)i#ura%ion )iles )rom di))eren% ARs use a common Mule codebaseRe0ues%s over <((* can be direc%ed %o pass %$rou#$ a servle% %$a% rou%es re0ues%s %o %$eappropria%e service ($is preven%s us )rom s%ar%in# a Mule server @i%$in (omca%1ne Mule con%ex% is crea%ed per @eb applica%ion %$a% con%ains one or more Mulecon)i#ura%ion )iles

• Embedded in an ins%ance o) a ?"A ! 8 complian% con%ainer• Embedded in a @eb applica%ion

($e @eb applica%ion con%ains %$e necessary Mule libraries Eac$ suc$ applica%ion run i%so@n Mule codebase ($is use case is very similar %o usin# Mule in a s%andalone applica%ionand so no example @ill be #iven

($is c$ap%er's examples @ill use (omca% In addi%ion %o %$e server and %$e appropria%e Muledis%ribu%ions, @e @ill also need %$e )ollo@in# libraries:

• Mule 2 x only:Fcommons&lo##in#&! ! 4arG ?AR )ile I% can be do@nloaded )rom$ere

• Mule 3 ! only:($e ?ac/son dis%ribu%ion enclosed @i%$ Mule seems %oo old and mus% be replaced @i%$ ane@er version Bse version ! ; or la%er .o@nload ?ac/son $ereI @ill use version ! ; 7, @$ic$ is %$e la%es% s%able version o) %$e ! ; branc$, in %$is c$ap%er

• Mule 2 x only:($e mule&module&%omca%&2 2 +& 9A* <1( 4ar ?AR )ile can be do@nloaded$ere

25

Page 25: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 25/354

" 1 Prepare Tomcat for (ule De$elopment($e )ollo@in# s%eps assume %$a% you $ave do@nloaded and ins%alled(omca% As be)ore, I $avec$osen %o use (omca% , bu% any )airly recen% version s$ould @or/ %oo

I) you in%end %o @or/ %$rou#$ bo%$ %$e Mule 2 x and Mule 3 x examples o) %$is c$ap%er, you s$ould

)irs% prepare (omca% )or Mule 2 x developmen% accordin# %o %$e ins%ruc%ions belo@ and %$en come bac/ )or a second i%era%ion )or Mule 3 x

!dd the "ecessar# $ibraries to the %o&cat Server

6irs% @e'll add %$e necessary libraries %o %$e (omca% ins%ance in @$ic$ @e are %o develop %$isc$ap%er's example pro#ram ($is is a common s%ep %$a% needs %o be under%a/en re#ardless o)@$e%$er you are runnin# (omca% )rom @i%$in Eclipse or s%andalone

In %$e )ollo@in# ins%ruc%ions, NO(1M"A(P<1MEQ re)ers %o %$e (omca% ins%alla%ion direc%ory and NOMB-EP<1MEQ re)ers %o %$e roo% direc%ory o) %$e Mule binary dis%ribu%ion

In %$e NO(1M"A(P<1MEQ direc%ory, crea%e a direc%ory named Fmule&libsG• "opy %$e con%en%s o) %$e NOMB-EP<1MEQ=lib direc%ory, excludin# %$e Fboo%G direc%ory %o

%$e NO(1M"A(P<1MEQ=mule&libs direc%ory crea%ed in %$e previous s%ep($e pic%ure belo@ s$o@s %$e direc%ories %o copy

"opy %$e direc%ories in %$e Mule dis%ribu%ion %o %$e mule&libs direc%ory in %$e (omca% ins%alla%ion

• Mule 2 x only:"opy %$e mule&module&%omca%&2 2 +& 9A* <1( 4ar ?AR )ile %o %$e NO(1M"A(P<1MEQ=mule&libs=mule direc%ory

• Mule 3 x only:"opy %$e @rapper&3 2 3 4ar ?AR )ile )rom %$e NOMB-EP<1MEQ=lib=boo% direc%ory %o %$e NO(1M"A(P<1MEQ=mule&libs=op% direc%ory

• Mule 3 ! only:.ele%e %$e )ollo@in# ?AR )iles )rom %$e NO(1M"A(P<1MEQ=mule&libs=op% direc%ory:

4ac/son&core&asl&! 3 ! 4ar, 4ac/son&4axrs&! 3 ! 4ar, 4ac/son&mapper&asl&! 3 ! 4ar "opy %$e )ollo@in# ?AR )iles %o %$e NO(1M"A(P<1MEQ=mule&libs=op% direc%ory:

4ac/son&core&asl&! ; 7 4ar, 4ac/son&4axrs&! ; 7 4ar, 4ac/son&mapper&asl&! ; 7 4ar, 4ac/son&xc&! ; 7 4ar

28

Page 26: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 26/354

• Mule 2 x and 3 ! :6rom %$e NOMB-EP<1MEQ=lib=boo% direc%ory, copy %$e )ollo@in# ?AR )iles %o %$e NO(1M"A(P<1MEQ=mule&libs=op% direc%ory:

4cl! 5&over&sl)54&! 8 4ar, lo#54&! 2 !5 4ar, sl)54&api&! 8 4ar, sl)54&lo#54!2&! 8 4ar

"opy %$e above ?AR )iles )rom %$e Mule dis%ribu%ion %o %$e mule&libs=op% direc%ory in %$e (omca% ins%alla%ion

Mule 2 x only:"opy %$e commons&lo##in#&! ! 4ar ?AR )ile %o %$e NO(1M"A(P<1MEQ=mule&libs=op%direc%ory

($e necessary libraries are no@ in place and @e are ready %o con)i#ure %$e (omca% ins%ance

27

Page 27: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 27/354

Page 28: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 28/354

• In %$e FVM ar#umen%sG )ield, inser% %$e )ollo@in# parame%ers )irs% in %$e parame%er s%rin#:-Xms512m -Xmx1024m -XX:PermSize=512m -XX:MaxPermSize=512m

I) any o) %$e above parame%ers are already presen%, remove %$e old value

Addin# memory con)i#ura%ion VM parame%ers in %$e (omca% launc$ con)i#ura%ion

• In %$e *ac/a#e Explorer, loca%e %$e ervers )older and expand %$e node )or %$e (omca%ins%ance you in%end %o use )or Mule developmen%

-oca%in# %$e server se%%in#s )iles )or %$e (omca% ins%ance in Eclipse

Hcon%inued on nex% pa#e

2;

Page 29: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 29/354

• .ouble&clic/ %$e )ile server xml and add %$e -is%enerL c$ild elemen% belo@ %o %$e erverLelemen% in %$e )ile<Listener className="org.mule.module.tomcat.MuleTomcatListener" />($e relevan% se#men% o) %$e server xml )ile s$ould loo/ li/e %$is, @i%$ %$e ne@ -is%enerLelemen% $i#$li#$%ed:

...

<Server port="8005" shutdown="SHUTDOWN"> <!-- Security listener. Documentation at /docs/config/listeners.html <Listener className="org.apache.catalina.security.SecurityListener" /> --> <!--APR library loader. Documentation at /docs/apr.html --> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html --> <Listener className="org.apache.catalina.core.JasperListener" /> <!-- Prevent memory leaks due to use of particular java/javax APIs--> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

<!-- Mule loader. --> <Listener className="org.mule.module.tomcat.MuleTomcatListener" />

<!-- Global JNDI resources Documentation at /docs/jndi-resources-howto.html --> <GlobalNamingResources> ...

• ave %$e modi)ied server xml )ile• .ouble&clic/ %$e ca%alina proper%ies )ile and append %$e )ollo@in# s%rin# %o %$e value o) %$e

common loader proper%y Hno%e %$e comma )irs% in %$e s%rin# :,NOca%alina $omeQ=mule&libs=user= 4ar,NOca%alina $omeQ=mule&libs=mule= 4ar, NOca%alina $omeQ=mule&libs=op%= 4ar ($e resul% s$ould loo/ li/e %$is, @i%$ %$e modi)ied proper%y $i#$li#$%ed:

...## List of comma-separated paths defining the contents of the "common"# classloader. Prefixes should be used to define what is the repository type.# Path may be relative to the CATALINA_HOME or CATALINA_BASE path or absolute.# If left as blank,the JVM system loader will be used as Catalina's "common"# loader.# Examples:# "foo": Add this folder as a class repository# "foo/*.jar": Add all the JARs of the specified folder as class# repositories# "foo/bar.jar": Add bar.jar as a class repositorycommon.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar,${catalina.home}/mule-libs/user/*.jar,${catalina.home}/mule-

libs/mule/*.jar, ${catalina.home}/mule-libs/opt/*.jar...

• ave %$e modi)ied ca%alina proper%ies )ile

($e con)i#ura%ion o) %$e (omca% server, @$en run )rom @i%$in Eclipse, is no@ )inis$ed

2+

Page 30: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 30/354

Configuring a Standalone %o&cat Server

9ex% @e'll con)i#ure %$e (omca% ins%ance )or use )rom ou%side o) Eclipse Examples o) suc$ use is@$en (omca% is s%ar%ed as a service or )rom %$e %erminal($e )ollo@in# s%eps can be s/ipped i) you only in%end %o run %$e (omca% ins%ance )rom @i%$inEclipseIn %$e )ollo@in# ins%ruc%ions, NO(1M"A(P<1MEQ re)ers %o %$e (omca% ins%alla%ion direc%ory

• 1pen %$e )ile NO(1M"A(P<1MEQ=con)=server xml and add %$e -is%enerL c$ild elemen% belo@ %o %$e erverL elemen% in %$e )ile<Listener className="org.mule.module.tomcat.MuleTomcatListener" />($e resul% s$ould loo/ li/e %$is H%$e ne@ -is%enerL elemen% $i#$li#$%ed :

...<Server port="8005" shutdown="SHUTDOWN"> <!-- Security listener. Documentation at /docs/config/listeners.html <Listener className="org.apache.catalina.security.SecurityListener" /> --> <!--APR library loader. Documentation at /docs/apr.html --> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />

<!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html --> <Listener className="org.apache.catalina.core.JasperListener" /> <!-- Prevent memory leaks due to use of particular java/javax APIs--> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

<!-- Mule loader. --> <Listener className="org.mule.module.tomcat.MuleTomcatListener" />

<!-- Global JNDI resources Documentation at /docs/jndi-resources-howto.html --> <GlobalNamingResources> ...

• ave %$e modi)ied server xml )ile• 1pen %$e )ile NO(1M"A(P<1MEQ=con)=ca%alina proper%ies and append %$e )ollo@in# s%rin#

%o %$e value o) %$e common loader proper%y:,NOca%alina $omeQ=mule&libs=user= 4ar,NOca%alina $omeQ=mule&libs=mule= 4ar, NOca%alina $omeQ=mule&libs=op%= 4ar ($e resul% s$ould loo/ li/e %$is Hmodi)ied proper%y $i#$li#$%ed :

...### List of comma-separated paths defining the contents of the "common"# classloader. Prefixes should be used to define what is the repository type.# Path may be relative to the CATALINA_HOME or CATALINA_BASE path or absolute.# If left as blank,the JVM system loader will be used as Catalina's "common"# loader.# Examples:# "foo": Add this folder as a class repository# "foo/*.jar": Add all the JARs of the specified folder as class# repositories# "foo/bar.jar": Add bar.jar as a class repositorycommon.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar,${catalina.home}/mule-libs/user/*.jar,${catalina.home}/mule-libs/mule/*.jar, ${catalina.home}/mule-libs/opt/*.jar...

ave %$e modi)ied ca%alina proper%ies )ile

3

Page 31: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 31/354

Page 32: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 32/354

" " Tomcat (ule Example #eb -pplicationIn %$is sec%ion @e @ill loo/ a% $o@ %o develop a @eb applica%ion %$a% use Mule %o expose a 1A*@eb service and deploy i% %o a (omca% ins%ance prepared as describedearlier Mule @ill be con)i#ured %o receive re0ues%s %o %$e @eb service over <((* usin# a servle% %$a% runsin (omca%, %$a% is, no addi%ional server @ill be s%ar%ed

Create the 'ro(ect

($e /ind o) Eclipse pro4ec% used in %$is example is a re#ular dynamic @eb pro4ec% pro4ec% @i%$ are#ular @eb xml deploymen% descrip%or

• In Eclipse, crea%e a .ynamic eb *ro4ec%I call my pro4ec% F(omca%Mule ebApplica%ionG I% is %o be %ar#e%ed a% %$e Apac$e (omca%ins%ance %$a% @e prepared )or Mule developmen% earlier

"rea%in# a dynamic @eb pro4ec% in Eclipse

32

Page 33: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 33/354

• "lic/ %$e 6inis$ bu%%on in %$e lo@er ri#$% corner

($a% is ac%ually all @e need %o do @$en crea%in# %$e pro4ec%($ere are some special con)i#ura%ion re0uired in %$e @eb xml deploymen% descrip%or %$a% @e @illloo/ a% la%er

Create the Service )&ple&entation Class($e service implemen%a%ion class is %$e service implemen%a%ion class )or %$e <ello ervicedeveloped in c$ap%er ! 9o special considera%ions need %o be made @$en deployin# %$e serviceimplemen%a%ion class %o a (omca% serverpackage com.ivan.mule;

import java.util.Date;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;

/** * SOAP web service endpoint implementation class that implements * a service that extends greetings. *

* @author Ivan A Krizsan */@WebServicepublic class HelloService{ /** * Default constructor. * Logs creation of service instances. */ public HelloService() { System.out.println("***** HelloService instance created."); }

/**

* Greets the person with the supplied name. ** @param inName Name of person to greet.

* @return Greeting. */ @WebResult(name="greeting") public String greet(@WebParam(name="name") String inName) { return "Hello " + inName + ", the time is now " + new Date(); }}

33

Page 34: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 34/354

Create the Mule Configuration Files

($e Mule con)i#ura%ions )iles used in %$is example are iden%ical %o %$ose used in %$e previousc$ap%er , excep% )or one small modi)ica%ion @e need %o do @$en deployin# %o a @eb applica%ionIn %$is example, I $ave placed %$e Mule con)i#ura%ion )iles in %$e pac/a#e com ivan mule, nex% %o%$e service implemen%a%ion class in order %o s$o@ $o@ %$e loca%ion o) %$e Mule con)i#ura%ion )ilecan be con)i#ured @$en deployin# %o (omca%

• In %$e pac/a#e com ivan mule, crea%e a )ile named Fmule&con)i#2 xmlG @i%$ %$e )ollo@in#con%en%s:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://www.mulesource.org/schema/mule/cxf/2.2" xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.2" xsi:schemaLocation=" http://www.mulesource.org/schema/mule/cxf/2.2 http://www.mulesource.org/schema/mule/cxf/2.2/mule-cxf.xsd

http://www.mulesource.org/schema/mule/core/2.2

http://www.mulesource.org/schema/mule/core/2.2/mule.xsd http://www.mulesource.org/schema/mule/stdio/2.2

http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd">

<!-- A model contains a set of services. --> <model name="GreetingModel"> <!-- A service specifies how messages are received, to which component the messages are delivered and how the result is handled. --> <service name="GreetingService"> <!-- The <inbound> element specifies how the service receives messages. Note that the address of the inbound endpoint has been changed. This has been done in order to use the server this web application is deployed to and avoid starting another server. To access the WSDL of the greeting service, use the following URL: http://localhost:8080/MuleWebApplication/mule/services/GreetingService?wsdl --> <inbound> <cxf:inbound-endpoint address="servlet://GreetingService"/> </inbound>

<!-- Specifies the component that will be invoked when there is an incoming message.

The component can be one of the following types: singleton-object: one single instance handles all messages. prototype-object: each instance handles a single message. spring-object: messages handled by specified Spring bean. --> <component> <prototype-object class="com.ivan.mule.HelloService"/> </component>

<!-- Specifies where to direct responses. Responses are results of incoming messages having been processed by the above component. --> <outbound> <pass-through-router> <!-- Logs messages to the console.

35

Page 35: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 35/354

--> <stdio:outbound-endpoint system="OUT"/> </pass-through-router> </outbound> </service> </model></mule>

• In %$e pac/a#e com ivan mule, crea%e a )ile named Fmule&con)i#3 xmlG @i%$ %$e )ollo@in#con%en%s:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns:stdio="http://www.mulesoft.org/schema/mule/stdio" xmlns:spring="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/3.2/mule-cxf.xsd

http://www.mulesoft.org/schema/mule/core

http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd http://www.mulesoft.org/schema/mule/stdio

http://www.mulesoft.org/schema/mule/stdio/3.2/mule-stdio.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<flow name="GreetingFlow"> <!-- The <inbound> element specifies how the service receives messages. Note that the address of the inbound endpoint has been changed. This has been done in order to use the server this web application is deployed to and avoid starting another server. To access the WSDL of the greeting service, use the following URL: http://localhost:8080/MuleWebApplication/mule/services/GreetingService?wsdl --> <inbound-endpoint address="servlet://GreetingService" exchange-pattern="request-response"/> <!-- Incoming requests are received using a JAX-WS web service using the Apache CXF web service stack. The CXF jaxws-service honors JAX-WS annotations in the service class. Note that the serviceClass attribute must specify the class implementing the service. --> <cxf:jaxws-service serviceClass="com.ivan.mule.HelloService"/>

<!--

Despite having specified the service class in the above element, we must supply a <component> specifying the object to be invoked when having received a message. --> <component> <!-- The component implementation object can be specified either using a Spring bean, a singleton object or a prototype object. The component can be one of the following types: singleton-object: one single instance handles all requests. prototype-object: each instance handles a single request. spring-object: requests handled by specified Spring bean. --> <spring-object bean="helloService"/> </component>

<!--

38

Page 36: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 36/354

The log component can be inserted in a flow to log the content of a message being passed through the log component. --> <log-component/> </flow>

<!-- In the <spring:beans> element we can declare Spring beans

and other things that can be done in a regular Spring configuration file. --> <spring:beans> <!-- This Spring bean supplies the implementation of the Hello service. --> <spring:bean id="helloService" class="com.ivan.mule.HelloService"/> </spring:beans></mule>

9o%e %$a%:• ($e address a%%ribu%e o) %$e cx):inbound&endpoin%L elemen% in bo%$ %$e con)i#ura%ion )iles

$as %$e value Fservle%:==Cree%in# erviceG($e pre)ix Fservle%:==G %ells Mule %$a% @e @an% %o use %$e servle% %ranspor% and re#is%ers %$eendpoin% @i%$ %$e servle% in%e#ra%ed @i%$ %$e servle% con%ainer($is )acili%a%es in%e#ra%ion @i%$ %$e servle% con%ainer, (omca% in our case, and preven%s %$eopenin# o) an addi%ional por% )or communica%ion @i%$ %$e Mule endpoin%I% is possible %o use %$e same con)i#ura%ion )or %$e endpoin% address as in a s%andaloneapplica%ion

• I) you @an% %o deploy %$e Mule con)i#ura%ion )ile %o Mule 3 ! , you need %o replaceoccurrences o) F3 2G in %$e schemaLocation a%%ribu%e @i%$ F3 !G

Create the *eplo#&ent *escriptor

$en deployin# %o a @eb con%ainer li/e (omca%, @e need %o con)i#ure %$e @eb applica%iondeploymen% descrip%or in order %o s%ar% Mule and %o %ell Mule @$ere our Mule con)i#ura%ion )ileHsare loca%ed

• In %$e Eclipse pro4ec%, in %$e eb"on%en%= E>&I96 direc%ory, crea%e a )ile namedF@eb xmlG @i%$ %$e )ollo@in# con%en%s:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<display-name>TomcatMuleWebApplication</display-name>

<!-- Tell Mule where the Mule configuration file is located. More than one configuration file can be supplied by separating the filenames with commas. --> <context-param> <param-name>org.mule.config</param-name> <param-value>com/ivan/mule/mule-config2.xml</param-value> </context-param>

37

Page 37: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 37/354

<!-- Servlet context listener that loads the Mule configuration file(s) and creates a MuleContext. --> <listener> <listener-class> org.mule.config.builders.MuleXmlBuilderContextListener </listener-class> </listener>

<!-- Receives HTTP requests via a servlet and routes them to services that has an address starting with servlet://, that is that uses the servlet transport. This approach facilitates closer integration with the servlet container, Tomcat in our case. --> <servlet> <servlet-name>muleServlet</servlet-name> <servlet-class>org.mule.transport.servlet.MuleReceiverServlet</servlet-class> <load-on-startup>100</load-on-startup> </servlet>

<!-- Set the URL pattern used to access exposed Mule services. The URL pattern is appended to the server address and the web application context. --> <servlet-mapping> <servlet-name>muleServlet</servlet-name> <url-pattern>/mule/services/*</url-pattern> </servlet-mapping>

<welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list></web-app>

9o%e %$a%:•

($e deploymen% descrip%or con%ains a con%ex% parame%er @i%$ %$e name For# mule con)i#Gand %$e value Fcom=ivan=mule=mule&con)i#2 xmlG($is con%ex% parame%er is used %o in)orm Mule abou% %$e name and loca%ion o) ourcon)i#ura%ion )ileMul%iple Mule con)i#ura%ion )iles may be speci)ied by separa%in# %$e names @i%$ commas

$en runnin# %$e Mule 3 x version o) %$e example, c$an#e %$e value o) %$is con%ex% parame%er %o Fcom=ivan=mule=mule&con)i#3 xmlG ($is is %$e only c$an#e re0uired %o s@i%c$ be%@een %$e %@o versions, apar% )rom %$e libraries in (omca%

• ($ere is a lis%enerL elemen% in %$e deploymen% descrip%or %$a% con%ains a lis%ener&classLelemen% @i%$ %$e value For# mule con)i# builders Mule ml>uilder"on%ex%-is%enerG($is servle% con%ex% lis%ener is responsible )or )indin# Mule con)i#ura%ion )iles, usin# )orins%ance %$e con%ex% parame%er discussed above, and crea%in# a Mule con%ex% usin# %$esecon)i#ura%ion )iles

• ($ere is a servle%L elemen% %$a% con)i#ures a servle% named Fmule ervle%G($is con)i#ura%ion is op%ional and only necessary i) any endpoin%s in our Mule con)i#ura%ion)ile uses %$e servle% %ranspor% discussed above in connec%ion %o %$e Mule con)i#ura%ion )ile

• ($ere is a servle%&mappin#L elemen% %$a% maps %$e Fmule ervle%G %o %$e BR- pa%%ernF=mule=services= G($is servle% mappin# speci)ies %$e common BR- pre)ix )or endpoin%s usin# %$e servle%%ranspor% e @ill loo/ more closely a% %$e BR- o) %$e service in our example a)%er $avin#

deployed %$e @eb applica%ion

3

Page 38: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 38/354

*eplo# the Web !pplication

i%$ %$e Mule con)i#ura%ion )ile, %$e service implemen%a%ion class and %$e @eb applica%iondeploymen% descrip%or in place, @e are no@ ready %o deploy %$e @eb applica%ion

• %ar% %$e (omca% server @$ic$ $as been prepared )or Mule developmen%

In Eclipse, ri#$%&clic/ %$e server in %$e ervers vie@ and selec% %ar%In a s%andalone (omca% server, navi#a%e %o %$e FbinG direc%ory in %$e (omca% ins%alla%iondirec%ory and execu%e %$e scrip% Fs%ar%up s$G, or Fs%ar%up ba%G i) you are usin# a .1 &basedopera%in# sys%em

• .eploy %$e @eb applica%ion %o %$e (omca% serverIn Eclipse, ri#$%&clic/ %$e pro4ec% in %$e *ac/a#e or *ro4ec% explorer vie@ and selec%Run As&LRun 1n erver In %$e dialo# %$a% appears, ma/e sure %$e appropria%e (omca%server is selec%ed and clic/ %$e 6inis$ bu%%onI) you are as/ed @$e%$er %o res%ar% %$e server, selec% F"on%inue @i%$ou% res%ar%in#G"onsole ou%pu% similar %o %$e )ollo@in# s$ould be #enera%ed:

...Sep 30, 2011 3:46:41 PM org.apache.catalina.startup.HostConfig deployDirectoryINFO: Deploying web application directory TomcatMuleWebApplicationSep 30, 2011 3:46:54 PM org.apache.cxf.bus.spring.BusApplicationContextgetConfigResourcesINFO: No cxf.xml configuration file detected, relying on defaults.***** HelloService instance created.Sep 30, 2011 3:46:56 PM org.apache.cxf.service.factory.ReflectionServiceFactoryBeanbuildServiceFromClassINFO: Creating Service {http://mule.ivan.com/}HelloServiceService from classcom.ivan.mule.HelloServiceSep 30, 2011 3:46:56 PM org.apache.cxf.endpoint.ServerImpl initDestinationINFO: Setting the server's publish address to be servlet://GreetingService...

9o%e %$a% an ins%ance o) %$e <ello ervice endpoin% implemen%a%ion class @as crea%ed as par% o)deployin# %$e @eb applica%ion

In addi%ion, an error may be lo##ed:

...[09-30 15:46:56] ERROR ServiceService [Thread-9]: Error post-registering the MBeanjavax.management.MalformedObjectNameException: Invalid character ':' in value part ofproperty...

($is error occurs @$en %ryin# %o re#is%er an M>ean )or ?M mana#emen% @i%$ an ille#al c$arac%erin one o) %$e proper%y names ($e error does no% a))ec% %$e )unc%ionali%y o) our example pro#ramand can be i#nored

3;

Page 39: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 39/354

%est the Service

i%$ %$e @eb applica%ion success)ully deployed, @e s$ould no@ be able %o vie@ %$e .- o) %$eservice exposed usin# Mule

• In a @eb bro@ser, open %$e )ollo@in# BR-:

$%%p:==local$os%:; ; =(omca%Mule ebApplica%ion=mule=services=Cree%in# ervice @sdl($e .- s$ould appear• 9o%e $o@ %$e BR- o) %$e service is crea%ed by appendin# Fmule=servicesG %o %$e @eb

applica%ion BR- and %$en %$e name o) %$e service, FCree%in# erviceG, as speci)ied in %$eMule con)i#ura%ion )ile

• Bsin# soapBI, %es% %$e service and ma/e sure you receive a #ree%in# in %$e )ollo@in# )orma%)rom %$e service:

Hello Ivan, the time is now Mon Oct 03 05:33:33 CEST 2011

($e service $as an BR- %$a% Fis par% o)G %$e @eb applica%ion, %$a% is uses %$e same por% and con%ex%roo% as %$e @eb applica%ion In addi%ion, and also si#ni)ican% %o some ex%en%, %$e service ans@ersre0ues%s as expec%ed

I) you @an% %o embed Mule in %$e @eb applica%ion, ins%ead o) in %$e (omca% server, %$en copy %$eMule libraries men%ioned above %o %$e library )older o) %$e @eb applica%ion H E>&I96=lib ins%eado) copyin# %$em %o %$e (omca% server Also remember %$a% no con)i#ura%ion o) %$e (omca% server isnecessary in %$is case

I) you @is$ %o repea% %$e exercise )or Mule 3 x, do %$e )ollo@in#:• %op %$e (omca% server i) i% is runnin#•

Remove %$e Mule 2 x libraries )rom (omca%• Add %$e Mule 3 x libraries %o (omca%, as describedabove• "$an#e %$e Mule con)i#ura%ion )ile used in %$e @eb xml deploymen% descrip%or• Res%ar% (omca% and redeploy %$e @eb applica%ion

($is concludes %$e example s$o@in# $o@ %o develop and deploy a @eb applica%ion %$a% uses Mule%o a (omca% server

3+

Page 40: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 40/354

3. Modular Mule Configuration($e example pro#ram in %$is c$ap%er @ill promp% %$e user )or console inpu%, reverse %$e en%ereds%rin# and prin% i% on %$e console 1ccasionally, @$ic$ in %$is case means randomly, %$e s%rin#

processin# @ill %$ro@ an excep%ion

e @ill loo/ a% %$e )ollo@in# in various de#ree o) de%ail:• Modularizin# Mule con)i#ura%ion )iles

9o% en%irely obvious in suc$ a small example, bu% dividin# Mule con)i#ura%ion in mul%iple)iles can $elp us or#anize %$in#s

• .e)ine paren%&c$ild rela%ions$ips be%@een modelsAs in ob4ec%&orien%ed pro#rammin#, %$is %ec$ni0ue can be used %o re)ac%or ou% proper%iescommon %o several modules and avoid duplica%ion

• Implemen% cus%om excep%ion $andlin#In Mule 2 x, @e @ill use a cus%om excep%ion lis%ener, and in Mule 3 x @e @ill use a cus%omexcep%ion $andler($e example pro#ram @ill #ive a brie) in%roduc%ion %o excep%ion $andlin# in Mule ($issub4ec% $as a c$ap%er o) i%s o@n devo%ed %o i%la%er in %$is boo/

e @ill use %$e %ec$ni0ue o) runnin# %$e Mule con)i#ura%ion )iles, described in a previous c$ap%er , so @e @ill no% need a s%ar%er class

& 1 Create the Pro!ect"rea%e %$e pro4ec% as described in %$e appendix"rea%e a Mule *ro4ec%, namin# i%FMuleModular"on)i#ura%ionG ($e Mule 3 $o% deploymen% can be s@i%c$ed o)) )rom %$e s%ar%, as%$is )ea%ure is no% o) use @$en runnin# Mule embedded

5

Page 41: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 41/354

& " Create the Ser$ice %mplementation Class($e @eb service endpoin% implemen%a%ion class implemen%s a service %$a% ex%ends #ree%in#s

• In %$e source roo%, crea%e %$e pac/a#ecom.ivan.mule• In %$e ne@ pac/a#e, crea%e %$e class #everseStringService @i%$ %$e )ollo@in# con%en%s:

package com.ivan.mule;

/** * Service that reverses strings. *

* @author Ivan A Krizsan */public class ReverseStringService{ /** * Default constructor. * Logs creation of service instances. */ public ReverseStringService() {

System.out.println("***** ReverseStringService instance created."); }

/** * Reverses the string with the supplied name. *

* @param inString String to reverse. * @return Reversed string. */ public String reverse(String inString) { /* Sometimes an error occurs. */ if (Math.random() > 0.8) { throw new Error("An exception occurred in the reverse-service"); }

StringBuffer theBuf = new StringBuffer(inString); return theBuf.reverse().toString(); }}

9o%e %$a%:• ($e #everseStringService class is comple%ely decoupled )rom any%$in# rela%ed %o Mule

($is #ives us %$e )reedom %o, usin# Mule, expose %$e service in di))eren% @ays, @i%$ou%$avin# %o c$an#e %$e service implemen%a%ion

5!

Page 42: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 42/354

& & Create the Custom Exception .an/ler0Listener Classes($ere are %@o di))eren% cus%om excep%ion $andler=lis%ener classes in %$e example pro#ramJ one )orMule 2 x and one )or Mule 3 x ($is is due %o %$e )ac% %$a% cus%om excep%ion $andlers=lis%enersimplemen% di))eren% in%er)aces in Mule 2 x and Mule 3 x>o%$ %$e excep%ion lis%ener=$andler classes $ave a proper%y %$a% is se% a% crea%ion %ime and prin%ed@$enever %$ere is an excep%ion ($is is 4us% %o s$o@ $o@ excep%ion $andlers=lis%eners can becus%omized @i%$ proper%ies and $o@ %$ese are con)i#ured in %$e Mule con)i#ura%ion )ile

$en s@i%c$in# %o %$e Mule 2 x run%ime libraries, %$ere @ill be compila%ion errors in %$e Mule 3excep%ion $andler class ($is is normal and s$ould be i#nored

Create the Mule 2.x Custo& Exception $istener

($e Mule 2 x excep%ion lis%ener class is named M$Mule%&xceptionListener and implemen%ed as)ollo@s:

package com.ivan.mule;

import java.beans.ExceptionListener;

/** * Custom Mule 2 exception listener. *

* @author Ivan A Krizsan */public class MyMule2ExceptionListener implements ExceptionListener{ private String mListenerProperty;

/** * Default constructor. * Logs creation of instances of the exception listener. */ public MyMule2ExceptionListener() { System.out.println("*** MyMule2ExceptionListener created"); }

/* (non-Javadoc) * @see java.beans.ExceptionListener#exceptionThrown(java.lang.Exception) */ @Override public void exceptionThrown(final Exception inException) { System.out.println("*** MyMule2ExceptionListener.exceptionThrown: " + inException.getLocalizedMessage()); System.out.println(" Listener property: " + mListenerProperty); }

public String getListenerProperty()

{ return mListenerProperty; }

public void setListenerProperty(String inListenerProperty) { System.out.println( "*** Setting MyMule2ExceptionListener.listenerProperty: " + inListenerProperty); mListenerProperty = inListenerProperty; }}

52

Page 43: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 43/354

Page 44: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 44/354

Create the Mule 3.x Custo& Exception +andler

($e Mule 3 x excep%ion lis%ener class is named M$Mule)&xceptionHandler and implemen%ed as)ollo@s:

package com.ivan.mule;

import org.mule.api.MuleEvent;import org.mule.api.exception.MessagingExceptionHandler;

/** * A custom Mule 3 exception handler. *

* @author Ivan A Krizsan */public class MyMule3ExceptionHandler implements MessagingExceptionHandler{ private String mListenerProperty;

/** * Default constructor. * Logs creation of instances of the exception handler. */ public MyMule3ExceptionHandler() { System.out.println("*** MyMule3ExceptionHandler created"); }

/* (non-Javadoc) * @seeorg.mule.api.exception.MessagingExceptionHandler#handleException(java.lang.Exception,org.mule.api.MuleEvent) */ @Override public MuleEvent handleException(Exception inException, MuleEvent inEvent) { System.out.println("*** MyMule3ExceptionHandler.exceptionThrown: " + inException.getLocalizedMessage()); System.out.println(" Listener property: " + mListenerProperty);

return inEvent; }

public String getListenerProperty() { return mListenerProperty; }

public void setListenerProperty(String inListenerProperty) { System.out.println( "*** Setting MyMule3ExceptionHandler.listenerProperty: " + inListenerProperty); mListenerProperty = inListenerProperty; }}

9o%e %$a%:• ($e class implemen%s %$e Messaging&xceptionHandler in%er)ace

($is in%er)ace is par% o) %$e Mule 3 x A*I and con%ains one sin#le me%$od & %$ehandle&xception me%$od<avin# your excep%ion lis%ener implemen% %$is in%er)ace is %$e mos% basic approac$ %oimplemen%in# an excep%ion lis%ener

• ($e handle&xception me%$od %a/es %@o parame%ers($e )irs% parame%er is %$e excep%ion %$a% caused %$e excep%ion lis%ener %o be invo/ed

($e second parame%er is a Mule even% ob4ec% represen%in# %$e Mule even% %$a% @as processed

55

Page 45: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 45/354

@$en %$e excep%ion occurred

• ($e handle&xception me%$od re%urns a Mule even% ob4ec%($e even% ob4ec% re%urned is %$e even% %$a% is %o con%inue %o be rou%ed %$rou#$ %$e remainin#

par% o) %$e )lo@•

($e excep%ion $andler is invo/ed @$en excep%ions occur durin# processin# o) a messa#e"ompare %$is %o %$e Mule 2 x excep%ion lis%ener and %$e Mule 3 x in%er)aceS$stem&xceptionHandler ($e la%%er is %o be implemen%ed by Mule 3 x excep%ion $andlers%$a% are %o $andle excep%ions %$a% occur @$en a messa#e is no% processed

• ($e class $as an ins%ance variableJ mListenerPropert$ and associa%ed #e%%er and se%%erme%$odsAs @i%$ any o%$er ?ava bean, %$is exposes a proper%y named Flis%ener*roper%yG

• ($e excep%ion lis%ener class $as a de)aul% cons%ruc%orI% is no% re0uired, bu% implemen% %o see @$en %$e lis%ener is ins%an%ia%ed

6or a more in&dep%$ discussion on implemen%in# cus%om Mule 3 x excep%ion lis%eners, please re)er %o %$is sec%ion in %$e c$ap%er onExcep%ion <andlin# in Mule

58

Page 46: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 46/354

& ' Create the (ule Configuration )iles($is example @ill, as men%ioned earlier, modularize %$e Mule con)i#ura%ion ($us, @e @ill crea%eseveral con)i#ura%ion )ilesJ %$ree )or Mule 2 x and %@o )or Mule 3 x ($e con)i#ura%ion )iles @ill$ave %$e )ollo@in# con%en%s:

Main con)i#ura%ion )ileIncludes %$e o%$er con)i#ura%ion )ileHs and de)ines %$e main module=)lo@• Clobal connec%or con)i#ura%ion )ile

"on)i#ures %$e (.I1 %ranspor% connec%or• *aren% model con)i#ura%ion )ile HMule 2 x only in %$is example

"on)i#ures an abs%rac% paren% model @i%$ cus%om excep%ion $andlin# In a lar#er sys%em, %$ismodule could be %$e paren% o) all modules %$a% @an%ed %o $ave cus%om excep%ion $andlin#(ec$nically, %$is is also possible @i%$ Mule 3 x, bu% @e use a )lo@ ins%ead o) a module in %$eMule 3 x con)i#ura%ion and )lo@s do no% suppor% in$eri%ance li/e modules do

Create the Mule 2.x Configuration Files

($ere are %$ree Mule 2 x con)i#ura%ion )iles, all loca%ed in %$e roo% o) %$e source direc%ory, namedFmule&con)i#2 xmlG, Fmule2&#lobal&con)i# xmlG and Fmule2&paren%model&con)i# xmlG

($e Fmule&con)i#2 xmlG )ile impor%s %$e %@o o%$er )iles and de)ines %$e model:<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.2" xmlns:spring="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd

http://www.mulesource.org/schema/mule/stdio/2.2 http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- Import configuration files that this configuration depends on. Note that the imports must be wrapped in a <spring:beans> element. Also note that it is possible to import Mule configuration files, not only Spring bean configuration files. --> <spring:beans> <spring:import resource="mule2-global-config.xml"/> <spring:import resource="mule2-parentmodel-config.xml"/>

</spring:beans> <model name="echoModel" inherit="true">

<service name="echoServiceWithReverse"> <inbound> <!-- Inbound endpoint receiving text from the standard IO console. Use a connector defined in a separate configuration file. --> <stdio:inbound-endpoint system="IN" connector-ref="SystemStreamConnector"/> </inbound>

<component> <singleton-object class="com.ivan.mule.ReverseStringService"/> </component>

57

Page 47: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 47/354

<outbound>

<pass-through-router> <stdio:outbound-endpoint system="OUT"/> </pass-through-router> </outbound> </service> </model></mule>

9o%e %$a%:• ($e con)i#ura%ion )ile is a re#ular Mule 2 x con)i#ura%ion )ile• prin# impor%L elemen%s are used %o impor% %$e o%$er %@o con)i#ura%ion )iles

>o%$ Mule con)i#ura%ion )iles as @ell as prin# bean con)i#ura%ion )iles can be impor%ed• ($e prin# impor%L elemen%s are @rapped in a prin# beansL elemen%• ($e modelL elemen% $as, as @e @ill la%er see, %$e same name as %$e paren% modelL

elemen% i% in$eri%s )rom

In addi%ion, %$e c$ild modelL elemen% also se%s %$einherit a%%ribu%e %o %rue• ($e modelL elemen% con%ains a s%dio:inbound&endpoin%L elemen%

($is elemen% is used %o speci)y $o@ %$e )lo@ receives messa#es• ($e s%dio:inbound&endpoin%L elemen% con%ains aconnector-ref a%%ribu%e

An endpoin% is a concre%iza%ion o) a connec%or ($econnector-ref a%%ribu%e is used %o speci)y%$e connec%or @$ic$ %$e endpoin% concre%izes I) %$ere is only one connec%or )or a par%icular

pro%ocol, Mule @ill au%oma%ically )ind %$e appropria%e connec%or ($us, in %$is example, i% isno% s%ric%ly necessary %o speci)y %$e connec%or re)erence

• ($e connec%or S$stemStream onnector is no% de)ined in %$e above con)i#ura%ion )ile($is connec%or is, as @e @ill s$or%ly see, de)ined in a separa%e con)i#ura%ion )ile @$ic$ could

be included by several con)i#ura%ion )iles($is allo@s us %o avoid repe%i%ion and ma/e modi)ica%ions %o %$is par%icular connec%or easier

5

Page 48: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 48/354

($e nex% )ile, Fmule2&#lobal&con)i# xmlG, con%ains %$e de)ini%ion o) %$eS$stemStream onnector%$a% @e sa@ in %$e above con)i#ura%ion )ile:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd

http://www.mulesource.org/schema/mule/stdio/2.2 http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd">

<!-- Configure the STDIO transport connector to use a specific prompt when reading data and to output a message to the console when a message, the read data, is sent. --> <stdio:connector name="SystemStreamConnector" promptMessage="Please enter text: " messageDelayTime="200" outputMessage="STDIO connector sending string. "/>

</mule>

9o%e %$a%:• ($e s%dio:connec%orL elemen% is an immedia%e c$ild o) %$e muleL elemen%

Mule allo@s us %o con)i#ure connec%ors, )il%ers, %rans)ormers e%c in separa%e con)i#ura%ion)iles %$a% can la%er be reused

• ($e s%dio:connec%orL elemen% con%ains %$ename a%%ribu%e @i%$ %$e valueF ys%em %ream"onnec%orGAs mi#$% be suspec%ed, %$is #ives %$e connec%or %$e name by @$ic$ i% may be re)erred %o Has

seen earlier @i%$ %$econnector-ref a%%ribu%e• ($e s%dio:connec%orL elemen% con%ains a promptMessage a%%ribu%e

As @e @ill see @$en @e run %$e example pro#ram, %$is a%%ribu%e can be used %o speci)y amessa#e %$a% is %o be displayed @$en %$e connec%or as/s )or inpu%

• ($e s%dio:connec%orL elemen% con%ains amessage+ela$'ime a%%ribu%e($is a%%ribu%e speci)ies %$e %ime in milliseconds %$a% %$e connec%or @ill @ai% be)ore prin%in#%$e promp% messa#e

• ($e s%dio:connec%orL elemen% con%ains anoutputMessage a%%ribu%e($is a%%ribu%e speci)ies a messa#e %$a% @ill be prin%ed %o %$e console @$en, in %$e case o) %$isexample, %$e inpu% en%ered by %$e user is sen% %o %$e nex% processin# s%a#e

5;

Page 49: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 49/354

($e )inal Mule 2 x con)i#ura%ion )ile, Fmule2&paren%model&con)i# xmlG, con%ains %$e declara%ion o)a model %$a% speci)ies a cus%om excep%ion&$andlin# s%ra%e#y:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.2" xmlns:spring="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd

http://www.mulesource.org/schema/mule/stdio/2.2 http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- Model specifying a custom exception handling strategy. This model can be used as a parent model which other models can inherit from. They will thus all have the same custom exception handling strategy. -->

<model name="echoModel"> <custom-exception-strategy class="com.ivan.mule.MyMule2ExceptionListener"> <spring:property name="listenerProperty" value="SomeData"/> </custom-exception-strategy> </model></mule>

9o%e %$a%:• ($e name o) %$e model is Fec$oModelG

All c$ild models mus% $ave %$e same name, @$ic$ @e sa@ in %$e )irs% Mule 2 xcon)i#ura%ion )ile

($e model does no% con%ain a serviceL elemen%ince %$is model is no% in%ended %o be used on i%s o@n, i% only needs %o con%ain elemen%sspeci)yin# @$a% is %o become common proper%ies @i%$ i%s c$ild&models

• ($e model con%ains a cus%om&excep%ion&s%ra%e#yL elemen%($is elemen% %ells Mule %o use %$e cus%om excep%ion&lis%ener class %$a% @e implemen%edearlier

• ($e cus%om&excep%ion&s%ra%e#yL elemen% con%ains a sprin#:proper%yL elemen%($e sprin#:proper%yL elemen% allo@s us %o in4ec% a value in%o an ins%ance )ield o) %$eexcep%ion&lis%ener class @$enever an ins%ance is crea%ed

5+

Page 50: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 50/354

Create the Mule 3.x Configuration Files

($ere are %@o Mule 3 x con)i#ura%ion )iles, loca%ed in %$e roo% o) %$e source direc%ory, namedFmule&con)i#3 xmlG and Fmule3&#lobal&con)i# xmlG

($e Fmule&con)i#3 xmlG )ile impor%s %$e o%$er con)i#ura%ion )ile and de)ines a )lo@:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns:stdio="http://www.mulesoft.org/schema/mule/stdio" xmlns:spring="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/3.2/mule-cxf.xsd

http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd

http://www.mulesoft.org/schema/mule/stdio http://www.mulesoft.org/schema/mule/stdio/3.2/mule-stdio.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- Import configuration files that this configuration depends on. Note that the imports must be wrapped in a <spring:beans> element. Also note that it is possible to import Mule configuration files, not only Spring bean configuration files. --> <spring:beans> <spring:import resource="mule3-global-config.xml"/> </spring:beans>

<flow name="GreetingFlow"> <!-- Inbound endpoint receiving text from the standard

IO console. Use a connector defined in a separate configuration file. --> <stdio:inbound-endpoint system="IN" connector-ref="ConsoleInputConnector"/>

<component> <singleton-object class="com.ivan.mule.ReverseStringService"/> </component>

<stdio:outbound-endpoint system="OUT"/>

<!-- Flows cannot inherit from other flows, so we specify the custom exception handling strategy for the flow. --> <custom-exception-strategy class="com.ivan.mule.MyMule3ExceptionHandler"> <spring:property name="listenerProperty" value="SomeData"/> </custom-exception-strategy> </flow></mule>

Hcon%inued on nex% pa#e

8

Page 51: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 51/354

9o%e %$a%:• ($e con)i#ura%ion )ile is a re#ular Mule 3 x con)i#ura%ion )ile• prin# impor%L elemen%s are used %o impor% %$e o%$er con)i#ura%ion )ile

>o%$ Mule con)i#ura%ion )iles as @ell as prin# bean con)i#ura%ion )iles can be impor%ed• ($e prin# impor%L elemen% is @rapped in a prin# beansL elemen%• ($e )lo@L elemen% con%ains a s%dio:inbound&endpoin%L elemen%

($e inbound endpoin% elemen% speci)ies $o@ %$e )lo@ receives messa#es• ($e s%dio:inbound&endpoin%L elemen% con%ains aconnector-ref a%%ribu%e

An endpoin% is a concre%iza%ion o) a connec%or ($econnector-ref a%%ribu%e is used %o speci)y%$e connec%or @$ic$ %$e endpoin% concre%izes I) %$ere is only one connec%or )or a par%icular

pro%ocol, Mule @ill au%oma%ically )ind %$e appropria%e connec%or ($us, in %$is example, i% isno% s%ric%ly necessary %o speci)y %$e connec%or re)erence

• ($e connec%or onsoleInput onnector is no% de)ined in %$e above con)i#ura%ion )ile($is connec%or is, as @e @ill s$or%ly see, de)ined in a separa%e con)i#ura%ion )ile @$ic$ could

be included by several con)i#ura%ion )iles• ($e )lo@L elemen% con%ains a cus%om&excep%ion&s%ra%e#yL elemen%

ince one )lo@ canno% in$eri% )rom ano%$er )lo@, @e $ave %o speci)y %$e excep%ion s%ra%e#yli/e %$is

• ($e cus%om&excep%ion&s%ra%e#yL elemen% con%ains a sprin#:proper%yL elemen%($e sprin#:proper%yL elemen% allo@s us %o in4ec% a value in%o an ins%ance )ield o) %$eexcep%ion&lis%ener class @$enever an ins%ance is crea%ed

8!

Page 52: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 52/354

($e nex% )ile, Fmule3&#lobal&con)i# xmlG, is iden%ical %o %$e Mule 2 x coun%erpar% excep% )or %$enamespace declara%ions and con%ains %$e de)ini%ion o) %$eS$stemStream onnector %$a% @e sa@ in%$e above con)i#ura%ion )ile:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:stdio="http://www.mulesoft.org/schema/mule/stdio"

xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd

http://www.mulesoft.org/schema/mule/stdio http://www.mulesoft.org/schema/mule/stdio/3.2/mule-stdio.xsd">

<!-- Configure the STDIO transport connector to use a specific prompt when reading data and to output a message to the console when a message, the read data, is sent. --> <stdio:connector name="ConsoleInputConnector" promptMessage="Please enter text: " messageDelayTime="200" outputMessage="STDIO connector sending string. "/></mule>

9o%e %$a%:• ($e s%dio:connec%orL elemen% is an immedia%e c$ild o) %$e muleL elemen%

Mule allo@s us %o con)i#ure connec%ors, )il%ers, %rans)ormers e%c in separa%e con)i#ura%ion)iles %$a% can la%er be reused

• ($e s%dio:connec%orL elemen% con%ains %$ename a%%ribu%e @i%$ %$e valueF ys%em %ream"onnec%orGAs mi#$% be suspec%ed, %$is #ives %$e connec%or %$e name by @$ic$ i% may be re)erred %o Has

seen earlier @i%$ %$econnector-ref a%%ribu%e• ($e s%dio:connec%orL elemen% con%ains a promptMessage a%%ribu%e

As @e @ill see @$en @e run %$e example pro#ram, %$is a%%ribu%e can be used %o speci)y amessa#e %$a% is %o be displayed @$en %$e connec%or as/s )or inpu%

• ($e s%dio:connec%orL elemen% con%ains amessage+ela$'ime a%%ribu%e($is a%%ribu%e speci)ies %$e %ime in milliseconds %$a% %$e connec%or @ill @ai% be)ore prin%in#%$e promp% messa#e

• ($e s%dio:connec%orL elemen% con%ains anoutputMessage a%%ribu%e($is a%%ribu%e speci)ies a messa#e %$a% @ill be prin%ed %o %$e console @$en, in %$e case o) %$is

example, %$e inpu% en%ered by %$e user is sen% %o %$e nex% processin# s%a#e

82

Page 53: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 53/354

& * +un the Example Programe are no@ ready %o run %$e example pro#ram e'll use %$e %ec$ni0ue o) runnin# %$e Mule

con)i#ura%ion )iles as described in %$e earlier exampleI% is assumed %$a% %$e pro4ec% is con)i#ured @i%$ %$e Mule 3 x dis%ribu%ion on %$e classpa%$ @$ens%ar%in# %$is sec%ion

• In %$e *ac/a#e or *ro4ec% Explorer, ri#$%&clic/ %$e Fmule&con)i#3 xmlG )ile and selec% RunAs &L Mule erver

• ($e Mule 3 x server s$ould s%ar% up and display some%$in# similar %o %$is on %$e consoleHsome ou%pu% omi%%ed %o conserve space :

...INFO 2011-03-17 17:36:47,595 [main] org.mule.lifecycle.AbstractLifecycleManager:Initialising connector: ConsoleInputConnector*** MyMule3ExceptionHandler created*** Setting MyMule3ExceptionHandler.listenerProperty: SomeDataINFO 2011-03-17 17:36:47,748 [main] org.mule.construct.FlowConstructLifecycleManager:Initialising flow: GreetingFlow...INFO 2011-03-17 17:36:48,017 [main] org.mule.component.ComponentLifecycleManager:Starting component: commponent.1300307500***** ReverseStringService instance created.INFO 2011-03-17 17:36:48,029 [main] org.mule.transport.stdio.PromptStdioConnector:Registering listener: GreetingFlow on endpointUri: stdio://system.in...INFO 2011-03-17 17:36:48,242 [main] org.mule.DefaultMuleContext:*********************************************************************** Mule ESB and Integration Platform ** Version: 3.2.0 Build: 22917 *...* Agents Running: ** JMX Agent ***********************************************************************

Please enter text:

9o%e %$a%:• An ins%ance o) %$e cus%om excep%ion $andler class is crea%ed and %$e value o) %$e proper%y

listenerPropert$ is se% %o F ome.a%aG H$i#$li#$%ed in yello@• An ins%ance o) %$e #everseStringService class is crea%ed H$i#$li#$%ed in #reen• ($e Cree%in#6lo@ is re#is%ered on an endpoin% @i%$ %$e BRI Fs%dio:==sys%em inG H$i#$li#$%ed

in %ur0uoise($e endpoin% providin# inpu% %o %$e )lo@ @ill %$us %a/e i%s inpu% )rom %$e ys%em in s%ream

Hcon%inued on nex% pa#e

83

Page 54: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 54/354

"on%inuin# @i%$ %$e example:• En%er some %ex% a% %$e promp% and press re%urn• 1u%pu% similar %o %$e )ollo@in# s$ould be #enera%ed on %$e console:

...Please enter text: SomeTextINFO 2011-03-17 17:40:01,324 [ConsoleInputConnector.dispatcher.1]org.mule.lifecycle.AbstractLifecycleManager: Initialising:'ConsoleInputConnector.dispatcher.599179652'. Object is: StdioMessageDispatcherINFO 2011-03-17 17:40:01,325 [ConsoleInputConnector.dispatcher.1]org.mule.lifecycle.AbstractLifecycleManager: Starting: 'ConsoleInputConnector.dispatcher.599179652'. Object is: StdioMessageDispatcherSTDIO connector sending string. txeTemoSPlease enter text:...

9o%e %$a%:• ($e s%rin# F (.I1 connec%or sendin# s%rin#G is ou%pu% on %$e console H$i#$li#$%ed in red ,

indica%in# %$a% %$e connec%or passes on a messa#e %o %$e nex% s%a#e in %$e )lo@• ($e reversed s%rin# I en%ered H$i#$li#$%ed in #reen is ou%pu% on %$e console

A#ain, con%inuin# @i%$ %$e example:• En%er more %ex% a% %$e promp% and press re%urn

ubse0uen% ou%pu% @ill only consis% o) %$e messa#e )rom %$e connec%or and %$e reverseds%rin# en%ered

• "on%inue %o en%er %ex% a% %$e promp% un%il a messa#e )rom %$e cus%om excep%ion $andler iss$o@n:

...Please enter text: MoreText*** MyMule3ExceptionHandler.exceptionThrown: Component that caused exception is:org.mule.component.DefaultJavaComponent component for:SimpleFlowConstruct{GreetingFlow}. Message payload is of type: String Listener property: SomeData

Please enter text:...

9o%e %$a%:• ($e cus%om excep%ion $andler is invo/ed and ou%pu%s a messa#e H$i#$li#$%ed in #reen•

($e value con%ained in %$e proper%y o) %$e cus%om excep%ion $andler is ou%pu% %o %$e consoleH$i#$li#$%ed in redAs expec%ed, i% con%ains %$e value @i%$ @$ic$ i% @as ini%ialized

($e example pro#ram @or/s as @e $oped i% @ould, @i%$ mul%iple con)i#ura%ion )iles, a cus%omexcep%ion $andler and a cus%omized connec%or

$en you are done inpu%%in# %ex%, %ermina%e %$e pro#ram

85

Page 55: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 55/354

e no@ #o on %o run %$e Mule 2 x version o) %$e example pro#ram:• "$an#e %$e Mule dis%ribu%ion o) %$e pro4ec%, as described in %$e appendix"rea%e a Mule

*ro4ec%As be)ore, %$is @ill resul% in a compila%ion error in %$e M$Mule)&xceptionHandler class bu%%$is s$ould be i#nored

Run %$e Fmule&con)i#2 xmlG con)i#ura%ion )ile by ri#$%&clic/in# i% and selec%in# Run As &LMule erverI#nore any @arnin#s abou% errors in %$e pro4ec% and proceed @i%$ launc$in# %$e pro#ram

• In %$e console, @e s$ould see resul% similar %o %$a% o) %$e Mule 3 x version, @i%$ somevaria%ions:

INFO 2011-03-17 18:22:20,555 [main] org.mule.MuleServer: Mule Server initializing......INFO 2011-03-17 18:22:23,792 [main] org.mule.DefaultExceptionStrategy: Initialisingexception listener: org.mule.DefaultExceptionStrategy@2825491d*** MyMule2ExceptionListener created*** Setting MyMule2ExceptionListener.listenerProperty: SomeData***** ReverseStringService instance created.INFO 2011-03-17 18:22:23,845 [main] org.mule.component.DefaultJavaComponent:Initialising: org.mule.component.DefaultJavaComponent component for: SedaService{null}...INFO 2011-03-17 18:22:23,886 [main] org.mule.MuleServer: Mule Server starting...*** MyMule2ExceptionListener created*** Setting MyMule2ExceptionListener.listenerProperty: SomeData***** ReverseStringService instance created.INFO 2011-03-17 18:22:23,910 [main] org.mule.transport.stdio.PromptStdioConnector:Connected: PromptStdioConnector{this=578b1f8f, started=false, initialised=true,name='SystemStreamConnector', disposed=false, numberOfConcurrentTransactedReceivers=4,createMultipleTransactedReceivers=true, connected=true, supportedProtocols=[stdio],serviceOverrides=null}...INFO 2011-03-17 18:22:24,007 [main] org.mule.util.queue.TransactionalQueueManager:Started ResourceManagerINFO 2011-03-17 18:22:24,056 [main] org.mule.DefaultMuleContext:**********************************************************************

* Mule ESB and Integration Platform ** Version: 2.2.1 Build: 14422 *...* Agents Running: None ***********************************************************************

Please enter text: SomeTextINFO 2011-03-17 18:22:42,411 [SystemStreamConnector.dispatcher.1]org.mule.transport.stdio.StdioMessageDispatcher: Connected:endpoint.outbound.stdio://system.outSTDIO connector sending string. txeTemoSPlease enter text: MoreTextINFO 2011-03-17 18:22:50,599 [SystemStreamConnector.dispatcher.2]org.mule.transport.stdio.StdioMessageDispatcher: Connected:endpoint.outbound.stdio://system.outSTDIO connector sending string. txeTeroMPlease enter text: MoreText*** MyMule2ExceptionListener.exceptionThrown: Component that caused exception is:SedaService{echoServiceWithReverse}. Message payload is of type: String Listener property: SomeData

Please enter text:

($is concludes %$e example s$o@in#, amon# o%$er %$in#s, $o@ %o modularize Mule con)i#ura%ion)iles and $o@ %o cus%omize excep%ion $andlin#

88

Page 56: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 56/354

,. )nsert File *ata into a *atabaseIn %$is c$ap%er @e @ill loo/ a% an example s$o@in# $o@ %o inser% da%a )rom M- )iles in%o a %able ina da%abase usin# Mule e @ill also loo/ very brie)ly a% messa#e rou%in#Earlier examples $ave $ad dis%inc%ly di))eren% Mule 2 x and Mule 3 x con)i#ura%ion )ilesJ usuallyusin# a model @i%$ Mule 2 x and a )lo@ @i%$ Mule 3 x In %$is c$ap%er @e @ill use con)i#ura%ion)iles %$a% are iden%ical, excep% )or %$e M- namespace declara%ions %o s$o@ %$a% i% is also possible %ocon)i#ure Mule 3 x in a manner iden%ical %o %$a% used @i%$ Mule 2 x

A #rap$ical represen%a%ion o) %$is c$ap%er's example pro#ram may loo/ li/e %$is:

($is c$ap%er's example pro#ram consis%s o) one inbound endpoin%, a rou%er and %$ree ou%bound endpoin%s

A#ain, @e @ill use %$e %ec$ni0ue o) runnin# %$e Mule con)i#ura%ion )iles, described in %$e previousc$ap%er, so @e @ill no% need a s%ar%er class

($e example pro#ram @ill u%ilize a *os%#re S- da%abase @i%$ %$e de)aul% accoun% Fpos%#resG @i%$%$e pass@ord FadminadminG Hpass@ord c$osen durin# ins%alla%ion %$a% $as all privile#es Dou are)ree %o use any da%abase se%up you desire, bu% mus% adap% %$e example accordin#lyI) you $aven'% already, do@nload and ins%all %$e la%es% version o) %$e *os%#re S- da%abase )rom$%%p:==@@@ pos%#res0l or#=

e @ill use %$e Eclipse .a%a ource Explorer %o examine da%a in %$e da%abase "on)i#ura%ion and basic use o) %$e .a%a ource Explorer is described in %$e appendix .a%abase Access )rom @i%$inEclipse

87

Page 57: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 57/354

' 1 Create the Pro!ect"rea%e %$e pro4ec% as described in %$e appendix"rea%e a Mule *ro4ec%, namin# i%FMule6ile(o.a%abaseG ($e Mule 3 $o% deploymen% can be s@i%c$ed o)) )rom %$e s%ar%, as %$is)ea%ure is no% o) use @$en runnin# Mule embedded In addi%ion %o %$e basic pro4ec% se%up, @e @ill

per)orm some addi%ional se%up )or %$is par%icular pro4ec%:• In %$e pro4ec% roo%, crea%e a direc%ory named F)ile&inpu%&direc%oryG

($is is %$e direc%ory in @$ic$ )iles are %o be placed @$en %$eir con%en%s is %o be inser%ed in%o%$e da%abase

• In %$e pro4ec% roo%, crea%e ano%$er direc%ory and name i% F)ile&ou%pu%&direc%oryG($is is %$e direc%ory %o @$ic$ @e @ill ins%ruc% Mule %o copy )iles %$a% $as been processed

• Enable Maven dependency mana#emen% )or %$e ne@ pro4ec% accordin# %o ins%ruc%ions in %$eappendix Enablin# Maven .ependency Mana#emen% )or an Eclipse *ro4ec%

Maven dependency mana#emen% @ill be used %o include some addi%ional libraries needed )or %$eexample pro#ram

8

Page 58: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 58/354

' " -// Depen/encies($e Maven pom xml )ile is used %o mana#e %$e addi%ional dependencies o) %$e pro4ec% ($e Mulelibraries @ill s%ill be included on %$e pro4ec%'s build pa%$ in %$e re#ular )as$ion in order %o enable us%o easily s@i%c$ be%@een usin# %$e Mule 2 x run%ime and %$e Mule 3 x run%ime

A pom xml )ile s$ould already $ave been crea%ed @$en @e enabled Maven dependencymana#emen% )or %$e pro4ec% e 4us% need %o add dependencies %o %$e libraries @e need• 1pen %$e pom xml )ile• Add %$e dependencies %o %$e )ile so %$a% i% loo/s li/e %$is:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion> <groupId>com.ivan.mule</groupId> <artifactId>MuleFileToDatabase</artifactId> <version>0.0.1-SNAPSHOT</version>

<dependencies> <!-- Includes the Spring framework's JDBC module. --> <dependency> <groupId>org.springframework</groupId> <artifactId>org.springframework.jdbc</artifactId> <version>3.0.2.RELEASE</version> </dependency> <!-- Includes the Apache Commons data source implementation. --> <dependency>

<groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> <type>jar</type> <scope>compile</scope> </dependency> <!-- Includes the PostgreSQL JDBC connector. Change this if you use another database. --> <dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.0-801.jdbc4</version> </dependency> </dependencies></project>

9o%e %$a%:• I) you @an% %o use ano%$er da%abase, you need %o replace %$e *os%#re S- ?.>" connec%or

library @i%$ %$e appropria%e ?.>" connec%or library )or %$e da%abase in 0ues%ion

8;

Page 59: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 59/354

' & Database Table Creation ScriptIn order )or Mule %o be able %o s%ore da%a in%o a da%abase, %$ere mus% be a sc$ema and a %able in@$ic$ %o inser% %$e da%a In %$is sec%ion @e'll @ri%e and execu%e %$e S- scrip% %$a% crea%es %$eda%abase sc$ema and %able used by our example pro#ram

In %$e 6ile menu, selec% 9e@ &L 1%$er• In %$e @izard dialo#, selec% S- 6ile and clic/ 9ex%

elec%in# %$e S- 6ile @izard in Eclipse

Hcon%inued on nex% pa#e

8+

Page 60: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 60/354

• In %$e "rea%e S- 6ile dialo#, selec% %$e Mule6ile(o.a%abase pro4ec% as %$e paren% )older

($e "rea%e S- 6ile @izard in Eclipse

• %ill in %$e "rea%e S- 6ile dialo#, en%er Fcrea%e&)iles%ore&db s0lG as name o) %$e S- )ile• Veri)y %$e da%abase server %ype, connec%ion pro)ile name and da%abase name• "lic/ 6inis$

7

Page 61: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 61/354

• In %$e ne@ S- )ile @indo@, en%er %$e )ollo@in# S- s%a%emen%s:

DROP SCHEMA IF EXISTS mule CASCADE;CREATE SCHEMA mule;CREATE TABLE mule.filestore (id SERIAL UNIQUE,message text NOT NULL,time_stamp timestampNOT NULL,PRIMARY KEY (id));

• Ri#$%&clic/ in %$e S- )ile @indo@ and selec% Execu%e All

Execu%in# %$e S- scrip% in Eclipse

• In %$e S- Resul%s vie@ %$a% is opened, veri)y %$e success o) %$e S- scrip% execu%ion

($e resul% o) execu%in# %$e S- scrip% s$o@ in Eclipse

• Veri)y %$a% %$e F)iles%oreG %able in %$e FmuleG da%abase is emp%yBse %$e procedure described in %$e .a%a Access sec%ion o) appendix E

9o%e %$a%:• In %$e par% o) %$e scrip% %$a% crea%es %$e %able in %$e da%abase, %$e id column is speci)ied as

$avin# %$e %ype ERIA-($e ERIA- %ype is *os%#re S-'s @ay o) de)inin# an uni0ue iden%i)ier column %$a% is o) %$e%ype in%e#er and @$ic$ de)aul% values @ill be assi#ned )rom a se0uence #enera%or*lease re)er %o %$e *os%#re S- documen%a%ion )or de%ails

7!

Page 62: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 62/354

' ' Create the Spring ean Configuration )ile>o%$ versions o) %$e example pro#ram uses one and %$e same prin# bean con)i#ura%ion )ile %ocon)i#ure a prin# bean %$a% ac%s as a da%a&source ($is da%a&source @ill be used by Mule @$eninser%in# da%a in%o %$e da%abase

In %$e roo% o) %$e pro4ec% source direc%ory, crea%e a )ile named Fsprin#&con)i# xmlG @i%$ %$e)ollo@in# con%en%s:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- Data source Spring bean used by both the Mule 2.x and Mule 3.x versions of the example. Should be changed if another database is used etc.

Note that before being able to run the example, the database must be set up using the enclosed script. --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="org.postgresql.Driver" p:url="jdbc:postgresql://localhost:5432/postgres" p:username="postgres" p:password="adminadmin"/></beans>

9o%e %$a%:• ($e p namespace is used %o speci)y %$e da%abase proper%ies

*lease re)er %o %$e prin# re)erence documen%a%ion )or de%ails• ($e username and pass@ord are, as be)ore, Fpos%#resG and FadminadminG respec%ively

72

Page 63: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 63/354

' * Create the (ule Configuration )iles($e Mule con)i#ura%ion o) %$is c$ap%er's example pro#ram is spli% in%o %$ree di))eren% )ilesJ one )ilecon%ainin# %$e )ile connec%or de)ini%ion, one )ile con%ainin# %$e ?.>" connec%or de)ini%ion and one)ile con%ainin# %$e main Mule model=)lo@

Create the Mule File Connector Configuration Files

($e Mule )ile connec%or speci)ies $o@ )iles s$ould be read or @ri%%en )rom or %o a direc%ory in %$elocal )ile sys%emIn %$is example, @e speci)y $o@ )iles in %$e inpu% direc%ory are %o be $andled

($e only di))erence be%@een %$e Mule 2 x and Mule 3 x )ile connec%or con)i#ura%ion )iles are %$enamespaces >o%$ are loca%ed in %$e roo% o) %$e pro4ec%'s source direc%ory($e Mule 2 x con)i#ura%ion )ile is named Fmule2&)ileconnec%or xmlG:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:file="http://www.mulesource.org/schema/mule/file/2.2" xsi:schemaLocation=" http://www.mulesource.org/schema/mule/file/2.2 http://www.mulesource.org/schema/mule/file/2.2/mule-file.xsd

http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd">

<!-- File connector specifying: - How file content should be passed along. The streaming attribute specifies whether a stream (value true) or the contents of the file (value false) is to be passed around as the incoming message. - How often the input directory should be checked for new files. The pollingFrequencey attribute specifies the interval in

milliseconds between checks for files in the input directory. The default setting is that files in the input directory,

once read, should be deleted. --> <file:connector name="fileConnector" streaming="false" pollingFrequency="1000"> <!-- The expression-filename-parser creates a filename for arrived messages. The filename is placed in a header passed along with the message which will be used later when writing the file contents to an output file. --> <file:expression-filename-parser/> </file:connector></mule>

73

Page 64: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 64/354

($e Mule 3 x )ile connec%or con)i#ura%ion )ile is named Fmule3&)ileconnec%or xmlG:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:file="http://www.mulesoft.org/schema/mule/file" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/3.2/mule-file.xsd

http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd">

<!-- File connector specifying: - How file content should be passed along. The streaming attribute specifies whether a stream (value true) or the contents of the file (value false) is to be passed around as the incoming message. - How often the input directory should be checked for new files. The pollingFrequencey attribute specifies the interval in milliseconds between checks for files in the input directory.

The default setting is that files in the input directory, once read, should be deleted. --> <file:connector name="fileConnector" streaming="false" pollingFrequency="1000"> <!-- The expression-filename-parser creates a filename for arrived messages. The filename is placed in a header passed along with the message which will be used later when writing the file contents to an output file. --> <file:expression-filename-parser/> </file:connector></mule>

9o%e %$a%:• ($e streaming a%%ribu%e o) %$e )ile:connec%orL elemen% speci)ies @$e%$er a s%ream )rom

@$ic$ a )ile can be read H%rue or @$e%$er %$e en%ire con%en%s o) a )ile H)alse is %o be passedaround @i%$ a messa#e

• ($e polling,re uenc$ a%%ribu%e o) %$e )ile:connec%orL elemen% speci)ies %$e $o@ )re0uen%lyan inpu% direc%ory is %o be c$ec/ed )or ne@ )iles (ime is in milliseconds

• ($e )ile:expression&)ilename&parserL elemen% in %$e )ile:connec%orL elemen% associa%es a)ilename parser @i%$ %$e )ile connec%orIn %$is example, %$e parser @ill ex%rac% %$e name o) an incomin# )ile and place i% in a $eaderin %$e messa#e associa%ed @i%$ %$e incomin# )ile($e parser can also be used %o cons%ruc% %$e name o) a )ile %o be @ri%%en

As per de)aul%, incomin# )iles are dele%ed a)%er $avin# been read($is be$aviour can be c$an#ed usin# %$e auto+elete a%%ribu%e o) %$e )ile:connec%orLelemen%

75

Page 65: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 65/354

Create the Mule -* C Connector Configuration Files

($e ?.>" connec%or indirec%ly speci)ies @$ic$ da%abase %o be used by re)erencin# %$e da%a source bean de)ined in %$e prin# con)i#ura%ion )ile earlier A ?.>" connec%or con%ain one or more S- 0ueries used %o read )rom or @ri%e %o a da%abase In%$is example, %$ere is one sin#le 0uery used %o @ri%e )ile da%a %o %$e da%abase %able de)ined in %$eda%abase scrip% @e @ro%eearlier A#ain, %$e Mule con)i#ura%ion )iles )or %$e di))eren% versions o) Mule are iden%ical, excep% )or %$enamespaces >o%$ con)i#ura%ion )iles are loca%ed in %$e roo% o) %$e pro4ec%'s source direc%ory

($e Mule 2 x con)i#ura%ion )ile is named Fmule2&4dbcconnec%or xmlG:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.mulesource.org/schema/mule/jdbc/2.2" xsi:schemaLocation=" http://www.mulesource.org/schema/mule/jdbc/2.2 http://www.mulesource.org/schema/mule/jdbc/2.2/mule-jdbc.xsd

http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd">

<!-- JDBC connector specifying which data source to use when interacting with the database. The data source is a Spring bean. A JDBC connector contain one or more queries. --> <jdbc:connector name="jdbcConnector" dataSource-ref="dataSource"> <!-- A JDBC query is a database query that can be used by, for example, an inbound or outbound endpoint to read data from or write data to a database.

This query inserts the payload of a message received along with a timestamp into the table FILESTORE. --> <jdbc:query key="fileInsert" value="INSERT INTO mule.filestore VALUES(DEFAULT,#[payload:java.lang.String],CURRENT_TIMESTAMP)"/> </jdbc:connector></mule>

Hcon%inued on nex% pa#e

78

Page 66: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 66/354

($e Mule 3 x con)i#ura%ion )ile is named Fmule3&4dbcconnec%or xmlG:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.mulesoft.org/schema/mule/jdbc" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/jdbc http://www.mulesoft.org/schema/mule/jdbc/3.2/mule-jdbc.xsd

http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd">

<!-- JDBC connector specifying which data source to use when interacting with the database. The data source is a Spring bean. A JDBC connector contain one or more queries. --> <jdbc:connector name="jdbcConnector" dataSource-ref="dataSource"> <!-- A JDBC query is a database query that can be used by, for example, an inbound or outbound endpoint to read data from or write data to a database.

This query inserts the payload of a message received along with a timestamp into the table FILESTORE. --> <jdbc:query key="fileInsert" value="INSERT INTO mule.filestore VALUES(DEFAULT,#[payload:java.lang.String],CURRENT_TIMESTAMP)"/> </jdbc:connector></mule>

9o%e %$a%:• $ic$ da%a&source %o be used by %$e ?.>" connec%or is speci)ied usin# %$e dataSource-ref

a%%ribu%e o) %$e 4dbc:connec%orL elemen%($e name re)ers %o %$e prin# bean Fda%a ourceG de)ined in %$e prin# con)i#ura%ion )ile in%$e previous sec%ion

• ($e 4dbc:connec%orL elemen% con%ains a 4dbc:0ueryL elemen%A 4dbc:connec%orL elemen% con%ain one or more 0ueries, eac$ $avin# a name speci)ied by%$e e$ a%%ribu%e o) %$e 4dbc:0ueryL elemen%

• ($e 0uery speci)ied by %$e value a%%ribu%e o) %$e 4dbc:0ueryL elemen% con%ains %$e s%rin#F.E6AB-(GBsin# *os%#re S-, %$is @ill cause %$e nex% de)aul% value %o be inser%ed in %$e correspondin#column in %$e da%abase %able

• ($e 0uery speci)ied by %$e value a%%ribu%e o) %$e 4dbc:0ueryL elemen% con%ains %$e s%rin#

FTUpayload:4ava lan# %rin# G($is s%rin# an expression %$a% speci)ies %$a% %$e messa#e payload s$ould be inser%ed as a ?ava

%rin# in%o %$e 0uery($e FTUF is re)erred %o as %$e place$older pre)ix

• ($e 0uery speci)ied by %$e value a%%ribu%e o) %$e 4dbc:0ueryL elemen% con%ains %$e s%rin#F"BRRE9(P(IME (AM*GBsin# *os%#re S-, %$is @ill cause %$e da%e and %ime o) %$e s%ar% o) %$e curren% %ransac%ion %o

be inser%ed in%o %$e correspondin# column in %$e da%abase %able

77

Page 67: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 67/354

Create the Main Mule Configuration Files

($e main Mule con)i#ura%ion )iles are responsible )or pu%%in# %$e )ile processin# model %o#e%$erusin# %$e connec%ors and prin# con)i#ura%ion crea%ed earlier in %$is c$ap%er As in %$e in%roduc%iono) %$is c$ap%er, @e @ill delibera%ely use a model in bo%$ %$e con)i#ura%ion )iles %o s$o@ %$a% Mule2 x con)i#ura%ion can, @i%$ minimal modi)ica%ions, be run on Mule 3 x as @ell($ese Mule con)i#ura%ion )iles are also loca%ed in %$e roo% o) %$e source direc%ory o) %$e pro4ec%($e Mule 2 x con)i#ura%ion )ile is named Fmule&con)i#2 xmlG:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:file="http://www.mulesource.org/schema/mule/file/2.2" xmlns:jdbc="http://www.mulesource.org/schema/mule/jdbc/2.2" xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.2" xmlns:spring="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.mulesource.org/schema/mule/file/2.2 http://www.mulesource.org/schema/mule/file/2.2/mule-file.xsd

http://www.mulesource.org/schema/mule/jdbc/2.2 http://www.mulesource.org/schema/mule/jdbc/2.2/mule-jdbc.xsd

http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd

http://www.mulesource.org/schema/mule/stdio/2.2 http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<spring:beans> <spring:import resource="spring-config.xml"/> <spring:import resource="mule2-fileconnector.xml"/> <spring:import resource="mule2-jdbcconnector.xml"/> </spring:beans>

<model name="fileProcessingModel"> <service name="fileToDBService"> <inbound> <!-- Input endpoint receiving files in a directory, applying filtering by filename. --> <file:inbound-endpoint connector-ref="fileConnector" path="./file-input-directory"> <!-- Only process files with the .xml suffix. --> <file:filename-wildcard-filter pattern="*.xml"/> </file:inbound-endpoint> </inbound>

<outbound> <!-- The multicast router sends the received message to all contained endpoints. In this example, the contents of the file dropped in the input-directory will be written to a file in the output-director as well as printed to the console. --> <multicasting-router> <!-- Endpoint writing message to file. The name of the output file contains the name of the input file. The name of the file written contains a timestamp showing the time the file was written.

The timestamp is obtained from the function "dateStamp" and formatted according to the string that follows.

7

Page 68: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 68/354

--> <file:outbound-endpoint path="./file-output-directory" outputPattern= "processed-#[header:originalFilename]-#[function:dateStamp:yyyy-MM-dd_hh:mm:ss]"/>

<!-- Endpoint printing message to the console.

--> <stdio:outbound-endpoint system="OUT"/>

<!-- Endpoint inserting messages into database using the "fileInsert" query in the JDBC connector "jdbcConnector". --> <jdbc:outbound-endpoint connector-ref="jdbcConnector"queryKey="fileInsert"/> </multicasting-router> </outbound> </service> </model></mule>

($e Mule 3 x con)i#ura%ion )ile is named Fmule&con)i#3 xmlG:<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:jdbc="http://www.mulesoft.org/schema/mule/jdbc" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:stdio="http://www.mulesoft.org/schema/mule/stdio" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/3.2/mule-file.xsd

http://www.mulesoft.org/schema/mule/jdbc http://www.mulesoft.org/schema/mule/jdbc/3.2/mule-jdbc.xsd

http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd

http://www.mulesoft.org/schema/mule/stdio http://www.mulesoft.org/schema/mule/stdio/3.2/mule-stdio.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<spring:beans> <spring:import resource="spring-config.xml"/> <spring:import resource="mule3-fileconnector.xml"/> <spring:import resource="mule3-jdbcconnector.xml"/> </spring:beans>

<model name="fileProcessingModel">

<service name="fileToDBService"> <inbound> <!-- Input endpoint receiving files in a directory, applying filtering by filename. --> <file:inbound-endpoint connector-ref="fileConnector" path="./file-input-directory"> <!-- Only process files with the .xml suffix. --> <file:filename-wildcard-filter pattern="*.xml"/> </file:inbound-endpoint> </inbound>

<outbound> <!--

7;

Page 69: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 69/354

The multicast router sends the received message to all contained endpoints. In this example, the contents of the file dropped in the input-directory will be written to a file in the output-director as well as printed to the console. --> <multicasting-router> <!-- Endpoint writing message to file.

The name of the output file contains the name of the input file. The name of the file written contains a timestamp showing the time the file was written. The timestamp is obtained from the function "dateStamp" and formatted according to the string that follows. --> <file:outbound-endpoint path="./file-output-directory" outputPattern= "processed-#[header:originalFilename]-#[function:dateStamp:yyyy-MM-dd_hh:mm:ss]"/>

<!-- Endpoint printing message to the console. --> <stdio:outbound-endpoint system="OUT"/>

<!-- Endpoint inserting messages into database using the "fileInsert" query in the JDBC connector "jdbcConnector". --> <jdbc:outbound-endpoint connector-ref="jdbcConnector"queryKey="fileInsert"/> </multicasting-router> </outbound> </service> </model></mule>

9o%e %$a%:• prin# impor%L elemen%s inside a prin# beansL elemen% are used %o impor% %$e prin#

bean con)i#ura%ion )ile as @ell as %$e o%$er %@o con)i#ura%ion )iles• ($e modelL elemen% con%ains one sin#le serviceL elemen%J %$e )ile(o.> ervice• ($e serviceL elemen% con%ain one inboundL and one ou%boundL elemen%, bo%$ @$ic$ are

op%ional($e inboundL elemen% speci)ies $o@ messa#es are received by %$e service and %$e

ou%boundL elemen% speci)ies $o@ messa#es )rom %$e service are sen% ou%In older versions o) Mule, a brid#e service componen% $ad %o be con)i#ured explici%ly %oconnec% %$e inbound par% o) a service %o %$e ou%bound par% %ar%in# @i%$ Mule 2 , %$ere is a

de)aul% pass&%$rou#$ componen% inser%ed i) no%$in# else is con)i#ured• ($e )ile(o.> ervice service con%ain one sin#le inbound endpoin%

($is is a )ile inbound endpoin% speci)ied in a )ile:inbound&endpoin%L elemen%($e inbound endpoin% uses %$e connector-ref a%%ribu%e %o speci)y %$a% i% uses %$e )ileconnec%or @i%$ %$e name )ile"onnec%or @e de)ined earlier In addi%ion, %$e endpoin% speci)ies@$ic$ direc%ory %o moni%or usin# %$e pa%$ a%%ribu%e o) %$e )ile:inbound&endpoin%L elemen%

• ($e )ile:inbound&endpoin%L elemen% con%ains a )ile:)ilename&@ildcard&)il%erL elemen%($e pattern a%%ribu%e o) %$is elemen% speci)y a pa%%ern %$a% names o) )iles in %$e inpu%direc%ory o) %$e )ile inbound endpoin% direc%ory mus% ma%c$ in order %o be accep%ed as inpu%%o %$e endpoin% Mul%iple pa%%erns can be supplied in a comma&separa%ed lis%

• ($e serviceL elemen% con%ains an ou%boundL elemen% a)%er %$e inboundL elemen%

7+

Page 70: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 70/354

A#ain, %$is elemen% speci)ies $o@ messa#es )rom %$e service are passed on a)%er any processin# %$e service per)orms

• ($e ou%boundL elemen% con%ains a mul%icas%in#&rou%erL elemen%($is elemen% sends messa#es %o all ou%bound endpoin%s con%ained in %$e mul%icas%in#&rou%erL elemen% Endpoin%s in %$e rou%er can use %$e s$nchronous a%%ribu%e %o indica%e@$e%$er %$ey are sync$ronous or no% I) all endpoin%s in %$e mul%icas%in# rou%er areasync$ronous, a messa#e @ill be sen% %o all endpoin%s a% %$e same %ime 1%$er@ise a messa#e@ill be sen% %o %$e con%ained endpoin%s in %$e order %$e are lis%ed

• ($e )irs% endpoin% in %$e mul%icas%in# rou%er is de)ined by %$e )ile:ou%bound&endpoin%Lelemen%($is endpoin% @ri%es messa#es %o %$e direc%ory speci)ied by %$e path a%%ribu%e and names)iles accordin# %o %$e pa%%ern speci)ied by %$eoutputPattern a%%ribu%e

• ($e outputPattern a%%ribu%e o) %$e )ile:ou%bound&endpoin%L elemen% con%ains %$e )ollo@in#s%rin#: processed&TU$eader:ori#inal6ilename &TU)unc%ion:da%e %amp:yyyy&MM&ddP$$:mm:ss

($e FTUF are, as be)ore, place$older pre)ixes($e F$eader:ori#inal6ilenameG indica%es %$a% %$e da%a %o be inser%ed in %$e s%rin# is %o be%a/en )rom %$e messa#e $eader Fori#inal6ilenameG($e F)unc%ion:da%e %amp:yyyy&MM&dd $$:mm:ssG execu%es %$e )unc%ion Fda%e %ampG andou%pu%s %$e da%e&%ime in)orma%ion %o a s%rin# in %$e speci)ied )orma%*lease consul% %$e Expression Evalua%or Re)erence sec%ion in %$e Mule Bser Cuide )oraddi%ional in)orma%ion

• ($e s%dio:ou%bound&endpoin%L elemen% @ill, as seen in previous examples, lo# %$e messa#e%o %$e s%andard I=1 console

• ($e 4dbc:ou%bound&endpoin%L elemen% de)ines %$e ?.>" ou%bound endpoin% %$a% inser%s %$e

messa#e in%o %$e da%abaseAs @i%$ o%$er %ypes o) endpoin%s, %$econnector-ref a%%ribu%e is used %o speci)y @$ic$connec%or %$e endpoin% is %o use In %$is example, %$e 4dbc"onnec%or is %$e da%abaseconnec%or de)ined earlier in a separa%e )ile($e uer$/e$ a%%ribu%e is used %o decide @$ic$ da%abase 0uery in %$e connec%or %$a% is %o beexecu%ed @$en a messa#e arrives %o %$e endpoin%

($is concludes %$e developmen% o) %$is c$ap%er's example pro#ram e are no@ ready %o %ry i%

Page 71: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 71/354

' , +un the Example ProgramIn %$is sec%ion @e @ill %ry %$e example pro#ram by s%ar%in# Mule, droppin# an M-&)ile in%o %$einpu% direc%ory and %$en loo/ a% %$e console, %$e da%abase and %$e ou%pu% direc%ory %o veri)y %$a% %$econ%en%s o) %$e )ile indeed @as processed as expec%ed

$en runnin# %$e example pro#ram, @e'll use %$e %ec$ni0ue o) runnin# %$e Mule con)i#ura%ion )ilesas described in %$e earlier example

I% is assumed %$a% %$e pro4ec% is con)i#ured @i%$ %$e Mule 3 x dis%ribu%ion on %$e classpa%$ @$ens%ar%in# %$is sec%ion

• Ma/e sure %$a% %$e da%abase server is runnin#Bsually %$e server s%ar%s au%oma%ically @$en %$e compu%er is s%ar%ed, bu% i) %$ere are

problems one mus% ma/e sure %$a% %$e server is runnin# and %$a% i% is possible %o connec% %o i%usin# %$e creden%ials used in %$e Mule ?.>" connec%or

• 6rom @i%$in Eclipse, connec% %o %$e da%a&source as described insec%ion 2 o) appendix E• In %$e *ac/a#e or *ro4ec% Explorer, ri#$%&clic/ %$e Fmule&con)i#3 xmlG )ile and selec% Run

As &L Mule erver• ($e Mule 3 x server s$ould s%ar% up and display console ou%pu% indica%in# a success)ul

s%ar%up• Re)res$ %$e FMule6ile(o.a%abaseG pro4ec% in Eclipse

9o%e %$a% %$ere is one ne@ direc%ories %$a% @e did no% crea%e ourselves I% is named F)ile&inpu%&direc%oryG ($is direc%ory @as crea%ed by Mule )or %$e inbound )ile endpoin%

• In Eclipse, copy any o) %$e M- )iles in %$e pro4ec% and pas%e i% in%o %$e F)ile&inpu%&direc%oryG I used %$e prin# con)i#ura%ion )ile

• ($e con%en%s o) %$e M- )ile s$ould be displayed on %$e console alon# @i%$ a number o) lo#messa#es )rom Mule($e las% %@o lo# messa#es %ells us %$a% %$e messa#e, %$a% is %$e con%en%s o) %$e M- )ile, @as@ri%%en %o a )ile in %$e ou%pu% direc%ory and success)ully inser%ed in%o %$e da%abase H%$e pa%$%o %$e ou%pu% )ile $as been edi%ed )or brevi%y :

... <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="org.postgresql.Driver" p:url="jdbc:postgresql://localhost:5432/postgres" p:username="postgres" p:password="adminadmin"/>

</beans>INFO 2011-07-13 18:02:22,977 [fileConnector.dispatcher.1]org.mule.transport.file.FileConnector: Writing file to: ./file-output-directory/processedspring-config.xml-2011-02-13_06:02:22INFO 2011-07-13 18:02:23,218 [jdbcConnector.dispatcher.1]org.mule.transport.jdbc.sqlstrategy.SimpleUpdateSqlStatementStrategy: Executing SQLstatement: 1 row(s) updated

!

Page 72: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 72/354

• In Eclipse, re)res$ %$e pro4ec% by clic/in# on i% and pressin# 68Al%erna%ively, ri#$%&clic/ on %$e pro4ec% and selec% Re)res$ in %$e menu %$a% appears($e inpu% direc%ory s$ould be emp%y and a ne@ direc%ory named F)ile&ou%pu%&direc%oryG, %$eou%pu% direc%ory, s$ould $ave appeared:

Inpu% and ou%pu% direc%ories in %$e pro4ec% a)%er Mule $avin# processed an M- )ile

• Vie@ %$e con%en%s o) %$e F)iles%oreG da%abase %able in %$e FmuleG da%abase

($e process )or vie@in# a da%abase %able in Eclipse is described in %$e second sec%ion o)appendix E , bu% you are )ree %o use %$e %ool o) your c$oice($ere s$ould be one ro@ in %$e %able @i%$ %$e con%en%s o) %$e M- )ile in %$e Fmessa#eGcolumn:

.a%abase %able F)iles%oreG a)%er Mule $avin# processed a M- )ile pas%ed in %$e inpu% direc%ory

In %$is example, @e $ave seen %$a% @$en @e drop a )ile in %$e desi#na%ed inpu% direc%ory, Mule pic/si% up and sends %$e con%en%s o) %$e )ile %o %$e console, an ou%pu% direc%ory and a da%abase %able, allaccordin# %o our con)i#ura%ion

Run %$e Mule 2 x version o) %$e example pro#ram:• "$an#e %$e Mule dis%ribu%ion %o Mule 2 x, as described inappendix >• %ar% %$e example pro#ram by ri#$%&clic/in# %$e Fmule&con)i#2 xmlG )ile and selec%in# Run

As&LMule erver

($e resul% o) runnin# %$e Mule 2 x version s$ould be similar %o @$a% @e've already seen

2

Page 73: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 73/354

' 2 (ule & x 3ersion 4ith )lo4As a sor% o) appendix %o %$is c$ap%er, @e @ill %a/e a brie) loo/ a% a Mule 3 x version o) %$is c$ap%er'sexample pro#ram %$a% uses a )lo@ ($is con)i#ura%ion )ile impor%s %$e same %$ree con)i#ura%ion )ilesas %$e Mule 3 x con)i#ura%ion )ile usedearlier

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:jdbc="http://www.mulesoft.org/schema/mule/jdbc" xmlns:stdio="http://www.mulesoft.org/schema/mule/stdio" xmlns:spring="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/3.2/mule-file.xsd

http://www.mulesoft.org/schema/mule/jdbc http://www.mulesoft.org/schema/mule/jdbc/3.2/mule-jdbc.xsd

http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.1/mule.xsd

http://www.mulesoft.org/schema/mule/stdio http://www.mulesoft.org/schema/mule/stdio/current/mule-stdio.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<spring:beans> <spring:import resource="spring-config.xml"/> <spring:import resource="mule3-fileconnector.xml"/> <spring:import resource="mule3-jdbcconnector.xml"/> </spring:beans>

<!-- Note that the flow does not contain any routers. A flow consists of a message source and a number of message processors.

Outbound endpoints are also message processors. --> <flow name="fileToDatabase"> <!-- Input endpoint receiving files in a directory, applying filtering by filename. --> <file:inbound-endpoint connector-ref="fileConnector" path="./file-input-directory"> <!-- Only process files with the .xml suffix. --> <file:filename-wildcard-filter pattern="*.xml"/> </file:inbound-endpoint>

<!-- Endpoint writing message to file. The name of the output file contains the name of the input file. The name of the file written contains a timestamp showing the time the file was written. The timestamp is obtained from the function "dateStamp" and formatted according to the string that follows. --> <file:outbound-endpoint path="./file-output-directory" outputPattern= "processed-#[header:originalFilename]-#[function:dateStamp:yyyy-MM-dd_hh:mm:ss]"/>

<!-- Endpoint printing message to the console.

--> <stdio:outbound-endpoint system="OUT"/>

3

Page 74: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 74/354

<!--

Endpoint inserting messages into database using the "fileInsert" query in the JDBC connector "jdbcConnector". --> <jdbc:outbound-endpoint connector-ref="jdbcConnector" queryKey="fileInsert"/> </flow></mule>

ome %$in#s %o no%e are:• ($e componen%s in %$e )lo@ can be )ound in %$e previous version o) %$e con)i#ura%ion )ile

"rea%in# %$e above con)i#ura%ion )ile @as ac%ually accomplis$ed by copyin# par%s o) %$e previous con)i#ura%ion )ile, @i%$ no c$an#es %o %$e con)i#ura%ion o) %$e individualcomponen%s

• ($ere are no rou%ers in %$e )lo@A )lo@ consis%s o) a messa#e source and a number o) messa#e processors 1u%boundendpoin%s are also a /ind o) messa#e processors

($is concludes %$is c$ap%er's example pro#ram

5

Page 75: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 75/354

/. alidate 0M$ *ataIn %$is c$ap%er @e @ill see an example s$o@in# $o@ %o valida%e incomin# da%a a#ains% one or more

M- sc$emas e'll see %ec$ni0ues s$o@in# $o@ %o @or/ around limi%a%ions in %$e erces M- parser %$a% is used by Mule )or M- valida%ion e @ill also see $o@ %o rou%e messa#es di))eren%ly,dependin# on @$e%$er %$ey $ave passed valida%ion or no%

($e s%ruc%ure o) %$is c$ap%er's Mule con)i#ura%ion loo/s li/e %$is:

%ruc%ure o) %$is c$ap%er's example pro#ram

($ere @ill be si#ni)ican% di))erences be%@een %$e Mule 2 x and 3 x versions o) %$e con)i#ura%ion)iles so %$e above s%ruc%ure does no% necessarily map very closely %o %$e Mule con)i#ura%ions

e @ill use %$e %ec$ni0ue o) runnin# %$e Mule con)i#ura%ion )iles, described in %$e previousc$ap%ers, so @e @ill no% need a s%ar%er class

8

Page 76: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 76/354

* 1 Create the Pro!ect"rea%e %$e pro4ec% as described in %$e appendix"rea%e a Mule *ro4ec%, namin# i%FMule M- c$emaValida%ionG ($e Mule 3 $o% deploymen% can be s@i%c$ed o)) )rom %$e s%ar%, as%$is )ea%ure is no% o) use @$en runnin# Mule embedded In addi%ion %o %$e basic pro4ec% se%up, @e@ill per)orm some addi%ional se%up )or %$is par%icular pro4ec%:

• In %$e pro4ec% roo%, crea%e a direc%ory named F)ile&inpu%&direc%oryG($is is %$e direc%ory in @$ic$ )iles are %o be placed @$en %$e are %o be processed by Mule

• A#ain in %$e pro4ec% roo%, crea%e a direc%ory named Fxml&da%aG($is direc%ory @ill con%ain %$e M- example )iles %$a% @e are #oin# %o send in%o Mule %o bevalida%ed

• In %$e roo% o) %$e source code $ierarc$y, crea%e a pac/a#e named Fsc$emasG($e M- sc$emas used @$en valida%in# are %o be s%ored in %$is pac/a#e

* " Create the 5(L Schemas($e example @ill use %$ree M- sc$emas @$en valida%in# (@o o) %$ese M- sc$emas, %$e mainsc$emas, $ave %$e same %ar#e% namespace ($is is %o s$o@ $o@ %o @or/ around a problem @i%$ %$eMule sc$ema valida%ion )il%er preven%in# %$e use o) mul%iple sc$emas @i%$ %$e same %ar#e%namespace in one )il%er($e %$ird sc$ema is included in %$e second M- sc$ema bu% %$e %ype de)ined in %$e %$ird sc$ema isno% used ye% e @ill loo/ a% i% a% %$e end o) %$e c$ap%er, %o solve %$e problem o) Mule no% bein# able%o )ind %$e included M- sc$ema

• In %$e Fsc$emaG pac/a#e in %$e source direc%ory, crea%e a )ile named Fsc$ema2 xsdG @i%$ %$e)ollo@in# con%en%s:

<?xml version="1.0" encoding="UTF-8"?><schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:ivan="http://www.ivan.com/schemas" targetNamespace="http://www.ivan.com/schemas" attributeFormDefault="unqualified">

<element name="person" type="ivan:Person"/> <complexType name="Person"> <sequence> <element name="firstName" type="string" nillable="false"/> <element name="lastName" type="string" nillable="false"/> <element name="age" type="int"/> </sequence> </complexType></schema>

• In %$e same pac/a#e, crea%e a )ile named Fsc$ema3 xsdG @i%$ %$e )ollo@in# con%en%s:

<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.ivan.com/schemas" targetNamespace="http://www.ivan.com/schemas" attributeFormDefault="unqualified">

<xs:element name="animal" type="Animal"/> <xs:complexType name="Animal"> <xs:sequence> <xs:element name="name" type="xs:string" nillable="false"/> <xs:element name="birthday" type="xs:date"/> </xs:sequence>

7

Page 77: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 77/354

Page 78: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 78/354

* & Create 5(L Data )iles($e )ollo@in# )iles are all %o be loca%ed in %$e direc%ory Fxml&da%aG

• ($e )ile Fanimal xmlG @i%$ %$e )ollo@in# con%en%s:1bservan% readers no%ice %$a% %$is M- )ra#men% s$ould be valida%ed a#ains% Fsc$ema3 xsdG

en%ered above<?xml version="1.0" encoding="UTF-8"?><ivan:animal xmlns:ivan="http://www.ivan.com/schemas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ivan.com/schemas schema3.xsd "> <name>Florpy Skudds</name> <birthday>2009-06-01</birthday></ivan:animal>

• ($e )ile Fperson xmlG @i%$ %$e )ollo@in# con%en%s:1bservan% readers no%ice %$a% %$is M- )ra#men% s$ould be valida%ed a#ains% Fsc$ema2 xsdG

en%ered above<?xml version="1.0" encoding="UTF-8"?><ivan:person xmlns:ivan="http://www.ivan.com/schemas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ivan.com/schemas schema2.xsd "> <firstName>Stiv</firstName> <lastName>Bator</lastName> <age>32</age></ivan:person>

• ($e )ile Finvalid xmlG @i%$ %$e )ollo@in# con%en%s:As %$e name su##es%s, %$is M- )ra#men% @ill no% pass valida%ion a#ains% any o) %$e above

M- sc$emas<ivan:person xmlns:ivan="http://www.ivan.com/schemas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> </ivan:person>

;

Page 79: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 79/354

* ' Create the (ule Configuration )iles($e example con%ains %@o Mule con)i#ura%ion )iles per Mule version, )or a %o%al o) )our Mulecon)i#ura%ion )iles

Create the Mule File Connector Configuration Files($e Mule )ile connec%or speci)ies $o@ )iles s$ould be read or @ri%%en )rom or %o a direc%ory in %$elocal )ile sys%em ($ese con)i#ura%ion )iles are iden%ical %o %$ose used in %$e previous example>o%$ )iles are %o be loca%ed in %$e roo% o) %$e pro4ec%'s source direc%ory

($e Mule 2 x con)i#ura%ion )ile is named Fmule2&)ileconnec%or xmlG:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:file="http://www.mulesource.org/schema/mule/file/2.2" xsi:schemaLocation=" http://www.mulesource.org/schema/mule/file/2.2 http://www.mulesource.org/schema/mule/file/2.2/mule-file.xsd

http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd">

<file:connector name="fileConnector" streaming="false" pollingFrequency="1000"> <file:expression-filename-parser/> </file:connector></mule>

($e Mule 3 x con)i#ura%ion )ile is named Fmule3&)ileconnec%or xmlG:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:file="http://www.mulesoft.org/schema/mule/file"

xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/3.2/mule-file.xsd

http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd">

<file:connector name="fileConnector" streaming="false" pollingFrequency="1000"> <file:expression-filename-parser/> </file:connector></mule>

($ese )ile connec%ors $ave %$e )ollo@in# proper%ies:•

($e name is F)ile"onnec%orGBsed @$en re)errin# %o %$e connec%ors )rom %$e )ile inbound endpoin%s• *ass a )ile, no% a )ile inpu% s%ream, as %$e messa#e payload• "$ec/ %$e inpu% direc%ory eac$ ! m• %ore %$e name o) a )ile in a messa#e proper%y

+

Page 80: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 80/354

Create the Main Mule Configuration Files

($e main Mule con)i#ura%ion )iles are 0ui%e di))eren%, al%$ou#$ %$ey rou#$ly represen% %$e sames%ruc%ure($ese Mule con)i#ura%ion )iles are also loca%ed in %$e roo% o) %$e source direc%ory o) %$e pro4ec%

The (ule " x Configuration )ile

($e Mule 2 x con)i#ura%ion )ile are buil%&up in a %radi%ional manner o) mul%iple services in a model:• 1ne inpu% service

Responsible )or receivin# %$e messa#es %o valida%e• 1ne valida%ion service

A%%emp%s %o valida%e messa#es a#ains% %$e %@o M- sc$emas crea%edearlier I) %$e messa#e passes valida%ion a#ains% ei%$er o) %$ese sc$emas, i% is considered %o $ave passed valida%ionand is passed on %o a service %$a% @ill only receive messa#es $avin# passed valida%ionI) %$e messa#e does no% pass valida%ion a#ains any o) %$e sc$emas, i% @ill be passed on %o anerror service

• 1ne processin# service($is service @ill receive only messa#es $avin# passed valida%ion Any processin# o) validmessa#es @ould be implemen%ed in %$is service In %$is example, %$ese messa#es are 4us%@ri%%en %o a direc%ory named F#ood&messa#esG usin# %$e same name as %$e ori#inal )ile

• 1ne error service($is service @ill receive only messa#es %$a% do no% pass valida%ionMessa#es $avin# )ailed valida%ion @ill be @ri%%en %o a direc%ory named Fbad&messa#esG

($e Mule 2 x con)i#ura%ion )ile is named Fmule2&valida%ion xmlG and loo/s li/e %$is:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:file="http://www.mulesource.org/schema/mule/file/2.2" xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.2" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:mule-xml="http://www.mulesource.org/schema/mule/xml/2.2" xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.2" xsi:schemaLocation=" http://www.mulesource.org/schema/mule/file/2.2 http://www.mulesource.org/schema/mule/file/2.2/mule-file.xsd

http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd

http://www.mulesource.org/schema/mule/stdio/2.2

http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.mulesource.org/schema/mule/xml/2.2 http://www.mulesource.org/schema/mule/xml/2.2/mule-xml.xsd

http://www.mulesource.org/schema/mule/vm/2.2 http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd">

<spring:beans> <spring:import resource="mule2-fileconnector.xml"/> </spring:beans>

<!-- The <model> in this example contains four different services: - An input service responsible for accepting messages in the

;

Page 81: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 81/354

form of files placed in a certain directory. - A validation service that validates messages. Depending on whether a message having passed validation or not, it is either passed on to the processing service or to the error service. - A processing service. This service receives only messages having passed validation. In this example, these messages are just written to the good messages directory.

- An error service. This service receives only messages that do not pass validtion. in this example, these messages are just written to the bad messages directory. --> <model name="xmlValidationModel"> <service name="xmlInputService"> <inbound> <!-- Input endpoint receiving files in a directory, applying filtering by filename. --> <file:inbound-endpoint connector-ref="fileConnector" path="./file-input-directory"> <file:filename-wildcard-filter pattern="*.xml"/> </file:inbound-endpoint> </inbound> <outbound> <pass-through-router> <vm:outbound-endpoint path="message.validation"/> </pass-through-router> </outbound> </service>

<!-- Service that validates messages and pass them on depending on the result of the validation. --> <service name="xmlValidationService"> <inbound> <vm:inbound-endpoint path="message.validation"/> <!-- A selective consumer router can apply one filter to incoming messages. If a message is matched by the filter, in this example passes XML validation, the message is passed on to the component (if any) and later to any outbound endpoint of the service. If a message is not matched by the filter, that is do not pass XML validation, the catch-all strategy is invoked if such is configured. Otherwise the message is ignored and a warning message logged. --> <selective-consumer-router> <!-- The OR filter contains two or more other filters and accept a message if it matches the criteria of at least one of the contained filters. --> <or-filter> <!-- A message sent to a schema validation filter is matched if it passes validation by the schema(s) of the filter. Due to a bug in the XML parser used for validation, multiple names with the same namespace cannot be used by a single schema validation filter. A work-around is to use an OR filter in combination with a schema validation filter for each schema. The returnResult attribute has been set to false in order to increase the speed of the filter. --> <mule-xml:schema-validation-filter schemaLocations="schemas/schema2.xsd" returnResult="false"/> <mule-xml:schema-validation-filter schemaLocations="schemas/schema3.xsd"

;!

Page 82: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 82/354

returnResult="false"/> </or-filter> </selective-consumer-router> <!-- Catch-all strategy invoked when a message does not pass XML validation. Route the message to the error service. --> <forwarding-catch-all-strategy>

<vm:outbound-endpoint path="message.error"/> </forwarding-catch-all-strategy> </inbound> <outbound> <!-- Messages arriving here are valid. Pass these messages on to the processing service. --> <pass-through-router> <vm:outbound-endpoint path="message.processing"/> </pass-through-router> </outbound> </service>

<!-- Service that receives messages having passed validation. Writes the message to the good messages directory using the original file name. --> <service name="xmlProcessingService"> <inbound> <vm:inbound-endpoint path="message.processing"/> </inbound> <outbound> <pass-through-router> <file:outbound-endpoint path="good-messages/" outputPattern="#[header:originalFilename]"/> </pass-through-router> </outbound> </service>

<!-- Service that receives messages having failed validation. Writes the message to the bad messages directory using the original file name. --> <service name="xmlErrorService"> <inbound> <vm:inbound-endpoint path="message.error"/> </inbound> <outbound> <pass-through-router> <file:outbound-endpoint path="bad-messages/" outputPattern="#[header:originalFilename]"/> </pass-through-router> </outbound> </service> </model></mule>

9o%e %$a% :• ($e )ile connec%or con)i#ura%ion )ile is impor%ed usin# %$e prin# mec$anism @e $ave seen

in earlier examples• ($e )irs% service in %$e model, %$e FxmlInpu% erviceG, con%ains an inbound endpoin%

accep%in# )iles @i%$ %$e ex%ension FxmlG )rom a desi#na%ed direc%ory• ($e FxmlInpu% erviceG passes received messa#es on usin# %$e VM %ranspor%

VM %ranspor% is commonly used as seen in %$is example, as a means %o connec% servicesrunnin# in a sin#le ?ava vir%ual mac$ine

;2

Page 83: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 83/354

• ($e FxmlValida%ion erviceG accep%s incomin# messa#es usin# VM %ranspor%• ($e FxmlInpu% erviceG con%ains a selec%ive&consumer&rou%erL elemen% in i%s inboundL

elemen%($e selec%ive consumer rou%er applies one )il%er %o incomin# messa#esI) a messa#e is ma%c$ed by %$e )il%er, i% is passed on %o %$e componen% o) %$e service Hi) anyand %$en la%er %o any ou%bound endpoin% o) %$e serviceI) a messa#e is no% ma%c$ed by %$e )il%er, i% @ill be passed on %o %$e ca%c$&all s%ra%e#y o) %$eservice I) %$ere is no ca%c$&all s%ra%e#y con)i#ured on %$e service, %$e messa#e @ill bei#nored and a @arnin# messa#e @ri%%en %o %$e lo#

• ($e selec%ive&consumer&rou%erL con%ains an or&)il%erL elemen%An 1R )il%er enables us %o combine mul%iple )il%ers A messa#e is ma%c$ed by %$e 1R )il%er i)i% is ma%c$ed by a% leas% one )il%er con%ained in %$e 1R )il%er

• ($e or&)il%erL elemen% con%ains %@o sc$ema&valida%ion&)il%erL elemen%sA sc$ema valida%ion )il%er valida%es messa#es a#ains% %$e sc$ema speci)ied by %$e

schemaLocation a%%ribu%e A messa#e is ma%c$ed i) i% passes valida%ion a#ains% %$e sc$ema

9o%e %$a% %$e sc$ema lan#ua#e, @$ic$ by de)aul% is M-, may be speci)ied• In a sc$ema&valida%ion&)il%erL, %$e sc$ema-oca%ions a%%ribu%e allo@s us %o supply a

comma&separa%ed lis% o) sc$emas %o valida%e a#ains%A bu# in %$e erces parser supplied by %$e ?ava E run%ime environmen% preven%s us )romspeci)yin# mul%iple M- sc$emas @i%$ %$e same %ar#e% namespace

• ($e cons%ruc% @i%$ %$e or&)il%erL and %$e %@o sc$ema&valida%ion&)il%erL elemen%s is a@or/&around due %o our M- sc$emas $avin# %$e same %ar#e% namespace

• ($e return#esult a%%ribu%e in bo%$ %$e sc$ema&valida%ion&)il%erL elemen%s $ave been se% %o)alse($e de)aul% value o) %$is a%%ribu%e is %rue, @$ic$ causes %$e resul% o) %$e valida%ion %o become%$e payload o) %$e messa#e e%%in# %$is a%%ribu%e %o )alse avoids %$e conversion o) %$eincomin# messa#e payload %o a .1M node and also avoids s%orin# %$e resul% o) %$evalida%ion

• ($e )inal elemen% in %$e inboundL elemen% o) %$e xmlValida%ion ervice service is a)or@ardin#&ca%c$&all&s%ra%e#yL elemen%

As described above @$en discussin# %$e selec%ive&consumer&rou%erL elemen%, %$e)or@ardin# ca%c$&all s%ra%e#y is used %o speci)y @$ere %o send messa#es %$a% are no% accep%ed

by %$e selec%ive consumer rou%er• ($e ou%boundL elemen% o) %$e xmlValida%ion ervice service speci)ies %$a% messa#es are %o

be sen% %o %$e messa#e processin# serviceA#ain, messa#es arrivin# $ere are messa#es %$a% pass valida%ion e can also see %$a% %$eVM %ranspor% is used %o send messa#es be%@een services in one and %$e same ?ava VM

• ($e xml*rocessin# ervice service receives messa#es on %$e VM %ranspor% and @ri%es %$em%o %$e F#ood&messa#esG direc%ory, usin# %$e ori#inal )ilename

• 6inally, %$e xmlError ervice service is almos% iden%ical %o %$e processin# service:Messa#es are received on %$e VM %ranspor% and @ri%%en %o a direc%ory usin# %$e ori#inal)ilename ($ese messa#es @ill be @ri%%en %o %$e Fbad&messa#esG direc%ory, since %$ey aremessa#es %$a% do no% pass valida%ion

;3

Page 84: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 84/354

The (ule & x Configuration )ile

($e s%ruc%ure o) %$e Mule 3 x con)i#ura%ion )ile is 0ui%e di))eren% )rom %$e con)i#ura%ion )ile in %$e previous sec%ion Ins%ead o) a number o) services, %$e Mule 3 x con)i#ura%ion consis%s o) %@o )lo@sJ%$e )irs% per)orms valida%ion o) a messa#e a#ains% our M- sc$emas, %$e second )lo@ is %$e main)lo@ %$a% rou%es messa#es dependin# on @$e%$er %$ey pass valida%ion or no%

($e Mule 3 x con)i#ura%ion )ile is named Fmule3&valida%ion xmlG and loo/s li/e %$is:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:stdio="http://www.mulesoft.org/schema/mule/stdio" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:mule-xml="http://www.mulesoft.org/schema/mule/xml" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/file

http://www.mulesoft.org/schema/mule/file/3.2/mule-file.xsd

http://www.mulesoft.org/schema/mule/stdio

http://www.mulesoft.org/schema/mule/stdio/3.2/mule-stdio.xsdhttp://www.mulesoft.org/schema/mule/corehttp://www.mulesoft.org/schema/mule/core/3.2/mule.xsd

http://www.mulesoft.org/schema/mule/xmlhttp://www.mulesoft.org/schema/mule/xml/3.2/mule-xml.xsd

http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!--

This is a private flow, that is a flow without any message sources. Such a flow can only be referenced from within the same Mule instance, not from outside the JVM in question. This flow validates the message payload against two XML schemas. If validation succeeds, a message property 'validated' is set to 'true'.

A private flow has its own context and exception strategy. This means that exceptions will not propagate out of the private flow. If an exception is thrown, the message payload is set to null.

The processing strategy of the flow must be synchronous, otherwise the flag indicating a message having passed validation may be checked before it is set. --> <flow name="ValidationFlow" processingStrategy="synchronous"> <!-- Any message properties set here will not be present on the message if the filter below does not match the message. -->

<message-filter throwOnUnaccepted="false"> <!-- The OR filter contains two or more other filters and accept a message if it matches the criteria of at least one of the contained filters. --> <or-filter> <!-- A message sent to a schema validation filter is matched if it passes validation by the schema(s) of the filter. Due to a bug in the XML parser used for validation, multiple names with the same namespace cannot be used by a single schema validation filter. A work-around is to use an OR filter in combination with a schema validation filter for each schema. The returnResult attribute has been set to false in order to increase the speed of the filter.

;5

Page 85: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 85/354

--> <mule-xml:schema-validation-filter schemaLocations="schemas/schema2.xsd" returnResult="false"/> <mule-xml:schema-validation-filter schemaLocations="schemas/schema3.xsd" returnResult="false"/> </or-filter> </message-filter>

<!-- If message passes validation, set a message property that indicates this. --> <message-properties-transformer scope="outbound"> <add-message-property key="validated" value="true"/> </message-properties-transformer> </flow>

<!-- This is the main flow that accepts input from a directory in the filesystem, sends messages to the private validation flow. Messages having passed validation are written to one directory (good messages) while messages causing exceptions, typically that does not pass validation, are written to another directory (bad messages). --> <flow name="SortingFlow"> <!-- Input endpoint receiving XML files from a directory. --> <file:inbound-endpoint connector-ref="fileConnector" path="./file-input-directory"> <file:filename-wildcard-filter pattern="*.xml"/> </file:inbound-endpoint>

<!-- Delegate message validation to a private flow. --> <flow-ref name="ValidationFlow"/>

<choice> <!-- A message is only considered to have passed validation when the 'validated' property has been set to 'true'. Note that the property is to be found in the outbound property scope. The 'header' evaluator evaluates the part of the message header specified by the expression attribute. --> <when expression="OUTBOUND:validated=true" evaluator="header"> <!-- Having passed validation, messages are written to the good-messages directory. --> <file:outbound-endpoint path="good-messages/" outputPattern="#[header:originalFilename]"/> </when> <otherwise> <!-- Messages in which the property indicating having passed validation is not present or is not set to 'true' are written to the bad-messages directory. --> <file:outbound-endpoint path="./bad-messages/" outputPattern="#[header:originalFilename]"/> </otherwise> </choice>

</flow></mule>

9o%e %$a% :• ($e )ile connec%or con)i#ura%ion )ile is impor%ed usin# %$e prin# mec$anism @e $ave seen

in earlier examples

;8

Page 86: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 86/354

• ($e )irs% )lo@, FValida%ion6lo@G, only con%ains a messa#e&)il%erL and a messa#e& proper%ies&%rans)ormerL($e FValida%ion6lo@G is a priva%e )lo@, a )lo@ %$a% $as no messa#e sources uc$ a )lo@ canonly be used )rom @i%$in %$e same ?VM ins%ance in @$ic$ %$e )lo@ resides

• ($e FValida%ion6lo@G )lo@ $as %$e a%%ribu%e processingStrateg$ se% %o Fsync$ronousG($is is because %$e )lo@ mus% comple%e be)ore i%s clien%s are allo@ed %o examine %$e resul% %$e value o) a messa#e proper%yI) @e allo@ed async$ronous processin#, %$en %$e FValida%ion6lo@G )lo@ @ould no% mana#e%o se% %$e messa#e proper%y, in %$e case o) a messa#e passin# valida%ion, be)ore %$e proper%y@as examined by %$e clien% ($us i% @ould seem as i) all messa#es )ailed %o pass valida%ion

• ($e messa#e&)il%erL $as %$e a%%ribu%ethro(On0naccepted se% %o )alse($is indica%es %$a% an excep%ion is no% %o be %$ro@n i) %$e )il%er con%ained in %$e messa#e&)il%erL elemen% does no% accep% a messa#e*rior %o Mule 3 2, excep%ions %$ro@n in priva%e )lo@s @ould propa#a%e ou% o) %$e )lo@ ($is

be$aviour @as considered a bu# and $as been )ixed in Mule 3 2 Also, i) an excep%ion is%$ro@n in a priva%e )lo@, %$e messa#e payload is se% %o an ins%ance o) %$e Mule class

1ullPa$load and %$e ori#inal payload @ould be los% ($is @ould mean %$a% %$e ori#inalmessa#e payload could no% be @ri%%en %o a )ile, i) i% )ailed %o pass valida%ion

• ($e messa#e&)il%erL con%ains an or&)il%erL elemen%An 1R )il%er enables us %o combine mul%iple )il%ers A messa#e is ma%c$ed by %$e 1R )il%er i)i% is ma%c$ed by a% leas% one )il%er con%ained in %$e 1R )il%er

• ($e or&)il%erL elemen% con%ains %@o sc$ema&valida%ion&)il%erL elemen%sA sc$ema valida%ion )il%er valida%es messa#es a#ains% %$e sc$ema speci)ied by %$e

schemaLocation a%%ribu%e A messa#e is ma%c$ed i) i% passes valida%ion a#ains% %$e sc$ema 9o%e %$a% %$e sc$ema lan#ua#e, @$ic$ by de)aul% is M-, may be speci)ied

In a sc$ema&valida%ion&)il%erL, %$e sc$ema-oca%ions a%%ribu%e allo@s us %o supply acomma&separa%ed lis% o) sc$emas %o valida%e a#ains%A bu# in %$e erces parser supplied by %$e ?ava E run%ime environmen% preven%s us )romspeci)yin# mul%iple M- sc$emas @i%$ %$e same %ar#e% namespace

• ($e cons%ruc% @i%$ %$e or&)il%erL and %$e %@o sc$ema&valida%ion&)il%erL elemen%s is a@or/&around due %o our M- sc$emas $avin# %$e same %ar#e% namespace

• ($e return#esult a%%ribu%e in bo%$ %$e sc$ema&valida%ion&)il%erL elemen%s $ave been se% %o)alse($e de)aul% value o) %$is a%%ribu%e is %rue, @$ic$ causes %$e resul% o) %$e valida%ion %o become%$e payload o) %$e messa#e e%%in# %$is a%%ribu%e %o )alse avoids %$e conversion o) %$eincomin# messa#e payload %o a .1M node and also avoids s%orin# %$e resul% o) %$evalida%ion

• ($e F or%in#6lo@G )lo@ is %$e main )lo@ o) %$is con)i#ura%ion• ($e F or%in#6lo@G con%ains an inbound endpoin% accep%in# )iles @i%$ %$e ex%ension FxmlG

)rom a desi#na%ed direc%ory• A )lo@&re)L elemen% is used %o re)er %o %$e FValida%ion6lo@G priva%e )lo@ discussed above

Recall %$a% %$e FValida%ion6lo@G )lo@ @ill %$ro@ an excep%ion i) a messa#e %$a% does no% pass valida%ion is encoun%ered

• ($e c$oiceL elemen% is similar %o %$e s@i%c$&case cons%ruc% in ?avaI% can con%ain a number o) @$enL elemen%s, %$a% speci)y ac%ions %o be %a/en @$en cer%ain

;7

Page 87: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 87/354

condi%ions are me%, and an o%$er@iseL elemen% speci)yin# ac%ions %o be %a/en i) none o) %$econdi%ions in %$e @$enL elemen%s in %$e c$oiceL @ere me%In %$is example, messa#es %$a% $as a proper%y @i%$ %$e name Fvalida%edG se% %o %$e valueF%rueG @ill be @ri%%en %o %$e F#ood&messa#esG direc%ory All o%$er messa#es @ill be @ri%%en%o %$e Fbad&messa#esG direc%ory

• ($e @$enL elemen% $as an expression a%%ribu%e @i%$ %$e valueF1B(>1B9.:valida%edW%rueG and an evaluator a%%ribu%e @i%$ %$e value F$eaderG($e messa#e processor inside %$e @$enL elemen% @ill be invo/ed @$en %$e messa#e$eader proper%y @i%$ %$e name Fvalida%orG in %$e ou%bound scope $as a value %$a% is e0ual %oF%rueG

e @ill %a/e a closer loo/ a% messa#e proper%ies in a la%er c$ap%er ($ere is also a sec%ion onmessa#e proper%ies in %$e Recipes&par% o) %$is boo/

;

Page 88: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 88/354

Page 89: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 89/354

• A#ain, re)res$ %$e pro4ec% in Eclipse($e )iles o) %$e pro4ec% s$ould loo/ li/e %$is in %$e Eclipse *ac/a#e Explorer:

*ro4ec% )iles a)%er $avin# copied %$e %$ree sample M-&)iles %o %$e inpu% direc%ory

and Mule $avin# per)ormed valida%ion o) %$e )iles

Run %$e Mule 2 x version o) %$e example pro#ram:• "$an#e %$e Mule dis%ribu%ion %o Mule 2 x, as described inappendix >• .ele%e %$e %$ree direc%ories crea%ed by Mule and %$eir con%en%s

($a% is, %$e F)ile&inpu%&direc%oryG, %$e F#ood&messa#esG and %$e Fbad&messa#esG direc%ories• %ar% %$e example pro#ram by ri#$%&clic/in# %$e Fmule2&valida%ion xmlG )ile and selec% Run

As&LMule erver

($e resul% o) runnin# %$e Mule 2 x version s$ould be similar %o @$a% @e've already seen

e see %$a% Mule is able %o rou%e messa#es @i%$ an M- payload dependin# on @$e%$er %$e M-da%a passes valida%ion a#ains% cer%ain sc$emas

;+

Page 90: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 90/354

* , 3ali/ation an/ 5(L Schema %mports($is c$ap%er could $ave ended $ere i) i% @ere no% )or a complica%ion %$a% arises i) an M- sc$emaused by %$e Mule sc$ema valida%or %ries %o impor% ano%$er M- sc$ema %$a% de)ines some %ypes%$a% %$e )irs% sc$ema uses

Modif# the 0M$ Sche&a

(o ma/e %$e problem visible, @e @ill modi)y %$e M- sc$ema Fsc$ema3 xsdG %o use %$e cus%omda%e&%ype de)ined in %$e M- sc$ema Fsc$ema5 xsdG

• Modi)y %$e M- sc$ema Fsc$ema3 xsdG %o loo/ li/e %$is Hmodi)ica%ions $i#$li#$%ed :

<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.ivan.com/schemas" targetNamespace="http://www.ivan.com/schemas" attributeFormDefault="unqualified">

<xs:include schemaLocation="schema4.xsd"/> <xs:element name="animal" type="Animal"/>

<xs:complexType name="Animal"> <xs:sequence> <xs:element name="name" type="xs:string" nillable="false"/> <xs:element name="birthday" type="ivan_date_type"/> </xs:sequence> </xs:complexType></xs:schema>

• "$an#e %$e Mule dis%ribu%ion %o Mule 3 x, as described inappendix >• In %$e *ac/a#e or *ro4ec% Explorer in Eclipse, ri#$%&clic/ %$e Fmule3&valida%ion xmlG )ile

and selec% Run As &L Mule erver• ($e Mule server is, a)%er some %ime, %ermina%ed due %o an error and %$e console s$o@s,

amon# lon# s%ac/&%races, %$e )ollo@in# error messa#e )rom Mule:

...ERROR 2011-10-14 06:42:44,408 [main] org.mule.MuleServer:********************************************************************************Message : src-resolve: Cannot resolve the name 'ivan_date_type' to a(n) 'typedefinition' component.Type : org.mule.api.lifecycle.InitialisationExceptionCode : MULE_ERROR-71999JavaDoc :http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/lifecycle/InitialisationException.htmlObject : org.mule.module.xml.filters.SchemaValidationFilter@53d9f80********************************************************************************...

Apparen%ly, Mule $as no% been able %o read %$e M- sc$ema Fsc$ema5 xsdG in @$ic$ %$e da%a%ypeFivanPda%aP%ypeG is de)ined

+

Page 91: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 91/354

)&ple&ent a Resource Resolver

I) @e %a/e a loo/ a% %$e A*I documen%a%ion o) %$e classSchema2alidation,ilter %$a% implemen%s %$eMule sc$ema&valida%ion&)il%erL, @e can see %$a% %$ere is a #e%%er& and se%%er&me%$od )or a resource&resolverJ get#esource#esolver and set#esource#esolver ($e %ype o) %$e resource resolver is LS#esource#esolver ($is is an in%er)ace %$a% can be )ound in%$e ?ava E A*I documen%a%ion -oo/in# a% %$a% A*I documen%a%ion, @e can read %$a% a resource&resolver implemen%in# %$is in%er)ace allo@s an applica%ion %o in%ercep% %$e inclusion o) ex%ernalen%i%ies %$is sounds very appropria%e in our case

• In %$e source direc%ory o) %$e pro4ec%, crea%e %$e pac/a#e Fcom ivan resolverG• Implemen% a cus%om resource resolver as in %$e lis%in# belo@:

package com.ivan.resolver;

import java.net.URL;import java.util.logging.Logger;import org.w3c.dom.DOMImplementation;import org.w3c.dom.bootstrap.DOMImplementationRegistry;

import org.w3c.dom.ls.DOMImplementationLS;import org.w3c.dom.ls.LSInput;import org.w3c.dom.ls.LSResourceResolver;

/** * A custom resource resolver used by the DOM parser to locate * external entities included in XML schemas. *

* @author Ivan Krizsan */public class MyResourceResolver implements LSResourceResolver{ /* Constant(s): */ /** Package in which XML schemas are to be located. */ private final static String SCHEMA_PACKAGE = "schemas/";

/* Class variable(s): */ private final static Logger sLogger = Logger .getLogger(MyResourceResolver.class.getName());

/* Instance variable(s): */ private DOMImplementationLS mDOMImplLs;

public MyResourceResolver() throws Exception { /* * We need a DOM impl LS, in order to be able to create LSInput * objects for the resources we resolve. */ DOMImplementationRegistry theDOMImplRegistry = DOMImplementationRegistry.newInstance(); DOMImplementation theDOMImpl = theDOMImplRegistry.getDOMImplementation("XML 3.0");

mDOMImplLs = (DOMImplementationLS)theDOMImpl.getFeature("LS", "3.0"); }

/* * (non-Javadoc) *

* @see * org.w3c.dom.ls.LSResourceResolver#resolveResource(java.lang * .String, java.lang.String, java.lang.String, java.lang.String, * java.lang.String) */ @Override public LSInput resolveResource(String inType, String inNamespaceURI, String inPublicId, String inSystemId, String inBaseURI) { LSInput theRsrcInput = null;

/* Log entering this method. */

+!

Page 92: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 92/354

sLogger.info("Resolve resource with parameters: " + inType + "," + inNamespaceURI + "," + inPublicId + "," + inSystemId + "," + inBaseURI);

/* * If we can retrieve an URL for the resource, then create a * DOM input object for the resource. */ URL theURL =

this.getClass().getClassLoader() .getResource(SCHEMA_PACKAGE + inSystemId); sLogger.info("Resource URL: " + theURL); if (theURL != null) { theRsrcInput = mDOMImplLs.createLSInput(); theRsrcInput.setBaseURI(inBaseURI); theRsrcInput.setSystemId(theURL.toString()); }

return theRsrcInput; }

}

9o%e %$a%:• ($e resource resolver class implemen%s %$e LS#esource#esolver in%er)ace

($is is %$e %ype expec%ed by Mule's sc$ema valida%or• ($e pac/a#e in @$ic$ M- sc$emas are loca%ed is $ardcoded usin# %$e cons%an%

"<EMAP*A"KACE• An ob4ec% implemen%in# %$e +OMImplementationLS in%er)ace is crea%ed in %$e resource

resolver class' cons%ruc%or($is ob4ec% is needed in order %o crea%e %$e appropria%e /ind o) ob4ec%s, implemen%in#

LSInput , %$a% speci)ies $o@ %o re%rieve a resolved resource

Des, ob4ec%s o) %$e %ype LSInput can be crea%ed by ne@&in# a cer%ain class, bu% %$is class is a priva%e implemen%a%ion class and s$ould no% be ins%an%ia%ed %$a% @ay• ($e resolve#esource me%$od %a/es a number o) parame%ers

e @ill see @$a% %$e di))eren% parame%ers con%ain @$en runnin# %$e example• A classloader is used %o ob%ain %$e BR- o) %$e resource and, i) success)ul, an ob4ec%

implemen%in# %$e LSInput in%er)ace is crea%ed and proper%ies speci)yin# %$e resource are se%on %$e ob4ec%

+2

Page 93: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 93/354

Modif# the Sche&a alidators

-oo/in# a% %$e documen%a%ion )or %$e Mule 2 x and 3 ! sc$ema&valida%ion&)il%erL, @e soondiscover %$a% %$ere is no a%%ribu%e allo@in# us %o se% a cus%om resource resolver

%ar%in# @i%$ Mule 3 2, %$eresource#esolver-ref a%%ribu%e $as been added %o sc$ema&valida%ion&)il%erL6or %$e cases in @$ic$ %$is a%%ribu%e does no% exis%, %$e solu%ion is %o use a cus%om )il%er, %o @$ic$ @ecan supply arbi%rary proper%ies

• Modi)y %$e Fmule2&valida%ion xmlG Mule con)i#ura%ion )ile %o loo/ li/e %$is Hmodi)ica%ions$i#$li#$%ed :

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesource.org/schema/mule/core/2.2"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:file="http://www.mulesource.org/schema/mule/file/2.2"

xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.2" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:mule-xml="http://www.mulesource.org/schema/mule/xml/2.2" xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.2" xsi:schemaLocation=" http://www.mulesource.org/schema/mule/file/2.2 http://www.mulesource.org/schema/mule/file/2.2/mule-file.xsd

http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd

http://www.mulesource.org/schema/mule/stdio/2.2 http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.mulesource.org/schema/mule/xml/2.2 http://www.mulesource.org/schema/mule/xml/2.2/mule-xml.xsd

http://www.mulesource.org/schema/mule/vm/2.2 http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd">

<spring:beans> <spring:import resource="mule2-fileconnector.xml"/> <!-- Declare a resource resolver so we can inject it into the schema validation filter. --> <spring:bean id="myResourceResolver" class="com.ivan.resolver.MyResourceResolver"> </spring:bean> </spring:beans>

<!-- The <model> in this example contains four different services: - An input service responsible for accepting messages in the form of files placed in a certain directory. - A validation service that validates messages. Depending on whether a message having passed validation or not, it is either passed on to the processing service or to the error service. - A processing service. This service receives only messages having passed validation. In this example, these messages are just written to the good messages directory. - An error service. This service receives only messages that do not pass validtion. in this example, these messages are just written to the bad messages directory. --> <model name="xmlValidationModel">

<service name="xmlInputService"> <inbound>

+3

Page 94: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 94/354

<!-- Input endpoint receiving files in a directory, applying filtering by filename. --> <file:inbound-endpoint connector-ref="fileConnector" path="./file-input-directory"> <file:filename-wildcard-filter pattern="*.xml"/> </file:inbound-endpoint>

</inbound> <outbound> <pass-through-router> <vm:outbound-endpoint path="message.validation"/> </pass-through-router> </outbound> </service>

<!-- Service that validates messages and pass them on depending on the result of the validation. --> <service name="xmlValidationService"> <inbound> <vm:inbound-endpoint path="message.validation"/> <!-- A selective consumer router can apply one filter to incoming messages. If a message is matched by the filter, in this example passes XML validation, the message is passed on to the component (if any) and later to any outbound endpoint of the service. If a message is not matched by the filter, that is do not pass XML validation, the catch-all strategy is invoked if such is configured. Otherwise the message is ignored and a warning message logged. --> <selective-consumer-router> <!-- The OR filter contains two or more other filters and accept a message if it matches the criteria of at least one of the contained filters. --> <or-filter> <!-- A message sent to a schema validation filter is matched if it passes validation by the schema(s) of the filter. Due to a bug in the XML parser used for validation, multiple names with the same namespace cannot be used by a single schema validation filter. A work-around is to use an OR filter in combination with a schema validation filter for each schema. The returnResult attribute has been set to false in order to increase efficiency of the filter. --> <custom-filter class="org.mule.module.xml.filters.SchemaValidationFilter"> <spring:property name="schemaLocations" value="schemas/schema2.xsd"/> <spring:property name="returnResult" value="false"/> <spring:property name="resourceResolver" ref="myResourceResolver"/> </custom-filter> <custom-filter class="org.mule.module.xml.filters.SchemaValidationFilter"> <spring:property name="schemaLocations" value="schemas/schema3.xsd"/> <spring:property name="returnResult" value="false"/> <spring:property name="resourceResolver" ref="myResourceResolver"/> </custom-filter> </or-filter> </selective-consumer-router> <!-- Catch-all strategy invoked when a message does not

+5

Page 95: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 95/354

pass XML validation. Route the message to the error service. --> <forwarding-catch-all-strategy> <vm:outbound-endpoint path="message.error"/> </forwarding-catch-all-strategy> </inbound> <outbound> <!--

Messages arriving here are valid. Pass these messages on to the processing service. --> <pass-through-router> <vm:outbound-endpoint path="message.processing"/> </pass-through-router> </outbound> </service>

<!-- Service that receives messages having passed validation. Writes the message to the good messages directory using the original file name. --> <service name="xmlProcessingService"> <inbound> <vm:inbound-endpoint path="message.processing"/> </inbound> <outbound> <pass-through-router> <file:outbound-endpoint path="good-messages/" outputPattern="#[header:originalFilename]"/> </pass-through-router> </outbound> </service>

<!-- Service that receives messages having failed validation. Writes the message to the bad messages directory using the original file name. --> <service name="xmlErrorService"> <inbound> <vm:inbound-endpoint path="message.error"/> </inbound> <outbound> <pass-through-router> <file:outbound-endpoint path="bad-messages/" outputPattern="#[header:originalFilename]"/> </pass-through-router> </outbound> </service> </model></mule>

Hcon%inued on nex% pa#e

+8

Page 96: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 96/354

Page 97: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 97/354

The returnResult attribute has been set to false in order to increase efficiency of the filter. Note that in Mule 3.2, the resource resolver can be set using an attribute on the schema validation filter element. In earlier versions this had to be accomplished using a custom filter and injecting the resource resolver using Spring properties. --> <mule-xml:schema-validation-filter

schemaLocations="schemas/schema2.xsd" returnResult="false" resourceResolver-ref="myResourceResolver"/> <mule-xml:schema-validation-filter schemaLocations="schemas/schema3.xsd" returnResult="false" resourceResolver-ref="myResourceResolver"/> </or-filter> </message-filter>

<!-- If message passes validation, set a message property that indicates this. --> <message-properties-transformer scope="outbound"> <add-message-property key="validated" value="true"/> </message-properties-transformer> </flow>

<!-- This is the main flow that accepts input from a directory in the filesystem, sends messages to the private validation flow. Messages having passed validation are written to one directory (good messages) while messages causing exceptions, typically that does not pass validation, are written to another directory (bad messages). --> <flow name="SortingFlow"> <!-- Input endpoint receiving XML files from a directory. --> <file:inbound-endpoint connector-ref="fileConnector" path="./file-input-directory"> <file:filename-wildcard-filter pattern="*.xml"/> </file:inbound-endpoint>

<!-- Delegate message validation to a private flow. --> <flow-ref name="ValidationFlow"/>

<choice> <!-- A message is only considered to have passed validation when the 'validated' property has been set to 'true'. Note that the property is to be found in the outbound property scope. The 'header' evaluator evaluates the part of the message header specified by the expression attribute. --> <when expression="OUTBOUND:validated=true" evaluator="header"> <!-- Having passed validation, messages are written to the good-messages directory. --> <file:outbound-endpoint path="good-messages/" outputPattern="#[header:originalFilename]"/> </when> <otherwise> <!-- Messages in which the property indicating having passed validation is not present or is not set to 'true' are written to the bad-messages directory. --> <file:outbound-endpoint path="./bad-messages/" outputPattern="#[header:originalFilename]"/> </otherwise> </choice>

</flow></mule>

+

Page 98: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 98/354

9o%e %$a%:• A prin# bean implemen%ed by %$e cus%om resource resolver @e crea%ed in %$e previous

sec%ion is crea%ed in %$e sprin#:beansL elemen%• A ne@ a%%ribu%e,resource#esolver-ref , $as been added %o %$e sc$ema&valida%ion&)il%erL

elemen%s($e value o) %$e a%%ribu%e is %$e name o) %$e resource resolver prin# bean

Run the Exa&ple 'rogra&

9o@ @e are ready %o %ry %$e example pro#ram a#ain• In %$e *ac/a#e or *ro4ec% Explorer in Eclipse, ri#$%&clic/ %$e Fmule3&valida%ion xmlG )ile

and selec% Run As &L Mule erver• ($e Mule 3 x server s$ould s%ar% up and display console ou%pu% indica%in# a success)ul

s%ar%up 9o%e %$e )ollo@in# console ou%pu% in red, @$ic$ %ells us %$a% our resource resolver$as been invo/ed and s$o@s us %$e parame%ers In addi%ion, %$e resolved BR- o) %$e M-sc$ema is also s$o@n

...INFO 2011-10-14 07:01:20,040 [main] org.mule.module.xml.filters.SchemaValidationFilter:Schema factory implementation:com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory@3c9ff588INFO 2011-10-14 07:01:20,050 [main] org.mule.module.xml.filters.SchemaValidationFilter:Schema factory implementation:com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory@2f56f920Oct 14, 2011 7:01:20 AM com.ivan.resolver.MyResourceResolver resolveResourceINFO: Resolve resource with parameters:http://www.w3.org/2001/XMLSchema,http://www.ivan.com/schemas,null,schema4.xsd,nullOct 14, 2011 7:01:20 AM com.ivan.resolver.MyResourceResolver resolveResourceINFO: Resource URL:

file:/Users/ivan/EclipseWorkspace/MULE_EXAMPLES/MuleXMLSchemaValidation/bin/schemas/schema4.xsdINFO 2011-10-14 07:01:20,131 [main] org.mule.construct.FlowConstructLifecycleManager:Initialising flow: ValidationFlow...

• .rop %$e example M- )iles in%o %$e F)ile&inpu%&direc%oryG and veri)y %$a% %$ey aredis%ribu%ed %o %$e proper direc%ories, as be)ore

Run %$e Mule 2 x version o) %$e example pro#ram:• "$an#e %$e Mule dis%ribu%ion %o Mule 2 x, as described inappendix >• .ele%e %$e %$ree direc%ories crea%ed by Mule and %$eir con%en%s

($a% is, %$e F)ile&inpu%&direc%oryG, %$e F#ood&messa#esG and %$e Fbad&messa#esG direc%ories• %ar% %$e example pro#ram by ri#$%&clic/in# %$e Fmule2&valida%ion xmlG )ile and selec% Run

As&LMule erver($e resul% s$ould be similar %o %$a% ob%ained @$en runnin# %$e Mule 3 x version

($is concludes %$e example in %$is c$ap%ere $ave seen $o@ %o valida%e M- messa#es in Mule, $o@ %o use mul%iple M- sc$emas @i%$ %$e

same namespace @$en valida%in# M- and $o@ %o implemen% and use a cus%om resource resolver@i%$ %$e Mule sc$ema valida%or

+;

Page 99: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 99/354

. Extract 0M$ Message 'a#load ith 0'athIn %$is c$ap%er @e @ill loo/ a% $o@ %o use *a%$ in Mule %o ex%rac% a selec%ed par% o) %$e M- in amessa#e e @ill also see $o@ %o $andle M- namespaces in Mule messa#es

e @ill use %$e %ec$ni0ue o) runnin# %$e Mule con)i#ura%ion )iles, described in %$e previous

c$ap%ers, so @e @ill no% need a s%ar%er class

, 1 Create the Pro!ect"rea%e %$e pro4ec% as described in %$e appendix"rea%e a Mule *ro4ec%, namin# i%FMule *a%$*rocessin#G ($e Mule 3 $o% deploymen% can be s@i%c$ed o)) )rom %$e s%ar%, as %$is)ea%ure is no% o) use @$en runnin# Mule embedded In addi%ion %o %$e basic pro4ec% se%up, @e @ill

per)orm an addi%ional se%up )or %$is par%icular pro4ec%:• In %$e pro4ec% roo%, crea%e a direc%ory named Fxml&da%aG

($is direc%ory @ill con%ain %$e M- example )iles

, " Create 5(L Data )iles($e )ollo@in# )ile con%ains inpu% da%a %$a% @e @ill )eed in%o Mule I% is %o be loca%ed in %$e direc%oryFxml&da%aG

• "rea%e %$e )ile F*$one>oo/ xmlG @i%$ %$e )ollo@in# con%en%s:

<?xml version="1.0" encoding="UTF-8" ?><ivan:PhoneBook xmlns:ivan="http://www.ivan.com/schemas/addressbook" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<ivan:Person lastUpdate="2011-07-21" category="actor"> <ivan:FirstName>Gösta</ivan:FirstName> <ivan:LastName>Ekman</ivan:LastName> <ivan:Phone>044-123123</ivan:Phone> </ivan:Person>

<ivan:Person lastUpdate="2011-07-22" category="athlete"> <ivan:FirstName>J-O</ivan:FirstName> <ivan:LastName>Waldner</ivan:LastName> <ivan:Phone>08-12312312</ivan:Phone> </ivan:Person>

<ivan:Person lastUpdate="2010-12-11" category="actor"> <ivan:FirstName>Dolph</ivan:FirstName> <ivan:LastName>Lundgren</ivan:LastName> <ivan:Phone>0706-32132156</ivan:Phone> </ivan:Person></ivan:PhoneBook>

++

Page 100: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 100/354

, & Create the (ule Configuration )iles($is example con%ains %@o Mule con)i#ura%ion )iles per Mule version, )or a %o%al o) )our Mulecon)i#ura%ion )iles(@o o) %$e con)i#ura%ion )iles are %$e )ile connec%or con)i#ura%ion )iles %$a% s$ould be )amiliar %oreaders o) previous examples

Create the Mule File Connector Configuration Files

($e Mule )ile connec%or speci)ies $o@ )iles s$ould be read or @ri%%en )rom or %o a direc%ory in %$elocal )ile sys%em ($ese con)i#ura%ion )iles are iden%ical %o %$ose used in, amon# o%$er, %$e previousexample>o%$ )iles are %o be loca%ed in %$e roo% o) %$e pro4ec%'s source direc%ory

($e Mule 2 x con)i#ura%ion )ile is named Fmule2&)ileconnec%or xmlG:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:file="http://www.mulesource.org/schema/mule/file/2.2" xsi:schemaLocation=" http://www.mulesource.org/schema/mule/file/2.2 http://www.mulesource.org/schema/mule/file/2.2/mule-file.xsd

http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd">

<file:connector name="fileConnector" streaming="false" pollingFrequency="1000"> <file:expression-filename-parser/> </file:connector></mule>

($e Mule 3 x con)i#ura%ion )ile is named Fmule3&)ileconnec%or xmlG:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:file="http://www.mulesoft.org/schema/mule/file" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/3.2/mule-file.xsd

http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd">

<file:connector name="fileConnector" streaming="false" pollingFrequency="1000"> <file:expression-filename-parser/> </file:connector></mule>

($ese )ile connec%ors $ave %$e )ollo@in# proper%ies:• ($e name is F)ile"onnec%orG

Bsed @$en re)errin# %o %$e connec%ors )rom %$e )ile inbound endpoin%s• *ass a )ile, no% a )ile inpu% s%ream, as %$e messa#e payload• "$ec/ %$e inpu% direc%ory once every second H! m• %ore %$e name o) a )ile in a messa#e proper%y

!

Page 101: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 101/354

Create the Main Mule Configuration Files

($e main Mule con)i#ura%ion )iles are 0ui%e di))eren%, al%$ou#$ %$ey rou#$ly represen% %$e sames%ruc%ure($ese Mule con)i#ura%ion )iles are also loca%ed in %$e roo% o) %$e source direc%ory o) %$e pro4ec%

The (ule " x Configuration )ile

($e Mule 2 x con)i#ura%ion )ile consis%s o) one sin#le service @i%$ %@o endpoin%s:• 1ne inbound )ile endpoin%

($is endpoin% accep%s messa#es in %$e )orm o) )iles @i%$ %$e F xmlG su))ix placed in an inpu%direc%ory @i%$ %$e name F)ile&inpu%&direc%oryG

• 1ne ou%bound )ile endpoin%($is endpoin% @ri%es received )iles, usin# %$eir ori#inal name, %o a direc%ory @i%$ %$e nameFprocessed&xpa%$G

($e par%s o) impor%ance %o %$is example are %$e %rans)ormers in %$e inbound endpoin%6irs%, le%'s %a/e a loo/ a% %$e en%ire con)i#ura%ion )ile, @$ic$ is named Fmule2&xpa%$ xmlG andloca%ed in %$e roo% o) %$e source direc%ory:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:file="http://www.mulesource.org/schema/mule/file/2.2" xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.2" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:mule-xml="http://www.mulesource.org/schema/mule/xml/2.2" xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.2" xsi:schemaLocation=" http://www.mulesource.org/schema/mule/file/2.2 http://www.mulesource.org/schema/mule/file/2.2/mule-file.xsd

http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd

http://www.mulesource.org/schema/mule/stdio/2.2 http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.mulesource.org/schema/mule/xml/2.2 http://www.mulesource.org/schema/mule/xml/2.2/mule-xml.xsd

http://www.mulesource.org/schema/mule/vm/2.2 http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd">

<spring:beans> <spring:import resource="mule2-fileconnector.xml"/> </spring:beans>

<model name="xpathProcessingModel"> <service name="xpathProcessingService"> <inbound> <file:inbound-endpoint connector-ref="fileConnector" path="./file-input-directory"> <file:filename-wildcard-filter pattern="*.xml"/>

<!-- Must transform the XML data to DOM in order to receive a DOM node as result of the XPath transform. Otherwise we will receive the contents of the elements in the extracted XML fragment.

! !

Page 102: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 102/354

--> <mule-xml:xml-to-dom-transformer

returnClass="org.w3c.dom.Document" />

<!-- Extract a person from the phonebook which is alone in his/her category. --> <mule-xml:xpath-extractor-transformer

expression="/ivan:PhoneBook/ivan:Person[not(@category=following-sibling::ivan:Person/@category)]" resultType="NODE"> <!-- The XML data we are processing uses a namespace and we use a namespace prefix in the XPath expression. We must thus link the namespace prefix and the namespace URI. --> <mule-xml:namespace prefix="ivan" uri="http://www.ivan.com/schemas/addressbook"/> </mule-xml:xpath-extractor-transformer>

<!-- Must transform the message from DOM to text to make the resulting XML readable. --> <mule-xml:dom-to-xml-transformer/> </file:inbound-endpoint> </inbound> <outbound> <pass-through-router> <file:outbound-endpoint path="processed-xpath" outputPattern="#[header:originalFilename]">

<!-- Format the XML so that it is easier to read. --> <mule-xml:xml-prettyprinter-transformer/> </file:outbound-endpoint> </pass-through-router> </outbound> </service> </model></mule>

9o%e %$a%:• ($e model Fxpa%$*rocessin#ModelG con%ains one sin#le service: Fxpa%$*rocessin# erviceG• ($e inboundL elemen% o) %$e Fxpa%$*rocessin# erviceG con%ains a )ile endpoin% de)ined by

a )ile:inbound&endpoin%L elemen%As in earlier examples, i% accep%s )iles @i%$ %$e F xmlG su))ix %$a% are placed in a direc%ory@i%$ %$e name F)ile&inpu%&direc%oryG as messa#es

• ($e )ile:inbound&endpoin%L elemen% con%ains %$ree %rans)ormers• ($e )irs% %rans)ormer in %$e )ile:inbound&endpoin%L elemen% is a M-&%o&.1M %rans)ormer

Hin %$e mule&xml:xml&%o&dom&%rans)ormerL elemen%($is %rans)ormer %rans)orms M- da%a %o an .1M documen% con%ained in %$e ins%ance o)%$e classorg.()c.dom.+ocument

ince @e @an% %o ex%rac% an M- )ra#men% )rom %$e messa#e, @e )irs% need %o %rans)orm %$e%ex%ual represen%a%ion o) %$e M- da%a %o a .1M ob4ec% %ree on @$ic$ %$e *a%$ expressioncan opera%e

• ($e second %rans)ormer in %$e )ile:inbound&endpoin%L elemen% is %$e *a%$ ex%rac%or%rans)ormer Hin %$e mule&xml:xpa%$&ex%rac%or&%rans)ormerL elemen%

($is %rans)ormer applies an *a%$ expression %o %$e incomin# messa#e and re%urns %$e

! 2

Page 103: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 103/354

resul%in# M-• ($e *a%$ ex%rac%or %rans)ormer $as %@o a%%ribu%es:expression and result'$pe

($e expression a%%ribu%e con%ains %$e *a%$ expression A brie) explana%ion o) %$e *a%$expression used in %$is elemen% @ill appear in a subse0uen% sec%ion o) %$is c$ap%er($e result'$pe a%%ribu%e speci)ies @$a% /ind o) ob4ec% is %o be produced @$en evalua%in# %$e

*a%$ expression ($e )ollo@in# %ypes are available, @i%$ (RI9C bein# %$e de)aul%:(RI9C 4ava lan# %rin#

91.E or# @3c dom 9ode 91.E E( or# @3c dom 9ode-is%>11-EA9 4ava lan# >oolean

9BM>ER 4ava lan# .ouble• ($e mule&xml:xpa%$&ex%rac%or&%rans)ormerL elemen% con%ains a mule&xml:namespaceL

elemen%($e namespace o) %$e M- documen% )rom @$ic$ @e are %o ex%rac% M- da%a isF$%%p:==@@@ ivan com==sc$emas=addressboo/G In %$e *a%$ expression, %$e namespace

pre)ix FivanG is used ($e mule&xml:namespaceL elemen% is used %o lin/ a namespace pre)ix @i%$ a namespace BRI

• ($e Mule documen%a%ion claims %$a% namespaces )or *a%$ expressions can be declared#lobally, usin# a mule&xml:namespace&mana#erL elemen%Re#re%)ully, %$ere is no suppor% )or a #lobal namespace mana#er in %$e *a%$ ex%rac%or%rans)ormer un%il in Mule 3 2

• ($e )inal %rans)ormer in %$e )ile:inbound&endpoin%L elemen% is a .1M&%o& M-%rans)ormer Hin %$e mule&xml:dom&%o&xml&%rans)ormer=L elemen%($is %rans)ormer conver%s an M- payload in %$e )orm o) a .1M documen% %o a serializeds%rin# represen%a%ion

e do %$is since @e are #oin# %o @ri%e %$e M- da%a %o a )ile %$a% is %o be read by a $uman• ($e ou%boundL elemen% o) %$e Fxpa%$*rocessin# erviceG con%ains a pass&%$rou#$&rou%erL

%$a% in %urn con%ains a )ile:ou%bound&endpoin%L elemen%($e )ile:ou%bound&endpoin%L speci)ies %$a% messa#es are %o be @ri%%en %o a direc%ory namedFprocessed&xpa%$G usin# %$e ori#inal )ile name )rom a Mule proper%y

• ($e )ile:ou%bound&endpoin%L elemen% con%ains a mule&xml:xml&pre%%yprin%er&%rans)ormerL elemen%($e pre%%y&prin%er %rans)ormer )orma%s %$e M- s%rin# %$a% is @ri%%en %o %$e )ile o%$er@ise%$e M- @ill end up on one sin#le ro@

! 3

Page 104: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 104/354

The (ule & x Configuration )ile

($e Mule 3 x con)i#ura%ion )ile is very similar %o %$e Mule 2 x con)i#ura%ion )ile, bu% ins%ead o)declarin# a service, i% declares a )lo@

($e Mule 3 x con)i#ura%ion )ile is named Fmule3&xpa%$ xmlG, is also loca%ed in %$e roo% o) %$e

source direc%ory, and loo/s li/e %$is:<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:stdio="http://www.mulesoft.org/schema/mule/stdio" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:mule-xml="http://www.mulesoft.org/schema/mule/xml" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/file

http://www.mulesoft.org/schema/mule/file/3.2/mule-file.xsd

http://www.mulesoft.org/schema/mule/stdiohttp://www.mulesoft.org/schema/mule/stdio/3.2/mule-stdio.xsd

http://www.mulesoft.org/schema/mule/corehttp://www.mulesoft.org/schema/mule/core/3.2/mule.xsd

http://www.mulesoft.org/schema/mule/xmlhttp://www.mulesoft.org/schema/mule/xml/3.2/mule-xml.xsd

http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<spring:beans> <spring:import resource="mule3-fileconnector.xml"/> </spring:beans>

<!-- This element declares a global namespace manager which links namespace prefixes and URIs. Support for a global namespace manager in the XPath transformer was not implemented until in Mule 3.2. --> <mule-xml:namespace-manager includeConfigNamespaces="false"> <mule-xml:namespace prefix="ivan" uri="http://www.ivan.com/schemas/addressbook"/> </mule-xml:namespace-manager>

<flow name="xpathProcessingFlow"> <!-- Input endpoint receiving XML files from a directory. --> <file:inbound-endpoint connector-ref="fileConnector" path="./file-input-directory"> <file:filename-wildcard-filter pattern="*.xml"/>

<!-- Must transform the XML data to DOM in order to receive a DOM node as result fo the XPath transform. Otherwise we will receive the contents of the elements in the extracted XML fragment. --> <mule-xml:xml-to-dom-transformer returnClass="org.w3c.dom.Document" />

<!-- Extract a person from the phonebook which is alone in his/her category. --> <mule-xml:xpath-extractor-transformer expression="/ivan:PhoneBook/ivan:Person[not(@category=following-sibling::ivan:Person/@category)]" resultType="NODE"> <!-- If using Mule < 3.2 or Mule >= 3.2 without a global

! 5

Page 105: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 105/354

namespace manager, namespace prefixes and URIs must be linked this way. With Mule 3.2, the <mule-xml:namespace> element can be commented out since we have a global namespace manager in this configuration file. --> <!-- <mule-xml:namespace prefix="ivan"

uri="http://www.ivan.com/schemas/addressbook"/> --> </mule-xml:xpath-extractor-transformer>

<!-- Must transform the message from DOM to text to make the resulting XML readable. --> <mule-xml:dom-to-xml-transformer/> </file:inbound-endpoint>

<file:outbound-endpoint path="processed-xpath" outputPattern="#[header:originalFilename]">

<!-- Format the XML so that it is easier to read. --> <mule-xml:xml-prettyprinter-transformer/> </file:outbound-endpoint> </flow></mule>

9o%e %$a%:• Immedia%ely a)%er %$e sprin#:beansL elemen% includin# %$e )ile connec%or, %$ere is a mule&

xml:namespace&mana#erL($is elemen% s$o@s $o@ M- namespace pre)ixes and BRIs can be lin/ed #lobally I) youare usin# Mule 3 2 or la%er, declarin# %$e namespace %$is @ay is su))icien%

• ($e )ile con%ains a sin#le )lo@, Fxpa%$*rocessin#6lo@G•

($e )lo@ con%ains a )ile endpoin% de)ined by a )ile:inbound&endpoin%L elemen%As in earlier examples, i% accep%s )iles @i%$ %$e F xmlG su))ix %$a% are placed in a direc%ory@i%$ %$e name F)ile&inpu%&direc%oryG as messa#es

• ($e )ile:inbound&endpoin%L elemen% con%ains %$ree %rans)ormers• ($e )irs% %rans)ormer in %$e )ile:inbound&endpoin%L elemen% is a M-&%o&.1M %rans)ormer

Hin %$e mule&xml:xml&%o&dom&%rans)ormerL elemen%($is %rans)ormer %rans)orms M- da%a %o an .1M documen% con%ained in %$e ins%ance o)%$e classorg.()c.dom.+ocument

ince @e @an% %o ex%rac% an M- )ra#men% )rom %$e messa#e, @e )irs% need %o %rans)orm %$e%ex%ual represen%a%ion o) %$e M- da%a %o a .1M ob4ec% %ree on @$ic$ %$e *a%$ expressioncan opera%e

• ($e second %rans)ormer in %$e )ile:inbound&endpoin%L elemen%, %$e mule&xml:xpa%$&ex%rac%or&%rans)ormerL elemen%, is %$e *a%$ ex%rac%or %rans)ormer($is %rans)ormer applies an *a%$ expression %o %$e incomin# messa#e and re%urns %$eresul%in# M-

• ($e *a%$ ex%rac%or %rans)ormer $as %@o a%%ribu%es:expression and result'$pe($e expression a%%ribu%e con%ains %$e *a%$ expression A brie) explana%ion o) %$e *a%$expression used in %$is elemen% @ill appear in %$e nex% sec%ion o) %$is c$ap%er($e result'$pe a%%ribu%e speci)ies @$a% /ind o) ob4ec% is %o be produced @$en evalua%in# %$e

*a%$ expression ($e )ollo@in# %ypes are available, @i%$ (RI9C bein# %$e de)aul%:(RI9C 4ava lan# %rin#

! 8

Page 106: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 106/354

91.E or# @3c dom 9ode 91.E E( or# @3c dom 9ode-is%>11-EA9 4ava lan# >oolean

9BM>ER 4ava lan# .ouble• ($e mule&xml:xpa%$&ex%rac%or&%rans)ormerL elemen% con%ains a mule&xml:namespaceL

elemen%($e namespace o) %$e M- documen% )rom @$ic$ @e are %o ex%rac% M- da%a isF$%%p:==@@@ ivan com==sc$emas=addressboo/G In %$e *a%$ expression, %$e namespace

pre)ix FivanG is used ($e mule&xml:namespaceL elemen% is used %o lin/ a namespace pre)ix @i%$ a namespace BRI

ince I @as usin# Mule 3 2 @$en developin# %$is example and $ave a #lobal namespace&mana#er, I $ave commen%ed ou% %$is elemen%

• ($e )inal %rans)ormer in %$e )ile:inbound&endpoin%L elemen% is a .1M&%o& M-%rans)ormer Hin %$e mule&xml:dom&%o&xml&%rans)ormer=L elemen%($is %rans)ormer conver%s an M- payload in %$e )orm o) a .1M documen% %o a serializeds%rin# represen%a%ion

e do %$is since @e are #oin# %o @ri%e %$e M- da%a %o a )ile %$a% is %o be read by a $uman• ($e )lo@ also con%ains a )ile:ou%bound&endpoin%L elemen%

($is ou%bound endpoin% speci)ies %$a% messa#es are %o be @ri%%en %o a direc%ory namedFprocessed&xpa%$G usin# %$e ori#inal )ile name )rom a Mule proper%y

• ($e )ile:ou%bound&endpoin%L elemen% con%ains a mule&xml:xml&pre%%yprin%er&%rans)ormerL elemen%($e pre%%y&prin%er %rans)ormer )orma%s %$e M- s%rin# %$a% is @ri%%en %o %$e )ile o%$er@ise%$e M- @ill end up on one sin#le ro@

%he 0'ath Expression($is sec%ion con%ains a very brie) explana%ion o) %$e *a%$ expression used in %$e above Mulecon)i#ura%ion )iles ($e expression loo/s li/e %$is:

/ivan:PhoneBook/ivan:Person[not(@category=following-sibling::ivan:Person/@category)]

($is expression selec%s elemen%s in an M- documen% %$a% )ills %$e )ollo@in# cri%eria:• <as a paren% elemen% *$one>oo/L belon#in# %o %$e namespace @i%$ %$e namespace pre)ix

FivanG• Is named *ersonL and belon#s %o %$e namespace @i%$ %$e namespace pre)ix FivanG• <as a categor$ a%%ribu%e @$ic$ value is no% %$e same as any *ersonL elemen%s in %$e

namespace @i%$ %$e namespace pre)ix FivanG %$a% $as %$e same *$one>oo/L elemen% as paren% and %$a% comes a)%er %$e *ersonL elemen% curren%ly bein# evalua%ed

Expressed in plain @ords, %$e aim o) %$e *a%$ expression is %o )ind a person in a p$one&boo/ %$a% belon#s %o ca%e#ory in @$ic$ %$a% person is alone Hsli#$%ly simpli)ied

! 7

Page 107: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 107/354

, ' +un the Example Programe are no@ ready %o %ry %$e example pro#ram ou% by s%ar%in# %$e appropria%e Mule con)i#ura%ion

)ile, copyin# %$e inpu% )ile %o %$e inpu%&direc%ory and %$en observin# %$e resul% produced in %$eou%pu% direc%ory

$en runnin# %$e example pro#ram, @e'll use %$e %ec$ni0ue o) runnin# %$e Mule con)i#ura%ion )ilesas described in an earlier exampleI% is assumed %$a% %$e pro4ec% is con)i#ured @i%$ %$e Mule 3 x dis%ribu%ion on %$e classpa%$ @$ens%ar%in# %$is sec%ion

• In %$e *ac/a#e or *ro4ec% Explorer in Eclipse, ri#$%&clic/ %$e Fmule3&xpa%$ xmlG )ile andselec% Run As &L Mule erver

• ($e Mule 3 x server s$ould s%ar% up and display console ou%pu% indica%in# a success)uls%ar%up

• Re)res$ %$e FMule *a%$*rocessin#G pro4ec% in Eclipse 9o%e %$a% %$ere is one ne@ direc%ory, F)ile&inpu%&direc%oryG, crea%ed by Mule

• In %$e Eclipse pac/a#e bro@ser, copy %$e )ile F*$one>oo/ xmlG and pas%e i% on%o %$e F)ile&inpu%&direc%oryG

ome lo# ou%pu% s$ould be #enera%ed by Mule• Re)res$ %$e FMule *a%$*rocessin#G pro4ec% in Eclipse

9o%e %$a% %$ere is one ne@ direc%ory, Fprocessed&xpa%$G, crea%ed by Mule• In %$e Eclipse pac/a#e explorer, expand %$e Fprocessed&xpa%$G direc%ory and open %$e )ile

F*$one>oo/ xmlG loca%ed in %$a% direc%oryI% s$ould $ave %$e )ollo@in# con%en% Hsome )orma%%in# $as been applied :

<?xml version="1.0" encoding="UTF-8"?>

<ivan:Person xmlns:ivan="http://www.ivan.com/schemas/addressbook" category="athlete" lastUpdate="2011-07-22"> <ivan:FirstName>J-O</ivan:FirstName> <ivan:LastName>Waldner</ivan:LastName> <ivan:Phone>08-12312312</ivan:Phone></ivan:Person>

• ($e roo% elemen% o) %$e processed M- )ile is ivan:*ersonLIndeed, %$is person is %$e only a%$le%e in our p$one&boo/

• ($e namespace $as been preserved correc%ly• In %$e Eclipse pac/a#e bro@ser selec% ano%$er M- )ile Ha Mule con)i#ura%ion )ile is )ine

and copy i% %o %$e F)ile&inpu%&direc%oryG• Re)res$ %$e pro4ec% in Eclipse• 1pen %$e ne@ )ile in %$e Fprocessed&xpa%$G direc%ory

I% s$ould $ave %$e )ollo@in# con%en%s: # # sr##org.mule.transport.NullPayload1#L5U # # #xp

• -oo/ a% %$e Mule A*I documen%a%ion )or %$e class 1ullPa$load e can see %$a% ins%ances o) %$is class represen% a Mule messa#e @i%$ emp%y Hnull payload

($is is unders%andable, since %$e *a%$ expression did no% )ind any ma%c$in# nodes in %$esecond )ile

!

Page 108: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 108/354

Run %$e Mule 2 x version o) %$e example pro#ram:• "$an#e %$e Mule dis%ribu%ion %o Mule 2 x, as described inappendix >• .ele%e %$e %@o direc%ories crea%ed by Mule and %$eir con%en%s

($a% is, %$e F)ile&inpu%&direc%oryG and %$e Fprocessed&xpa%$G direc%ories• %ar% %$e example pro#ram by ri#$%&clic/in# %$e Fmule2&xpa%$ xmlG )ile and selec% Run As&

LMule erver

($e resul% o) runnin# %$e Mule 2 x version s$ould be %$e same as @i%$ %$e Mule 3 x version

, * Exercises($e example pro#ram is comple%ed, bu% you are encoura#ed %o some experimen%a%ion omesu##es%ions )or exercises %o under%a/e on your o@n are:

• Remove %$e M- pre%%y&prin%er %rans)ormer• Remove %$e M-&%o&.1M %rans)ormer• "$an#e %$e resul%(ype o) %$e *a%$ %rans)ormer

Recall %$a% %$e possible values are (RI9C, 9BM>ER, >11-EA9, 91.E and 91.E E(

• Modi)y %$e *a%$ expression

($is concludes %$is c$ap%er

! ;

Page 109: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 109/354

. Monitoring MuleIns%ances o) Mule 2 x and 3 x can be moni%ored, and %o some ex%end con%rolled, usin# ?M H%$e?ava Mana#emen% e %ension In %$is c$ap%er @e @ill loo/ a% $o@ %o con)i#ure ?M mana#emen% ina Mule ins%ance, some examples o) @$a% can be learned usin# %$e ?M mana#emen% and someexamples o) $o@ @e can con%rol a Mule ins%ance usin# ?M

e @ill also loo/ a% %@o al%erna%ives re#ardin# ?M moni%orin# o) a Mule ins%ance & ?"onsole and%$e M 5? ?M @eb mana#emen% console

$ile %$is c$ap%er @ill )oremos% discuss moni%orin# and mana#emen% o) Mule ins%ances usin# ?M ,@e need a Mule ins%ance %o moni%or and mana#e 6or %$is purpose, @e @ill crea%e a small example

pro#ram %$a%, usin# ei%$er Mule 2 x or Mule 3 x, exposes a 1A* @eb service

2 1 Create the Pro!ect"rea%e %$e pro4ec% as described in %$e appendix"rea%e a Mule *ro4ec%, namin# i% FMoni%orin#MuleG($e Mule 3 $o% deploymen% can be s@i%c$ed o)) )rom %$e s%ar%, as %$is )ea%ure is no% o) use @$en

runnin# Mule embedded In addi%ion %o %$e basic pro4ec% se%up, @e @ill per)orm some addi%ionalse%up )or %$is par%icular pro4ec%:

• In %$e roo% o) %$e source code $ierarc$y, crea%e a pac/a#e namedcom.ivan.muleconfig ($is pac/a#e is %o con%ain %$e Mule 2 x and Mule 3 x con)i#ura%ion )iles o) %$is example

• A#ain in %$e roo% o) %$e source code $ierarc$y, crea%e a pac/a#e named com.ivan.services($is pac/a#e @ill con%ain %$e implemen%a%ion o) %$e 1A* @eb service

2 " Create the #eb Ser$ice %mplementation Class($e @eb service endpoin% implemen%a%ion class implemen%s a ?A & service %$a% ex%ends

#ree%in#s ($is class is common )or bo%$ %$e Mule 2 x and Mule 3 x versions o) %$e example pro#ram

• In %$e com ivan services pac/a#e, crea%e a class named <ello ervice implemen%ed as)ollo@s:

package com.ivan.services;

import java.util.Date;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;

/** * The old faithful Hello service implemented as a JAX-WS service. *

* @author Ivan Krizsan */@WebService(serviceName="HelloService")public class HelloService{ @WebResult(name="message", partName="sayHelloResponse") public String sayHello( @WebParam(name="name", partName="sayHelloRequest") final String inName) { String theMessage = "Hello, " + inName + ". The time is now: " + (new Date()); System.out.println("*** Message: " + theMessage); return theMessage; }}

! +

Page 110: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 110/354

2 & Create the (ule Configuration )iles($ere are %@o Mule con)i#ura%ion )iles, one )or eac$ Mule version Excep% )or one par%icularelemen%, %$e con)i#ura%ion )iles only con%ain con)i#ura%ion elemen%s %$a% $ave been discussed in

previous examples ($e con)i#ura%ion elemen% @e $aven'% seen be)ore is %$e elemen% used %ocon)i#ure ?M mana#emen% o) a Mule ins%ance>u% )irs%, le%'s %a/e a loo/ a% %$e %@o )iles and %$en discuss %$e ne@ elemen%

• In %$e pac/a#e com.ivan.muleconfig , crea%e a )ile named Fmule&con)i#2 xmlG %$a% con%ains%$e )ollo@in# Mule 2 x con)i#ura%ion:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:management="http://www.mulesource.org/schema/mule/management/2.2" xmlns:cxf="http://www.mulesource.org/schema/mule/cxf/2.2" xsi:schemaLocation=" http://www.mulesource.org/schema/mule/management/2.2 http://www.mulesource.org/schema/mule/management/2.2/mule-management.xsd http://www.mulesource.org/schema/mule/cxf/2.2

http://www.mulesource.org/schema/mule/cxf/2.2/mule-cxf.xsd http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd">

<!-- Enable JMX management and the MX4J web console. --> <management:jmx-default-config registerMx4jAdapter="true"/>

<model name="HelloModel"> <service name="HelloService"> <inbound> <cxf:inbound-endpoint address="http://localhost:8081/services/HelloService"/> </inbound> <component> <prototype-object class="com.ivan.services.HelloService"/> </component> </service> </model></mule>

• In %$e same pac/a#e, com.ivan.muleconfig , crea%e a )ile named Fmule&con)i#3 xmlG %$a%con%ains %$e )ollo@in# Mule 3 x con)i#ura%ion:

<?xml version="1.0" encoding="UTF-8"?><mule

xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns:management="http://www.mulesoft.org/schema/mule/management" xsi:schemaLocation="

http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/3.2/mule-cxf.xsd

http://www.mulesoft.org/schema/mule/management http://www.mulesoft.org/schema/mule/management/3.2/mule-management.xsd

http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd">

<!-- Enable JMX management and the MX4J web console. --> <management:jmx-default-config registerMx4jAdapter="true"/>

<flow name="HelloFlow"> <inbound-endpoint address="http://localhost:8081/services/HelloService"/> <cxf:jaxws-service serviceClass="com.ivan.services.HelloService"/> <component class="com.ivan.services.HelloService"/> </flow></mule>

!!

Page 111: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 111/354

Page 112: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 112/354

Page 113: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 113/354

HFA#en%s Runnin#G• Amon# %$e runnin# a#en%s, %$ere is a ?M a#en%

In my case, %$e ?M a#en% can be )ound a%:service:4mx:rmi:===4ndi=rmi:==local$os%:! ++=server

• Also amon# %$e runnin# a#en%s, %$ere is an en%ry )or %$e M 5? <((* adap%or

($is is %$e BR- a% @$ic$ @e can )ind %$e M 5? @eb console @e @ill loo/ a% s$or%lyIn my case, %$e BR- is: $%%p:==local$os%:++++• ($ere are o%$er en%ries in %$e lis% o) runnin# a#en%s, bu% in %$is example @e @ill only use %$e

%@o men%ioned above

Run the Mule 3.x Exa&ple 'rogra&

(o run %$e Mule 3 x version o) %$e example pro#ram, c$an#e %$e Mule run%ime o) %$e pro4ec% %o %$eMule 3 x run%ime, as described in %$is sec%ion o) appendix >

• In %$e *ac/a#e or *ro4ec% Explorer, ri#$%&clic/ %$e Fmule&con)i#3 xmlG )ile and selec% Run

As &L Mule erver• ($e Mule 3 x server s$ould s%ar% up and display some%$in# similar %o %$is on %$e console

Hsome ou%pu% omi%%ed %o conserve space :...INFO 2011-10-18 06:51:52,420 [main] org.mule.module.management.agent.JmxAgent:Attempting to register service with name: Mule.e148024f-f944-11e0-ae52-6f64b9e3e0f2:type=Endpoint,service="HelloFlow",connector=connector.http.mule.default,name="endpoint.http.localhost.8081.services.HelloService"INFO 2011-10-18 06:51:52,420 [main] org.mule.module.management.agent.JmxAgent:Registered Endpoint Service with name: Mule.e148024f-f944-11e0-ae52-6f64b9e3e0f2:type=Endpoint,service="HelloFlow",connector=connector.http.mule.default,name="endpoint.http.localhost.8081.services.HelloService"INFO 2011-10-18 06:51:52,423 [main] org.mule.module.management.agent.JmxAgent:Registered Connector Service with name Mule.e148024f-f944-11e0-ae52-6f64b9e3e0f2:type=Connector,name="connector.http.mule.default.1"INFO 2011-10-18 06:51:52,436 [main] org.mule.DefaultMuleContext:*********************************************************************** Mule ESB and Integration Platform ** Version: 3.2.0 Build: 22917 ** MuleSoft, Inc. ** For more information go to http://www.mulesoft.org ** ** Server started: 10/18/11 6:51 AM ** Server ID: e148024f-f944-11e0-ae52-6f64b9e3e0f2 ** JDK: 1.6.0_24 (mixed mode) ** OS encoding: UTF-8, Mule encoding: UTF-8 ** OS: Mac OS X (10.6.6, x86_64) ** Host: Bo5b.local (127.0.0.1) ** ** Agents Running: *

* Rmi Registry: rmi://localhost:1099 ** Jmx Notification Agent (Listener MBean registered) ** JMX Log4J Agent ** jmx-agent: service:jmx:rmi:///jndi/rmi://localhost:1099/server ** MX4J Http adaptor: http://localhost:9999 ** Default Jmx Support Agent ***********************************************************************

9o%e %$a%:• ($e <ello6lo@ @i%$ %$e <ello ervice @as success)ully s%ar%ed

($e .- o) %$e service can be )ound a% $%%p:==local$os%:; ;!=services=<ello ervice @sdl Veri)y %$a% %$ere indeed is a .- a% %$is BR- in a bro@ser

• -as% in %$e )ramed in)orma%ion abou% %$e Mule server, %$ere is a lis%in# o) runnin# a#en%s

!!3

Page 114: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 114/354

HFA#en%s Runnin#G• Amon# %$e runnin# a#en%s, %$ere is a ?M a#en%

In my case, %$e ?M a#en% can be )ound a%:service:4mx:rmi:===4ndi=rmi:==local$os%:! ++=server

• Also amon# %$e runnin# a#en%s, %$ere is an en%ry )or %$e M 5? <((* adap%or

($is is %$e BR- a% @$ic$ @e can )ind %$e M 5? @eb console @e @ill loo/ a% s$or%lyIn my case, %$e BR- is: $%%p:==local$os%:++++• ($ere are o%$er en%ries in %$e lis% o) runnin# a#en%s, bu% in %$is example @e @ill only use %$e

?M a#en% and %$e M 5? a#en%

%est the Running Exa&ple 'rogra&

($is par% is common )or bo%$ %$e Mule 2 x and Mule 3 x versions o) %$e example pro#ram• Bsin# soapBI, send a% leas% one re0ues% %o %$e runnin# <ello ervice

($e de%ails on $o@ %o do %$is is le)% as an exercise %o %$e reader• .o no% s%op %$e example pro#ram

e $ave s%ar%ed a Mule ins%ance %$a% exposes a 1A* @eb service and i% does loo/ li/e %$ere aresome a#en%s %$a% $ave been s%ar%ed due %o our a%%emp% a% enablin# ?M con)i#ura%ion, bu% some $ard

proo) @ould be nice

!!5

Page 115: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 115/354

2 * (anaging a (ule %nstance 6sing 7(5i%$ an ins%ance, be i% %$e Mule 2 x or Mule 3 x version, o) %$is c$ap%er's example pro#ram runnin#,

@e are ready %o %a/e a loo/ a% %$e op%ions available @$en mana#in# %$e Mule ins%ance($is sec%ion include par%s speci)ic %o Mule 2 x and Mule 3 x

Run -Console

?"onsole is a pro#ram )or ?M mana#emen% and is par% o) all recen% ?ava run%imesJ i% is available in%$e ?ava E 7 .K used %$rou#$ou% %$is boo/

• In a %erminal @indo@ or e0uivalen%, issue %$e commandjconsole

• $en ?"onsole s%ar%s, i% @ill as/ )or %$e parame%ers o) a ne@ connec%ionelec% %$e Remo%e *rocess radiobu%%on and inpu% %$e ?M a#en% address

Fservice:4mx:rmi:===4ndi=rmi:==local$os%:! ++=serverG ($e exac% address may vary, so pleasec$ec/ %$e console ou%pu% #enera%ed @$en s%ar%in# %$e Mule example pro#ram

?"onsole ne@ connec%ion dialo# @i%$ %$e address %o %$e Mule ?M a#en% en%ered

• "lic/ %$e "onnec% bu%%on?"onsole need some %ime %o es%ablis$ %$e connec%ion, so be pa%ien%

!!8

Page 116: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 116/354

• $en ?"onsole $as es%ablis$ed %$e connec%ion, a @indo@ @i%$ %$e %i%le same as %$e ?Ma#en% address en%ered s$ould be opened, li/e in %$e pic%ure belo@

?"onsole a)%er $avin# es%ablis$ed a connec%ion %o a Mule server

9o%e %$a%:•

($ere is a domain in %$e le)% panel @$ic$ name s%ar%s @i%$ FMuleG• ($ere is a plu##ed&in connec%or symbol in %$e upper ri#$% corner o) %$e @indo@

($e connec%or symbol s$o@s %$e connec%ion&s%a%us in %$is case ?"onsole is connec%ed %o%$e ?M service in %$e Mule ins%ance

e $ave no@ connec%ed %o a runnin# Mule server @i%$ ?"onsoleIn %$e nex% sec%ion @e @ill loo/ a% some o) %$e moni%orin# op%ions available )or Mule 2 x and in asubse0uen% sec%ion, moni%orin# op%ions available )or Mule 3 x

9o%e %$a% mos% o) %$e mana#emen% op%ions available )or Mule 2 x are also available )or Mule 3 x.i))erences @ill be discussed in a subse0uen% sec%ion on Mule 3 x ?M mana#emen%

4enerate So&e Statistics

(o see some numbers in %$e mana#emen% and moni%orin# da%a @e are abou% %o loo/ a%, use soapBI%o send some re0ues%s %o %$e <ello ervice Bsin# %$e -oad (es% )ea%ure i) you @an% %o #enera%e alar#er number o) re0ues%s

Dou are no@ ready %o loo/ a% %$e ?M mana#emen% op%ions available in Mule, de%ails on @$ic$ can be )ound in %$e Mule ?M Mana#emen% sec%ion in %$e re)erence par% o) %$is boo/

!!7

Page 117: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 117/354

Starting and Stopping a Mule )nstance 5sing -M0

(o convince ourselves %$a% @e indeed are able %o mana#e %$e Mule ins%ance )rom ?"onsole, %ry %$e)ollo@in#:

• In a bro@ser, open %$e BR- $%%p:==local$os%:; ;!=services=<ello ervice

($e resul% s$ould be %$e M- o) a 1A* )aul% messa#e appearin# in %$e bro@ser @indo@

($e resul% o) accessin# %$e <ello ervice endpoin% BR- )rom a bro@ser a 1A* )aul%

• In ?"onsole, in %$e or# mule "onnec%or node, #o %o %$e Fconnec%or cx) !G node and %$en %o%$e 1pera%ions node and clic/ %$e s%op"onnec%or bu%%onA dialo# s$ould appear sayin# %$a% %$e me%$od @as success)ully invo/ed

• A#ain, in a bro@ser, access %$e BR- $%%p:==local$os%:; ;!=services=<ello ervice ($is %ime %$ere s$ould be no 1A* )aul%, 4us% an emp%y @indo@

• >ac/ %o ?"onsole, in %$e or# mule "onnec%or node, #o %o %$e Fconnec%or cx) !G node and

%$en %o %$e 1pera%ions node and clic/ %$e s%ar%"onnec%or bu%%onA dialo# s$ould appear sayin# %$a% %$e me%$od @as success)ully invo/ed• 6inally, in a bro@ser, open %$e BR- $%%p:==local$os%:; ;!=services=<ello ervice

($e resul% s$ould a#ain be %$e M- o) a 1A* )aul% messa#e

e see %$a% @e @ere able %o s%ar% and s%op a connec%or in %$e runnin# Mule ins%ance

!!

Page 118: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 118/354

2 , (5'7 an/ (uleAs men%ioned earlier in %$is c$ap%er, %$ere is a% leas% one al%erna%ive %o usin# ?"onsole: M 5?M 5? is a @eb&based ?M mana#emen% console %$a% is embedded in Mule ($is is an al%erna%ive%$a% can come in $andy @$en i% is no% possible %o use ?"onsole )or one reason or ano%$erRecall %$e line in %$e lo# ou%pu%, #enera%ed @$en s%ar%in# %$e example pro#ram o) %$is c$ap%er,con%ainin# FM 5? <%%p adap%orG ($e BR- a)%er %$is %ex%, in my case$%%p:==local$os%:++++, is %$eBR- %o %$e M 5? @eb console

I) @e open %$is BR- in a @eb bro@ser, @e @ill see %$e s%ar% pa#e 1n %$e erver Vie@ pa#e %$ere isa lis% o) %$e di))eren% domains, similar %o %$e le)% panel in ?"onsole's M>eans vie@

M 5? Mule ?M @eb&based mana#emen% console, erver Vie@

e can see %$a% %$ere is a domain @$ic$ name s%ar%s @i%$ FMuleG, similar %o @$a% @e sa@ in?"onsole I) @e clic/ an M>ean in %$is domain, )or example %$e <ello6lo@ M>ean under %$e 6lo@node, @e are %a/en %o %$e M>ean Vie@:

M 5? Mule ?M @eb&based mana#emen% console, M>ean Vie@In %$e M>ean vie@, @e can vie@ a%%ribu%es, c$an#e @ri%eable a%%ribu%es and invo/e any opera%ionsavailable on %$e M>ean

($ese are %$e basic op%ions available in %$e M 5? Mule ?M mana#emen% console Explora%ion o)

addi%ional )ea%ures are le)% as an exercise )or %$e reader

!!;

Page 119: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 119/354

2 2 (onitoring (ule in #eb -pplicationsI% is possible %o moni%or Mule @eb applica%ions runnin# in a @eb or applica%ion con%ainer as @e sa@in %$e c$ap%er onMule in eb Applica%ions (o do %$is @e use %$e mana#emen%:4mx&de)aul%&con)i#L elemen% in %$e same @ay as @e $ave seen in %$is c$ap%er

I) @e connec% ?"onsole %o a (omca% ins%ance %$a% con%ains %@o @eb applica%ions %$a% bo%$ con%ainMule con)i#ura%ion )iles, @e can see %$e )ollo@in#:

Examinin# a (omca% server @i%$ %@o @eb applica%ions %$a% use Mule in ?"onsole

9o%e %$a%:• Eac$ @eb applica%ion $as i%s o@n se% o) Mule&rela%ed M>eans• 6or eac$ @eb applica%ion, %$ere is a Mule con%ex%

($is concludes %$e c$ap%er on Mule moni%orin# and mana#emen%

!!+

Page 120: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 120/354

6. Mule "otificationsMule suppor%s an implemen%a%ion o) %$e1bserver desi#n pa%%ern, enablin# no%i)ica%ions %o be sen% %olis%eners @$en cer%ain even%s occur Examples o) even%s are an endpoin% receivin# or sendin# amessa#e, c$an#es in %$e s%a%e o) a Mule model, an excep%ion @as %$ro@n e%cI% is also possible %o implemen% cus%om lis%eners @$ic$ receives cus%om even%s

9o%i)ica%ions can be con)i#ured ei%$er in %$e Mule con)i#ura%ion )ile or pro#ramma%ically ($eno%i)ica%ion con)i#ura%ion can be s%a%ic, meanin# %$a% i% does no% c$an#e a)%er %$e Mule con%ex% $as

been s%ar%ed, or even dynamic, allo@in# )or lis%eners %o be re#is%ered a)%er %$e Mule con%ex% $as been s%ar%ed

In %$is c$ap%er @e @ill crea%e a Mule applica%ion %$a% con%ains a no%i)ica%ion lis%ener %$a% receivesno%i)ica%ions @$en a @eb service endpoin% receives re0ues%s and sends responses

e @ill also loo/ a% %$e di))eren% %ypes o) no%i)ica%ion lis%eners available in Mule

8 1 Create the Pro!ect"rea%e %$e pro4ec% as described in %$e appendix"rea%e a Mule *ro4ec%, namin# i%FMule9o%i)ica%ionsG ($e Mule 3 $o% deploymen% can be s@i%c$ed o)) )rom %$e s%ar%, as %$is )ea%ure@ill no% be used

8 " Create the Ser$ice %mplementation Classince @e are #oin# %o expose a @eb service endpoin%, @e need a service implemen%a%ion class ($e

implemen%a%ion is similar %o @$a% @e sa@ inc$ap%er one, so no )ur%$er commen%s @ill be made• In %$e pac/a#e com.ivan.mule.service , crea%e %$e HelloService class implemen%ed as )ollo@s:

package com.ivan.mule.services;

import java.util.Date;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;

/** * SOAP web service endpoint implementation class that implements * a service that extends greetings. */@WebServicepublic class HelloService{ @WebResult(name="greeting") public String greet(@WebParam(name="name") String inName) {

return "Hello " + inName + ", the time is now " + new Date(); }}

!2

Page 121: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 121/354

8 & Create the 9otification Listeners 9o%i)ica%ion lis%eners are similar %o observers in %$e 1bserver desi#n pa%%ern 9o%i)ica%ion lis%enersare, $o@ever, more s%ron#ly coupled %o %$e /ind o) even% %$a% %$ey lis%en %o($e no%i)ica%ion lis%eners o) %$is example are implemen%ed in %$ree di))eren% classesJ one abs%rac%

base class implemen%in# common be$aviour, %$e Mule 2 x and %$e Mule 3 x no%i)ica%ion lis%ener>o%$ %$e Mule 2 x and Mule 3 x no%i)ica%ion lis%eners expec% %o receive %$e same /ind o)no%i)ica%ionsJ no%i)ica%ions crea%ed @$en an endpoin% sends or receives a messa#e

9o%e %$a% %$e Mule 2 x no%i)ica%ion lis%ener @ill no% compile properly @$en %$e Mule 3 x librariesare on %$e classpa%$ and vice versa *lease i#nore %$ese errors @$en developin# %$e example

Create the Co&&on "otification $istener ase Class

($e common no%i)ica%ion lis%ener base class implemen%s %$e )ollo@in#, common %o bo%$ %$eno%i)ica%ion lis%eners in %$is example:

• -o##in# o) no%i)ica%ion lis%ener ins%ance crea%ion• %orin# an uni0ue ins%ance iden%i)ierJ an in%e#er number

($is iden%i)ier is used @$en lo##in# %o %$e console, in order )or us %o be able %o %ell %$econsole ou%pu% )rom %$e lis%eners apar%

• -o##in# o) no%i)ica%ion proper%ies• -o##in# o) messa#e payload

e are no@ ready %o crea%e %$e no%i)ica%ion lis%ener base class• In %$e pac/a#e com.ivan.mule.notificationlisteners , crea%e %$e

M$AbstractMsg1otificationListener class implemen%ed li/e %$is:package com.ivan.mule.notificationlisteners;

import org.mule.DefaultMuleMessage;import org.mule.api.MuleMessage;import org.mule.api.context.notification.ServerNotification;import org.mule.api.endpoint.ImmutableEndpoint;import org.mule.context.notification.EndpointMessageNotification;

/** * Abstract message notification listener implementing common * properties of such listeners. *

* @author Ivan Krizsan */public abstract class MyAbstractMsgNotificationListener{

protected static int sCurrentInstanceNumber = 1; protected int mInstanceNumber = sCurrentInstanceNumber++;

/** * Default constructor. * Logs creation of instances of the listener. */ public MyAbstractMsgNotificationListener() { System.out.println("\n*** MyMsgNotificationListener " + mInstanceNumber + " instance created."); }

/** * Logs the Mule message payload from the supplied message. If the * supplied object is not a Mule message, a message stating this * is logged. *

!2!

Page 122: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 122/354

* @param inNotificationSource Notification source, which is the * message received/sent by the endpoint. */ protected void logMessagePayload(final Object inNotificationSource) { if (inNotificationSource instanceof DefaultMuleMessage) { MuleMessage theMessage = (DefaultMuleMessage)inNotificationSource; try

{ System.out.println(" Message payload object: " + theMessage.getPayload()); } catch (Exception theException) { theException.printStackTrace(); } } else { System.out.println(" Notification source is not a Mule message!"); System.out.println(" Notification source type: " + inNotificationSource.getClass()); } }

/** * Logs an action description based on the supplied action code. * The action name is available in the notification, the following * code shows how to use the action code. *

* @param inActionCode Action code used to find action * description. */ protected void logActionDescription(final int inActionCode) { String theActionName;

switch (inActionCode) { case EndpointMessageNotification.MESSAGE_RECEIVED: theActionName = "Message received"; break; case EndpointMessageNotification.MESSAGE_DISPATCHED: theActionName = "Message dispatched"; break; case EndpointMessageNotification.MESSAGE_SENT: theActionName = "Message sent"; break; /* * EndpointMessageNotification.MESSAGE_RESPONSE constant is * only defined in Mule 3.x. */ case 805: theActionName = "Responded to message"; break; default: theActionName = "Unknown action: " + inActionCode; break; } System.out.println(" Action description: " + theActionName); }

/** * Retrieves the immutable endpoint from the supplied endpoint * message notification. This is the only difference between Mule * 2.x and Mule 3.x, so it has been extracted to this method. *

* @param inNotification Notification from which to retrieve * immutable endpoint. * @return Immutable endpoint from notification. */ abstract protected ImmutableEndpoint retrieveNotificationMutableEndpoint( final ServerNotification inNotification);

/** * Processes notifications. *

* @param inNotification Notification to process.

!22

Page 123: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 123/354

*/ protected void myOnNotification(final ServerNotification inNotification) { System.out.println("\n*** MyMsgNotificationListener " + mInstanceNumber + " received a notification:");

/* * Extract properties from the notification and the associated message. */

String theActionName = inNotification.getActionName(); int theActionCode = inNotification.getAction(); String theNotificationType = inNotification.getClass().getName(); Object theNotificationSource = inNotification.getSource(); ImmutableEndpoint theEndpoint = retrieveNotificationMutableEndpoint(inNotification); String theEndpointConnector = theEndpoint.getConnector().getName(); String theResourceIdentifier = inNotification.getResourceIdentifier();

System.out.println(" Notification type: " + theNotificationType); System.out.println(" Action name: " + theActionName); System.out.println(" Action code: " + theActionCode); System.out.println(" Notification source type: " + theNotificationSource.getClass()); System.out.println(" Endpoint connector: " + theEndpointConnector); System.out.println(" Resource identifier: " + theResourceIdentifier);

logActionDescription(theActionCode); logMessagePayload(theNotificationSource); }}

9o%e %$a%:• ($e class does no% implemen% any par%icular in%er)ace or in$eri% )rom any par%icular class

As @e @ill see la%er, Mule 2 x and Mule 3 x no%i)ica%ion lis%eners di))er in %$a% Mule 3 xno%i)ica%ion lis%ener in%er)aces uses #enerics ($us i% is no% possible %o $ave %$e superclassimplemen% one sin#le in%er)ace coverin# bo%$ cases

($e logMessagePa$load me%$od %a/es a ?ava ob4ec% as parame%er and, i) %$e ob4ec% is a Mulemessa#e, a%%emp%s %o re%rieve and lo# %$e messa#e payload• ($e logAction+escription me%$od )inds a s%rin# describin# %$e ac%ion o) %$e endpoin% )rom

an in%e#er con%ainin# an ac%ion code($e no%i)ica%ion ob4ec% already con%ains a me%$od, getAction1ame , )rom @$ic$ %$e name o)%$e ac%ion may be ob%ained ($elogAction+escription me%$od is $ere %o s$o@ $o@ %o @or/@i%$ ac%ion codes )rom no%i)ica%ions usin# cons%an%s

• In %$elogAction+escription me%$od, %$ere is a case )or %$e in%e#er ; 8In Mule 3 x %$ere is a cons%an%, &ndpointMessage1otification.M&SSA5&6#&SPO1S& , )or%$is number ($is cons%an% is no% available in Mule 2 x

• ($ere is a me%$od named retrieve1otificationMutable&ndpoint %$a% re%urns an Immutable&ndpoint ob4ec%($e names o) %$e me%$ods )rom @$ic$ %o re%rieve an Immutable&ndpoint )rom no%i)ica%ionob4ec%s are di))eren% in Mule 2 x and Mule 3 x, as @e @ill see la%er ($is call @as re)ac%oredin%o a separa%e me%$od %$a% subclasses mus% implemen%, in order %o avoid unnecessary codeduplica%ion

• ($e me%$od m$On1otification re%rieves and lo#s a number o) proper%ies )rom %$eno%i)ica%ion ob4ec%A#ain, %$e )ac% %$a% %$e Mule 3 x no%i)ica%ion lis%ener in%er)ace uses #enerics preven% us )romimplemen%in# a sin#le on1otification me%$od %$a% can be used by bo%$ c$ild classes

!23

Page 124: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 124/354

Create the Mule 2.x "otification $istener

i%$ %$e common no%i)ica%ion lis%ener superclass in place, implemen%in# %$e version&speci)ic c$ildclasses becomes almos% %rivial 6irs% up is %$e Mule 2 x no%i)ica%ion lis%ener:

• In %$e pac/a#e com.ivan.mule.notificationlisteners , crea%e %$e M$Msg1otificationListener%class implemen%ed in %$e )ollo@in# manner:

package com.ivan.mule.notificationlisteners;

import org.mule.api.context.notification.EndpointMessageNotificationListener;import org.mule.api.context.notification.ServerNotification;import org.mule.api.endpoint.ImmutableEndpoint;import org.mule.context.notification.EndpointMessageNotification;

/** * A notification listener that is notified when an endpoint sends or * receives a message. Mule 2.x version. * The onNotification method specified by the notification listener * interface this class implements is implemented in the superclass. *

* @author Ivan A Krizsan */public class MyMsgNotificationListener2 extends

MyAbstractMsgNotificationListener implements EndpointMessageNotificationListener{ @Override protected ImmutableEndpoint retrieveNotificationMutableEndpoint( final ServerNotification inNotification) { /*

* Mule 2.x way of retrieving the endpoint object from * the notification. */ return ((EndpointMessageNotification)inNotification).getEndpoint(); }

/** * Processes notifications. * Need an onNotification method specific to the version of Mule used, * since Mule 3.x uses generics in the interface declaration while * Mule 2.x does not. *

* @param inNotification Notification to process. */ @Override public void onNotification(final ServerNotification inNotification) { myOnNotification(inNotification); }}

9o%e %$a%:•

($e no%i)ica%ion lis%ener class ex%ends our abs%rac% no%i)ica%ion lis%ener class, implemen%edearlier, and implemen%s %$e &ndpointMessage1otificationListener in%er)ace($e in%er)ace is c$osen based on %$e /ind o) no%i)ica%ions @e @an% %o receive ($e di))eren%no%i)ica%ion lis%ener in%er)aces available in Mule are described in %$e 9o%i)ica%ion re)erencesec%ion

• ($e me%$od retrieve1otificationMutable&ndpoint uses %$e get&ndpoint me%$od on %$eno%i)ica%ion ob4ec% %o re%rieve an Immutable&ndpoint ob4ec%($is is %$e Mule 2 x @ay o) re%rievin# an endpoin% ob4ec% )rom %$e no%i)ica%ion

• ($e on1otification me%$od %a/es a parame%er o) %$e %ypeServer1otificationServer1otification is a common superclass )or a number o) no%i)ica%ions %$a% represen%some%$in# $avin# $appened in %$e Mule server 6or de%ails, please re)er %o %$e Mule A*I

!25

Page 125: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 125/354

?avadoc documen%a%ion• ($e on1otification me%$od invo/es %$em$On1otification me%$od in %$e superclass

i%$ Mule 2 x, %$ere is no need %o cas% %$e no%i)ica%ion ob4ec%, as %$eon1otification me%$odreceives a Server1otification ob4ec% as parame%er

Create the Mule 3.x "otification $istener ($e Mule 3 x no%i)ica%ion lis%ener class only di))ers sli#$%ly )rom %$e Mule 2 x no%i)ica%ion lis%ener

• In %$e pac/a#e com.ivan.mule.notificationlisteners , crea%e %$e M$Msg1otificationListener)class Implemen% i% li/e %$is:

package com.ivan.mule.notificationlisteners;

import org.mule.api.context.notification.EndpointMessageNotificationListener;import org.mule.api.context.notification.ServerNotification;import org.mule.api.endpoint.ImmutableEndpoint;import org.mule.context.notification.EndpointMessageNotification;

/** * A notification listener that is notified when an endpoint sends or * receives a message. Mule 3.x version. * The onNotification method specified by the notification listener * interface this class implements is implemented in the superclass. *

* @author Ivan A Krizsan */public class MyMsgNotificationListener3 extends MyAbstractMsgNotificationListener implements EndpointMessageNotificationListener<EndpointMessageNotification>{ @Override protected ImmutableEndpoint retrieveNotificationMutableEndpoint( final ServerNotification inNotification) { /*

* Mule 3.x way of retrieving the endpoint object from

* the notification. */ return ((EndpointMessageNotification)inNotification).getImmutableEndpoint(); }

/** * Processes notifications. * Need an onNotification method specific to the version of Mule used, * since Mule 3.x uses generics in the interface declaration while * Mule 2.x does not. *

* @param inNotification Notification to process. */ @Override public void onNotification(final EndpointMessageNotification inNotification) { myOnNotification(inNotification); }}

9o%e %$a%:• ($e no%i)ica%ion lis%ener class ex%ends our abs%rac% no%i)ica%ion lis%ener class, implemen%ed

earlier, and implemen%s %$e &ndpointMessage1otificationListener7&ndpointMessage1otification8 in%er)ace($e use o) #enerics allo@s us crea%e an endpoin% no%i)ica%ion lis%ener %$a% only receives a

par%icular %ype, a subclass o) &ndpointMessage1otification , o) no%i)ica%ions "rea%in#cus%om no%i)ica%ion lis%eners is beyond %$e scope o) %$is %u%orial please re)er %o %$e Mule

documen%a%ion )or de%ails

!28

Page 126: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 126/354

($e in%er)ace is c$osen based on %$e /ind o) no%i)ica%ions @e @an% %o receive ($e di))eren%no%i)ica%ion lis%ener in%er)aces available in Mule are described in %$e 9o%i)ica%ion re)erencesec%ion

• ($e me%$od retrieve1otificationMutable&ndpoint uses %$e getImmutable&ndpoint me%$odon %$e no%i)ica%ion ob4ec% %o re%rieve an Immutable&ndpoint ob4ec%($is is %$e Mule 3 x @ay o) re%rievin# an endpoin% ob4ec% )rom %$e no%i)ica%ion

• ($e on1otification me%$od %a/es a parame%er o) %$e %ype &ndpointMessage1otification &ndpointMessage1otification is a speci)ic %ype o) no%i)ica%ion sen% in connec%ion %o anendpoin% receivin# or sendin# a messa#e

• ($e on1otification me%$od invo/es %$em$On1otification me%$od in %$e superclass($ere is no need %o cas% %$e &ndpointMessage1otification ob4ec%, since i% is also aServer1otification ob4ec% due %o in$eri%ance

!27

Page 127: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 127/354

8 ' Create the (ule Configuration )iles($is example uses one Mule con)i#ura%ion )ile per Mule version ($e con)i#ura%ion o) %$eno%i)ica%ion lis%eners, @$ic$ is @$a% is o) in%eres% in %$is example, only di))ers sli#$%ly be%@een %$e%@o Mule versions >o%$ versions o) %$e con)i#ura%ion )iles s$are %$e )ollo@in# s%ruc%ure:

.e)ini%ion o) no%i)ica%ion lis%ener beans($ese are plain prin# beans, implemen%ed by %$e classes @e developed above• peci)ica%ion o) no%i)ica%ions %o receive and no%i)ica%ion lis%eners• .e)ini%ion o) a @eb service endpoin%

($is is %$e endpoin% %$a%, )or %$e cause o) %$e example, receives re0ues%s and sends replies

Create the Mule 2.x Configuration File

($e Mule 2 x con)i#ura%ion )ile is %o be loca%ed in %$e roo% o) %$e source direc%ory and $ave %$ename Fmule&con)i#2 xmlG I% $as %$e )ollo@in# con%en%s:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://www.mulesource.org/schema/mule/cxf/2.2" xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.2" xmlns:spring="http://www.springframework.org/schema/beans"

xsi:schemaLocation=" http://www.mulesource.org/schema/mule/cxf/2.2 http://www.mulesource.org/schema/mule/cxf/2.2/mule-cxf.xsd

http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd

http://www.mulesource.org/schema/mule/stdio/2.2 http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- Notification listeners are Spring beans. The class implementing the bean implements an interface which type depends on the kind of notification we wish to receive. --> <spring:bean name="notificationListener1" class="com.ivan.mule.notificationlisteners.MyMsgNotificationListener2"/> <spring:bean name="notificationListener2" class="com.ivan.mule.notificationlisteners.MyMsgNotificationListener2"/>

<!-- In this global block, not particular to any model or service, we declare which kind of notifications we have listeners for and which the listeners are. The dynamic attribute enables, if set to true, registering for notifications after the Mule context has been started. Default value of the dynamic attribute is false. --> <notifications dynamic="true"> <!-- Enable notifications for the specified event type. The standard event types are: CONTEXT - Mule context started, stopped etc. MODEL - Model lifecycle state changed or components in the model registered or unregistered. SERVICE - Service started, stopped, etc.

SECURITY - Request for authorization occurred. ENDPOINT-MESSAGE - Message received or sent by endpoint.

!2

Page 128: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 128/354

COMPONENT-MESSAGE - Message processed by component. MANAGEMENT - State of Mule instance or its resources changed. CONNECTION - Connector connected to, released connection or failed connection attempt to resource. REGISTRY - Event occurred on the Mule registry. CUSTOM - Custom notification. EXCEPTION - An exception was thrown. TRANSACTION - Transaction begun, committed or rolled back. ROUTING - A routing event occurred.

There are additional attributes available on the <notification> element, allowing for fine-grained control when declaring custom notifications. --> <notification event="ENDPOINT-MESSAGE"/> <!-- Disable notifications for the specified event type. Disabling of notifications has precedence over notification enabling. Note that it is possible to disable notifications of one particular event type for a certain interface. This means the event will be sent to listeners with other interfaces. --> <disable-notification interface="COMPONENT-MESSAGE"/> <!-- If we add subscription="GreetingService" to the notification listener declaration below, the listener will only listen to notifications from the GreetingService. This will result in the listener receiving only notifications from the CXF connector and not the HTTP connector. --> <!-- The first notification listener (notificationListener1) will receive events from all source, while the second (notificationListener2) only will receive events from the resource with the name "GreetingService". --> <notification-listener ref="notificationListener1"/> <notification-listener ref="notificationListener2" subscription="GreetingService"/> </notifications>

<!-- Model with an inbound webservice endpoint. --> <model name="GreetingModel"> <service name="GreetingService"> <inbound> <cxf:inbound-endpoint address="http://localhost:8182/services/GreetingService"/> </inbound>

<component> <prototype-object class="com.ivan.mule.services.HelloService"/> </component>

<outbound> <pass-through-router> <stdio:outbound-endpoint system="OUT"/> </pass-through-router> </outbound> </service> </model></mule>

9o%e %$a%:• ($ere are %@o prin# beans, Fno%i)ica%ion-is%ener!G and Fno%i)ica%ion-is%ener2G, de)ined

usin# %$e same implemen%a%ion classe de)ine %@o no%i)ica%ion lis%eners in order %o apply )il%erin# %o one o) %$em and compare

%$e no%i)ica%ions received by %$e lis%eners• ($ere is a no%i)ica%ionsL elemen% de)ined ou%side o) any models or services

!2;

Page 129: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 129/354

($is is @$ere @e de)ine @$ic$ /inds o) no%i)ica%ions %o lis%en )or, @$ic$ no%i)ica%ions no% %olis%en )or and %$e no%i)ica%ion lis%eners o) %$e applica%ion

• ($e no%i)ica%ionsL elemen% $as an a%%ribu%e namedd$namic 9o%i)ica%ion lis%eners can be re#is%ered in %$e Mule con)i#ura%ion )ile, bu% may also bere#is%ered pro#ramma%ically As per de)aul%, i% is no% possible %o re#is%er no%i)ica%ion lis%enersa)%er %$e Mule con%ex% $as s%ar%ed e%%in# %$ed$namic a%%ribu%e on %$e no%i)ica%ionsLelemen% %o %rue allo@s )or pro#ramma%ic re#is%ra%ion o) no%i)ica%ion lis%eners a)%er %$e Mulecon%ex% $as been s%ar%edIn %$is par%icular example, @e do no% re#is%er any no%i)ica%ion lis%eners pro#ramma%ically($e dynamic a%%ribu%e $as only been included %o $ave an excuse )or describin# %$is )acili%y

• ($ere is a no%i)ica%ionL c$ild elemen% in %$e no%i)ica%ionsL elemen%, @$ic$ loo/s li/e %$is:<notification event="ENDPOINT-MESSAGE"/>($is elemen% %ells Mule %$a% @e @is$ %o receive no%i)ica%ions on endpoin% messa#esJ @$en%$ey are sen% or received ($e di))eren% /inds o) even%s available in Mule are lis%ed in %$e

9o%i)ica%ion sec%ion o) %$e re)erence• ($e nex% elemen% is a disable&no%i)ica%ionL elemen%, @$ic$ loo/s li/e %$is:

<disable-notification interface="COMPONENT-MESSAGE"/>($is elemen% disables %$e sendin# o) no%i)ica%ions )or componen% messa#es 9o%e %$a% @e$ave %o use %$einterface a%%ribu%e %o speci)y %$e /ind o) even% and canno% use %$eeventa%%ribu%e, @$ic$ also is available on %$e elemen% A#ain, %$e di))eren% /inds o) even%savailable in Mule are lis%ed in %$e 9o%i)ica%ion sec%ion o) %$e re)erence

ince %$ere are no componen% messa#e no%i)ica%ion lis%eners declared in %$is example, %$isline is super)luous and is only $ere %o s$o@ %$a% i% is possible %o disable no%i)ica%ions as @ell

• 9ex%, %$ere are %@o no%i)ica%ion&lis%enerL elemen%s($is /ind o) elemen% is used %o re#is%er a no%i)ica%ion lis%ener Bsin# %$eref a%%ribu%e, @especi)y @$ic$ prin# bean is %o be %$e lis%ener

• ($e second no%i)ica%ion&lis%enerL elemen% $as a subscription a%%ribu%eI) no subscription a%%ribu%e is presen%, %$e no%i)ica%ion lis%ener @ill receive no%i)ica%ions o)%$e %ype speci)ied by %$e no%i)ica%ion in%er)aceHs %$e lis%ener implemen% )rom allcomponen%s Bsin# %$e subscription a%%ribu%e, %$e lis%ener @ill only receive no%i)ica%ions)orm %$e componen% @$ic$ name is #iven as %$e value o) %$e a%%ribu%e

e @ill see %$is be$aviour @$en @e run %$e example pro#ram in a @$ile• ($e remainin# par% o) %$e Mule 2 x con)i#ura%ion )ile con%ains a model @$ic$ declares a

1A* @eb service endpoin% 6or de%ails on %$is par%, please re)er %oc$ap%er ! 9o%e, $o@ever, %$e name o) %$e inbound service in %$e model & FCree%in# erviceG, @$ic$ma%c$es %$e name @e used in %$e declara%ion o) one o) %$e no%i)ica%ion lis%eners

!2+

Page 130: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 130/354

Create the Mule 3.x Configuration File

($e Mule 3 x con)i#ura%ion )ile is %o be loca%ed a% %$e same loca%ion as %$e Mule 2 x con)i#ura%ion)ile, %$a% is in %$e roo% o) %$e source direc%ory I% is %o be named Fmule&con)i#3 xmlG and is, %o a lar#eex%en%, iden%ical @i%$ %$e Mule 2 x con)i#ura%ion )ile I% $as %$e )ollo@in# con%en%s:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns:stdio="http://www.mulesoft.org/schema/mule/stdio" xmlns:spring="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd

http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.1/mule.xsd

http://www.mulesoft.org/schema/mule/stdio http://www.mulesoft.org/schema/mule/stdio/current/mule-stdio.xsd

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- Notification listeners are Spring beans. The class implementing the bean implements an interface which type depends on the kind of notification we wish to receive. --> <spring:bean name="notificationListener1" class="com.ivan.mule.notificationlisteners.MyMsgNotificationListener3"/> <spring:bean name="notificationListener2" class="com.ivan.mule.notificationlisteners.MyMsgNotificationListener3"/>

<!-- In this global block, not particular to any model or service, we declare which kind of notifications we have listeners for and which the listeners are. The dynamic attribute enables, if set to true, registering for notifications after the Mule context has been started. Default value of the dynamic attribute is false. --> <notifications dynamic="false"> <!-- Enable notifications for the specified event type. The standard event types are: CONTEXT - Mule context started, stopped etc. MODEL - Model lifecycle state changed or components in the model registered or unregistered. SERVICE - Service started, stopped, etc. SECURITY - Request for authorization occurred. ENDPOINT-MESSAGE - Message received or sent by endpoint.

COMPONENT-MESSAGE - Message processed by component. MANAGEMENT - State of Mule instance or its resources changed. CONNECTION - Connector connected to, released connection or failed connection attempt to resource. REGISTRY - Event occurred on the Mule registry. CUSTOM - Custom notification. EXCEPTION - An exception was thrown. TRANSACTION - Transaction begun, committed or rolled back. ROUTING - A routing event occurred.

There are additional attributes available on the <notification> element, allowing for fine-grained control when declaring custom notifications. --> <notification event="ENDPOINT-MESSAGE"/> <!-- Disable notifications for the specified event type. Disabling of notifications has precedence over notification

!3

Page 131: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 131/354

enabling. Note that it is possible to disable notifications of one particular event type for a certain interface. This means the event will be sent to listeners with other interfaces. --> <disable-notification interface="COMPONENT-MESSAGE"/> <!-- If we add subscription="GreetingService" to the notification listener declaration below, the listener will only listen

to notifications from the GreetingService. This will result in the listener receiving only notifications from the CXF connector and not the HTTP connector. --> <!-- The first notification listener (notificationListener1) will receive events from all source, while the second (notificationListener2) only will receive events from the resource with the name "GreetingFlow". --> <notification-listener ref="notificationListener1"/> <notification-listener ref="notificationListener2" subscription="GreetingFlow"/> </notifications>

<!-- Flow with an inbound webservice endpoint. --> <flow name="GreetingFlow"> <inbound-endpoint address="http://localhost:8182/services/GreetingService" exchange-pattern="request-response"/> <cxf:jaxws-service serviceClass="com.ivan.mule.services.HelloService"/> <component> <prototype-object class="com.ivan.mule.services.HelloService"/> </component> <log-component/> </flow></mule>

9o%e %$a%:• ($ere are %@o prin# beans, Fno%i)ica%ion-is%ener!G and Fno%i)ica%ion-is%ener2G, de)ined

usin# %$e same implemen%a%ion classe de)ine %@o no%i)ica%ion lis%eners in order %o apply )il%erin# %o one o) %$em and compare

%$e no%i)ica%ions received by %$e lis%eners• ($ere is a no%i)ica%ionsL elemen% de)ined ou%side o) any models, )lo@s or services

($is is @$ere @e de)ine @$ic$ /inds o) no%i)ica%ions %o lis%en )or, @$ic$ no%i)ica%ions no% %olis%en )or and %$e no%i)ica%ion lis%eners o) %$e applica%ion

• ($e no%i)ica%ionsL elemen% $as an a%%ribu%e namedd$namic 9o%i)ica%ion lis%eners can be re#is%ered in %$e Mule con)i#ura%ion )ile, bu% may also be

re#is%ered pro#ramma%ically As per de)aul%, i% is no% possible %o re#is%er no%i)ica%ion lis%enersa)%er %$e Mule con%ex% $as s%ar%ed e%%in# %$ed$namic a%%ribu%e on %$e no%i)ica%ionsLelemen% %o %rue allo@s )or pro#ramma%ic re#is%ra%ion o) no%i)ica%ion lis%eners a)%er %$e Mulecon%ex% $as been s%ar%edIn %$is par%icular example, @e do no% re#is%er any no%i)ica%ion lis%eners pro#ramma%ically($e dynamic a%%ribu%e $as only been included %o $ave an excuse )or describin# %$is )acili%y

• ($ere is a no%i)ica%ionL c$ild elemen% in %$e no%i)ica%ionsL elemen%, @$ic$ loo/s li/e %$is:<notification event="ENDPOINT-MESSAGE"/>($is elemen% %ells Mule %$a% @e @is$ %o receive no%i)ica%ions on endpoin% messa#esJ @$en%$ey are sen% or received ($e di))eren% /inds o) even%s available in Mule are lis%ed in %$e

9o%i)ica%ion sec%ion o) %$e re)erence

!3!

Page 132: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 132/354

• ($e nex% elemen% is a disable&no%i)ica%ionL elemen%, @$ic$ loo/s li/e %$is:<disable-notification interface="COMPONENT-MESSAGE"/>($is elemen% disables %$e sendin# o) no%i)ica%ions )or componen% messa#es 9o%e %$a% @e$ave %o use %$einterface a%%ribu%e %o speci)y %$e /ind o) even% and canno% use %$eeventa%%ribu%e, @$ic$ also is available on %$e elemen% A#ain, %$e di))eren% /inds o) even%savailable in Mule are lis%ed in %$e 9o%i)ica%ion sec%ion o) %$e re)erence

ince %$ere are no componen% messa#e no%i)ica%ion lis%eners declared in %$is example, %$isline $as no e))ec% and is only $ere %o s$o@ %$a% i% is possible %o disable no%i)ica%ions as @ell• 9ex%, %$ere are %@o no%i)ica%ion&lis%enerL elemen%s

9o%i)ica%ion lis%ener elemen%s are used %o re#is%er no%i)ica%ion lis%eners ($eref a%%ribu%e isused %o speci)y @$ic$ prin# bean is %o be %$e lis%ener

• ($e second no%i)ica%ion&lis%enerL elemen% $as a subscription a%%ribu%eI) no subscription a%%ribu%e is presen%, %$e no%i)ica%ion lis%ener @ill receive no%i)ica%ions o)%$e %ype speci)ied by %$e no%i)ica%ion in%er)aceHs %$e lis%ener implemen% )rom allcomponen%s Bsin# %$e subscription a%%ribu%e, %$e lis%ener @ill only receive no%i)ica%ions)orm %$e componen% @$ic$ name is #iven as %$e value o) %$e a%%ribu%e

e @ill see %$is be$aviour @$en @e run %$e example pro#ram s$or%ly• ($e remainin# par% o) %$e Mule 3 x con)i#ura%ion )ile con%ains a )lo@ @$ic$ declares a

1A* @eb service endpoin% 6or de%ails on %$is par%, please re)er %oc$ap%er ! 9o%e, $o@ever, %$e name o) %$e )lo@ & FCree%in#6lo@G, @$ic$ ma%c$es %$e name @e used in%$e declara%ion o) one o) %$e no%i)ica%ion lis%eners

!32

Page 133: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 133/354

8 * +un the Example Program$en runnin# %$e example con)i#ura%ions, @e @ill need soapBI %o ac% as clien% %o %$e @eb service

@e @ill expose >e)ore proceedin#, ma/e sure %$a% you $ave soapBI ins%alledi%$ %$e example pro#ram in place and soapBI ready, @e are no@ se% %o run %$e example pro#ram

Run the Mule 2.x Exa&ple 'rogra&

I) %$e pro4ec% is no% con)i#ured @i%$ %$e Mule 2 x dis%ribu%ion on %$e classpa%$ @$en s%ar%in# %$issec%ion, please re)er %o%$is sec%ion in appendix > on $o@ %o con)i#ure @$ic$ Mule dis%ribu%ion %ouse

• In %$e *ac/a#e or *ro4ec% Explorer, ri#$%&clic/ %$e Fmule&con)i#2 xmlG )ile and selec% RunAs &L Mule erver

• ($e Mule 2 x server s$ould s%ar% up and display some%$in# similar %o %$is on %$e consoleHou%pu% omi%%ed %o conserve space :

...

*** MyMsgNotificationListener 1 instance created.

*** MyMsgNotificationListener 2 instance created....INFO: Setting the server's publish address to behttp://localhost:8182/services/GreetingService...INFO 2012-01-19 16:13:40,681 [main] org.mule.transport.cxf.CxfMessageReceiver:Connected: http://localhost:8182/services/GreetingService...INFO 2012-01-19 16:13:40,755 [main] org.mule.DefaultMuleContext:*********************************************************************** Mule ESB and Integration Platform ** Version: 2.2.1 Build: 14422 *

...

9o%e %$a%:• (@o no%i)ica%ion lis%ener ins%ances @ere crea%ed• ($e @eb service BR- is $%%p:==local$os%:;!;2=services=Cree%in# ervice

($us %$e .- can be )ound a% $%%p:==local$os%:;!;2=services=Cree%in# ervice @sdl

9o%$in# more $appens, since %$e endpoin% is nei%$er receivin# any re0ues%s, nor sendin# anyresponses

!33

Page 134: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 134/354

Create a soap6% Client

In order )or no%i)ica%ions %o be pos%ed in our example pro#ram, @e need %o send re0ues%s %o %$e @ebservice endpoin% exposed by %$e example pro#ram e do %$is usin# soapBI:

• %ar% soapBI• "rea%e a ne@ pro4ec% in soapBI

9ame %$e pro4ec% F9o%i)ica%ion"lien%G and use %$e )ollo@in# BR- as %$e Ini%ial.-= A.-: $%%p:==local$os%:;!;2=services=Cree%in# ervice @sdl

• Co %o Eclipse and clear %$e consolee @an% %o reduce %$e amoun% o) ou%pu% %$a% @e @ill analyze la%er

• Expand %$e node named F#ree%G under F<ello ervice ervice oap>indin#G• .ouble&clic/ %$e FRe0ues% !G under %$e F#ree%G node

• En%er a name in %$e nameL elemen% o) %$e re0ues%($e resul% s$ould loo/ li/e %$is:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"xmlns:ser="http://services.mule.ivan.com/"> <soapenv:Header/> <soapenv:Body> <ser:greet> <!--Optional:--> <name>Ivan</name> </ser:greet> </soapenv:Body></soapenv:Envelope>

• end %$e re0ues% by clic/in# %$e li%%le #reen arro@ in %$e upper le)% corner o) %$e re0ues%@indo@

• ($e resul%in# response s$ould loo/ li/e %$is:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns2:greetResponse xmlns:ns2="http://services.mule.ivan.com/"> <greeting>Hello Ivan, the time is now Thu Jan 19 16:29:29 CET 2012</greeting> </ns2:greetResponse> </soap:Body></soap:Envelope>

!35

Page 135: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 135/354

Examine the (ule " x Example Program +esult

e are no@ ready %o examine %$e console ou%pu% )rom %$e Mule 2 x version o) %$e example pro#ram

• Co %o %$e Eclipse console and loo/ a% %$e ou%pu%, @$ic$ s$ould read some%$in# li/e %$is:

*** MyMsgNotificationListener 1 received a notification: Notification type: org.mule.context.notification.EndpointMessageNotification Action name: received Action code: 801 Notification source type: class org.mule.DefaultMuleMessage Endpoint connector: connector.http.0 Resource identifier:

_cxfServiceComponent{http://services.mule.ivan.com/}HelloServiceService506786885 Action description: Message received Message payload object:org.apache.commons.httpclient.ContentLengthInputStream@29565e9d

*** MyMsgNotificationListener 2 received a notification: Notification type: org.mule.context.notification.EndpointMessageNotification Action name: received

Action code: 801 Notification source type: class org.mule.DefaultMuleMessage Endpoint connector: connector.cxf.0 Resource identifier: GreetingService Action description: Message received Message payload object: Ivan

*** MyMsgNotificationListener 1 received a notification: Notification type: org.mule.context.notification.EndpointMessageNotification Action name: received Action code: 801 Notification source type: class org.mule.DefaultMuleMessage Endpoint connector: connector.cxf.0 Resource identifier: GreetingService Action description: Message received Message payload object: IvanINFO 2012-01-19 16:35:12,286 [connector.stdio.0.dispatcher.2]org.mule.transport.stdio.StdioMessageDispatcher: Connected:endpoint.outbound.stdio://system.outHello Ivan, the time is now Thu Jan 19 16:35:12 CET 2012*** MyMsgNotificationListener 2 received a notification: Notification type: org.mule.context.notification.EndpointMessageNotification Action name: dispatched Action code: 802 Notification source type: class org.mule.DefaultMuleMessage Endpoint connector: connector.stdio.0 Resource identifier: GreetingService Action description: Message dispatched Message payload object: Hello Ivan, the time is now Thu Jan 19 16:35:12 CET 2012

*** MyMsgNotificationListener 1 received a notification: Notification type: org.mule.context.notification.EndpointMessageNotification Action name: dispatched

Action code: 802 Notification source type: class org.mule.DefaultMuleMessage Endpoint connector: connector.stdio.0 Resource identifier: GreetingService Action description: Message dispatched Message payload object: Hello Ivan, the time is now Thu Jan 19 16:35:12 CET 2012

9o%e %$a%:• ($ere is a %o%al o) )ive no%i)ica%ions received by our no%i)ica%ion lis%eners• (@o o) %$e no%i)ica%ions @ere received by no%i)ica%ion lis%ener 2 and %$ree @ere received by

no%i)ica%ion lis%ener !

($e reason )or %$is is %$a% @e used %$e subscription a%%ribu%e on %$e no%i)ica%ion&lis%enerL

!38

Page 136: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 136/354

elemen% used %o declare no%i)ica%ion lis%ener 2, limi%in# %$e no%i)ica%ions %o only %$ose )rom%$e componen% FCree%in# erviceG

• Examinin# %$e )irs% no%i)ica%ion lis%ener ou%pu% s$o@n belo@, @e can dra@ someconclusions:

...*** MyMsgNotificationListener 1 received a notification: Notification type: org.mule.context.notification.EndpointMessageNotification Action name: received Action code: 801 Notification source type: class org.mule.DefaultMuleMessage Endpoint connector: connector.http.0 Resource identifier:

_cxfServiceComponent{http://services.mule.ivan.com/}HelloServiceService506786885 Action description: Message received Message payload object:org.apache.commons.httpclient.ContentLengthInputStream@29565e9d...

& ($e endpoin% received a re0ues% Hsee Ac%ion name and Ac%ion code & ($e source o) %$e no%i)ica%ion @as a Mule messa#e Hsee 9o%i)ica%ion source %ype

& ($e connec%or %$a% received %$e messa#e @as a <((* connec%or Hsee Endpoin% connec%or& ($e ori#in o) %$e no%i)ica%ion is a " 6 componen% @i%$ a lon# name Hsee Resource

iden%i)ier& ($e payload o) %$e received Mule messa#e is a s%ream Hsee Messa#e payload ob4ec%& ($ere is no correspondin# no%i)ica%ion )or no%i)ica%ion lis%ener 2

• Examinin# %$e second and %$ird no%i)ica%ion lis%ener ou%pu%s @e can dra@ %$e )ollo@in#conclusions:& ($e endpoin% received a re0ues% Hsee Ac%ion name and Ac%ion codeAc%ually, %$is is %$e same re0ues% %$a% caused %$e )irs% no%i)ica%ion ou%pu%, bu% received byano%$er componen%

& ($e source o) %$e no%i)ica%ions @as a Mule messa#e Hsee 9o%i)ica%ion source %ype& ($e connec%or %$a% received %$e no%i)ica%ions @as a " 6 connec%or Hsee Endpoin%connec%or& ($e payload o) %$e received Mule messa#e is %$e s%rin# FIvanG Hsee Messa#e payloadob4ec%& ($e %@o no%i)ica%ion messa#es are almos% iden%ical>o%$ %$e no%i)ica%ion lis%eners $ave received a no%i)ica%ion o) one and %$e same even%

• A)%er %$e second and %$ird no%i)ica%ion lis%ener ou%pu%, %$ere is a lo# messa#e:

INFO 2012-01-19 16:35:12,286 [connector.stdio.0.dispatcher.2]org.mule.transport.stdio.StdioMessageDispatcher: Connected:endpoint.outbound.stdio://system.outHello Ivan, the time is now Thu Jan 19 16:35:12 CET 2012

.ue %o our Mule con)i#ura%ion, %$e #ree%in# s%rin# #enera%ed by our <ello service isou%pu% %o %$e console

• 6inally, %$e )our%$ and )i)%$ no%i)ica%ion lis%ener lo#s %$e dispa%c$ o) %$e response send %o %$eclien% o) %$e @eb service

e can see %$a% %$e messa#e payload is %$e #ree%in# s%rin# produced by our <ello service

!37

Page 137: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 137/354

Run the Mule 3.x Exa&ple 'rogra&

e @ill no@ run %$e Mule 3 x version o) %$e example pro#ram:

• "on)i#ure %$e Eclipse pro4ec% @i%$ %$e Mule 3 x dis%ribu%ion on %$e classpa%$*lease re)er %o %$is sec%ion in appendix > )or de%ailed ins%ruc%ions

• In %$e *ac/a#e or *ro4ec% Explorer, ri#$%&clic/ %$e Fmule&con)i#3 xmlG )ile and selec% RunAs &L Mule erver

e can see %$a% Mule 3 x s%ar%s up and %@o ins%ances o) our no%i)ica%ion lis%ener is crea%ed, as@as %$e case @i%$ %$e Mule 2 x version

• "lear %$e console in Eclipse• 6rom soapBI, send a re0ues% %o %$e @eb service

($ere is no need %o re&crea%e or even c$an#e %$e soapBI pro4ec% @e can con%inue %o use %$every same pro4ec% crea%edearlier

In Eclipse, %$ere s$ould be some ou%pu% in %$e console @$ic$ @e'll %a/e a closer loo/ a% nex%

!3

Page 138: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 138/354

Examine the (ule & x Example Program +esult

Runnin# %$e Mule 3 x version o) %$e example pro#ram produces a resul% %$a% is very similar %o %$a%o) %$e Mule 2 x version In %$is sec%ion @e'll mainly )ocus on %$e di))erences

• Examine %$e console ou%pu% in Eclipse, @$ic$ s$ould loo/ some%$in# li/e %$is:

*** MyMsgNotificationListener 1 received a notification: Notification type: org.mule.context.notification.EndpointMessageNotification Action name: received Action code: 801 Notification source type: class org.mule.DefaultMuleMessage Endpoint connector: connector.http.mule.default Resource identifier: GreetingFlow Action description: Message received Message payload object:org.apache.commons.httpclient.ContentLengthInputStream@26514577

*** MyMsgNotificationListener 2 received a notification: Notification type: org.mule.context.notification.EndpointMessageNotification Action name: received Action code: 801 Notification source type: class org.mule.DefaultMuleMessage Endpoint connector: connector.http.mule.default Resource identifier: GreetingFlow Action description: Message received Message payload object:org.apache.commons.httpclient.ContentLengthInputStream@26514577INFO 2012-01-20 06:48:43,469 [connector.http.mule.default.receiver.02]org.mule.component.simple.LogComponent:********************************************************************************* Message received in service: GreetingFlow. Content is: 'Hello Ivan, the ** time is now Fri Jan 20 06:48:43 CET 2012' *********************************************************************************

*** MyMsgNotificationListener 1 received a notification: Notification type: org.mule.context.notification.EndpointMessageNotification Action name: response Action code: 805

Notification source type: class org.mule.DefaultMuleMessage Endpoint connector: connector.http.mule.default Resource identifier: GreetingFlow Action description: Responded to message Message payload object: org.mule.transport.http.HttpResponse@3b5789a5

*** MyMsgNotificationListener 2 received a notification: Notification type: org.mule.context.notification.EndpointMessageNotification Action name: response Action code: 805 Notification source type: class org.mule.DefaultMuleMessage Endpoint connector: connector.http.mule.default Resource identifier: GreetingFlow Action description: Responded to message Message payload object: org.mule.transport.http.HttpResponse@3b5789a5

9o%e %$a%:• ($ere are only )our no%i)ica%ion lis%ener ou%pu% sec%ions

($is is due %o %$e Mule 3 x )lo@ di))erin# sli#$%ly )rom %$e Mule 2 x moduleMule 2 x uses a cons%ruc% @$ere messa#es )irs% arrives a% a <((* endpoin% and %$en are)or@arded %o a " 6 endpoin% Mule 3 x, on %$e o%$er $and, uses only a <((* endpoin% %oreceive messa#es, @$ic$ %$en are )or@arded %o a " 6 service componen% ($e " 6componen% is no% an endpoin% and @ill %$us no% #ive rise %o no%i)ica%ions in our example

pro#ram• ($e resource iden%i)ier is al@ays FCree%in#6lo@G

Even i) @e #ive %$e endpoin% in %$e )lo@ a name, %$e value o) %$e resource iden%i)ier @ill s%ill

!3;

Page 139: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 139/354

be %$e name o) %$e )lo@• e canno% see %$e ac%ual payload o) %$e Mule messa#es in %$e ou%pu% )orm %$e no%i)ica%ion

lis%eners($e endpoin% in %$e Mule 3 x version o) %$e example pro#ram deals @i%$ %$e Mule messa#every early=la%e in %$e processin# c$ainJ %$a% is, immedia%ely @$en receivin# %$e re0ues% orimmedia%ely prior %o sendin# %$e response A% %$ese s%a#es, %$e payload is accessed ei%$er%$rou#$ a s%ream or in a <((* response ob4ec%

($is concludes %$is c$ap%er, in @$ic$ @e %oo/ a loo/ a% Mule no%i)ica%ions Addi%ional in)orma%ionabou% Mule no%i)ica%ion can be )ound in %$e 9o%i)ica%ions sec%ion in par% %@o o) %$is boo/

8 , -//itional Exercises6or %$e curious, $ere are some su##es%ions )or addi%ional exercises )or @$ic$ %$is c$ap%er's example

pro#ram can ac% as a s%ar%in# poin%• Remove %$e subscrip%ion a%%ribu%e )ound in one o) %$e no%i)ica%ion&lis%enerL elemen%s in %$e

Mule con)i#ura%ion )ilesRe&run %$e example pro#ram and examine %$e ou%pu%

• "rea%e ano%$er no%i)ica%ion lis%ener %$a% lis%ens %o "1M*19E9(&ME ACE no%i)ica%ions.on'% )or#e% %o remove %$e disable&no%i)ica%ionL elemen% )rom %$e Mule con)i#ura%ion )iles

be)ore runnin# %$e example pro#ram, o%$er@ise your ne@ no%i)ica%ion lis%ener @ill no%receive any no%i)ica%ions

• Implemen% %$e )irin# o) a cus%om no%i)ica%ion in %$e <ello service and a cus%om no%i)ica%ionlis%ener %$a% lis%ens )or %$e cus%om no%i)ica%ion

!3+

Page 140: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 140/354

7. Exception +andling in MuleIn %$is c$ap%er @e @ill see $o@ Mule be$aves @$en some%$in# #oes @ron# and an excep%ion is%$ro@n ($is @ill be bu% an in%roduc%ion %o %$e )ollo@in# )ea%ures rela%ed %o error $andlin#:

• Excep%ion&s%ra%e#ies

e'll %ry ou% bo%$ de)aul% and cus%om excep%ion s%ra%e#iesExcep%ion s%ra%e#ies can be de)ined %o be model&#lobal as @ell as service&local >o%$ op%ions@ill be examined

• Excep%ion&based rou%in#Messa#es can be rou%ed dependin# on @$e%$er a%%emp%in# %o con%ac% a service resul%s in anexcep%ion or no%

• "us%om excep%ion lis%eners"us%om excep%ion lis%eners @ere par% o) %$e example inc$ap%er %$ree and are used in %$isc$ap%er as @ell

($e con)i#ura%ions )or Mule 2 x and Mule 3 x @ill di))er 0ui%e si#ni)ican%ly I% @ould $ave been possible %o use models and services in Mule 3 x %oo, bu% I @an%ed %o s$o@ $o@ %o ac$ieve excep%ion$andlin# @i%$ Mule 3 x&speci)ic )ea%ures

!5

Page 141: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 141/354

: 1 (ule " x Configuration Structure($e s%ruc%ure o) %$is c$ap%er's Mule 2 x example @ill loo/ li/e %$is:

%ruc%ure o) %$e Mule 2 x con)i#ura%ion o) %$is c$ap%er's example

9o%e %$e )ollo@in# abou% %$e above )i#ure:•

($ere are %@o models named FMainModelG($e reason )or %$is is %$a% %$e model&#lobal excep%ion s%ra%e#y as @ell as %$e error lo##in#service are de)ined in a paren%&model @$ic$ is %$en in$eri%ed by a c$ild modelRecall %$a%, as s$o@n inc$ap%er %$ree, in order %o suppor% model&in$eri%ance, %$e paren% andc$ild models mus% $ave %$e same name

• ($e model exposes %@o endpoin%s %$a% @e'll send messa#es %o• ($e service Freceiver erviceG passes incomin# messa#es on %o %@o o%$er servicesJ

Fexcep%ion erviceG and F$ello erviceG($e Freceiver erviceG does no% $ave an excep%ion s%ra%e#y o) i%s o@n and no excep%ions @illoccur in %$is service, ra%$er in one o) %$e services i% invo/es

• ($e service F#o%Excep%ion %ra%e#y erviceG @ill $ave a service&local excep%ion s%ra%e#y andan excep%ion @ill occur in %$is very service eac$ %ime i% is invo/ed

!5!

Page 142: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 142/354

: " (ule & x Configuration Structure($e s%ruc%ure o) %$e Mule 3 x con)i#ura%ion in %$is c$ap%er @ill loo/ li/e %$is:

%ruc%ure o) %$e Mule 3 x con)i#ura%ion o) %$is c$ap%er's example

9o%e %$e )ollo@in# abou% %$e Mule 3 x con)i#ura%ion s%ruc%ure:• ($ere are no paren%&c$ild rela%ions

I% is no% possible %o in$eri% proper%ies be%@een )lo@se @ill see %$a% one or more services can in$eri% one and %$e same excep%ion s%ra%e#y

de)ined in a common, abs%rac%, paren% simple&service in %$e c$ap%erMule "on)i#ura%ion*a%%erns

• ($ere are no #lobal excep%ion s%ra%e#y lis%eners>o%$ excep%ion s%ra%e#y lis%eners used are )lo@&local and @ill only receive no%i)ica%ions@$en an excep%ion occurred in %$e same )lo@ as in @$ic$ %$e lis%ener is de)ined

• ($e con)i#ura%ion exposes %@o endpoin%s %$a% @e'll send messa#es %o• ($e )lo@ FReceiver6lo@G passes incomin# messa#es on %o %@o o%$er )lo@sJ

FAl@aysExcep%ion6lo@G and F<ello6lo@G

($e )lo@ FReceiver6lo@G does no% $ave an excep%ion s%ra%e#y o) i%s o@n and no excep%ions@ill occur in %$is )lo@, ra%$er in one o) %$e )lo@s i% invo/es

• ($e )lo@ FCo%Excep%ion %ra%e#y6lo@G $as an excep%ion s%ra%e#y and an excep%ion @illoccur in %$is )lo@ eac$ %ime i% is invo/ed

: & Create the Pro!ect"rea%e %$e pro4ec% as described in %$e appendix"rea%e a Mule *ro4ec%, namin# i%FMuleExcep%ion<andlin#G ($e Mule 3 $o% deploymen% can be s@i%c$ed o)) )rom %$e s%ar%, as %$is)ea%ure @ill no% be used

!52

Page 143: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 143/354

: ' Create the Ser$ice %mplementation Classes($e example ma/es use o) %$ree services, @$ic$ implemen%a%ions all are iden%ical re#ardless o)@$e%$er used @i%$ Mule 2 x or Mule 3 x

• Excep%ion ervice• <ello ervice• -o##in# ervice

Create the Exception Service )&ple&entation Class

($e Excep%ion service al@ays %$ro@s an excep%ion @$en receivin# a messa#e, in order )or us %oob%ain excep%ions %o $andle in %$is example

• In %$e pac/a#e com.ivan.services , implemen% %$e &xceptionService class as %$is:

package com.ivan.services;

import org.mule.api.DefaultMuleException;import org.mule.api.MuleEventContext;import org.mule.api.lifecycle.Callable;

/** * A service that always throws an exception when being invoked. */public class ExceptionService implements Callable{ private static int sExceptionId = 1;

@Override public Object onCall(MuleEventContext inEventContext) throws Exception { int theExceptionId = sExceptionId++; System.out.println("*** In ExceptionService.onCall(): " + theExceptionId); Exception theNestedException = new Exception( "I am a nested exception with id " + theExceptionId); throw new DefaultMuleException( "I am an outer exception with id " + theExceptionId, theNestedException); }}

9o%e %$a%:• ($e service class implemen%s %$e allable in%er)ace

($is in%er)ace allo@s a service %o receive Mule even%s, bu% also in%roduces a dependency onMule "ompare %$is %o %$e service implemen%a%ion, a *1?1, %$a% @e used inc$ap%er %$ree

ee %$e sec%ionImplemen%in# %$e "allable In%er)ace belo@ )or more in)orma%ion• An iden%i)yin# number is appended %o %$e messa#e o) excep%ions

($is number is also lo##ed %o %$e console @$en %$e service is invo/ed($is @ill enable us %o see @$ic$ service invoca%ions cause an error %o be repor%ed

• ($e +efaultMule&xception %$ro@n by %$eon all me%$od @raps an excep%ion o) %$e %ype &xception

• I) @e only @an%ed a componen% %$a% %$ro@s an excep%ion every %ime i% is invo/ed, %$en %$e)ollo@in# con)i#ura%ion line @ould be enou#$:

%es%:componen% %$ro@Excep%ionWX%rueX=L($e above class is implemen%ed %o $ave nes%ed excep%ions and %o allo@ )or debu##in#

!53

Page 144: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 144/354

Create the +ello Service )&ple&entation Class

($e <ello service replies incomin# messa#es @i%$ a #ree%in# ($is service al@ays succeeds in processin# incomin# re0ues%s

• In %$ecom.ivan.services pac/a#e, implemen% %$e HelloService class

package com.ivan.services;

import java.util.Date;import org.mule.api.MuleEventContext;import org.mule.api.lifecycle.Callable;

/** * The old faithful Hello service. *

* @author Ivan Krizsan */public class HelloService implements Callable{ @Override public Object onCall(final MuleEventContext inEventContext) throws Exception { String theMessage = "Hello. The time is now: " + (new Date()); System.out.println("*** In HelloService.onCall(): " + theMessage); return theMessage; }}

9o%e %$a%:• As @i%$ %$e previous service implemen%a%ion class, %$e service class implemen%s %$e

allable in%er)aceee %$e sec%ionImplemen%in# %$e "allable In%er)ace belo@ )or more in)orma%ion

• A plain s%rin# ob4ec% is re%urned )rom %$eon all me%$od

($e s%rin# @ill %$us become %$e payload o) a Mule messa#e %$a% in %urn becomes %$e resul%o) %$e service invoca%ion

!55

Page 145: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 145/354

Create the $ogging Service )&ple&entation Class

($e -o##in# service lo#s messa#es received ($is service also con%ains s%a%ic me%$ods %o lo#messa#es ($ese me%$ods are used by o%$er par%s o) %$e example pro#ram

• In %$e same pac/a#e as %$e o%$er services,com.ivan.services , implemen% %$e LoggingService

class:package com.ivan.services;

import java.util.HashMap;import java.util.Map;import java.util.Set;import org.mule.api.ExceptionPayload;import org.mule.api.MuleEventContext;import org.mule.api.MuleMessage;import org.mule.api.lifecycle.Callable;import org.mule.api.transport.PropertyScope;

/** * Service that logs message payload and properties. *

* @author Ivan Krizsan */public class LoggingService implements Callable{

/* (non-Javadoc) * @see org.mule.api.lifecycle.Callable#onCall(org.mule.api.MuleEventContext) */ @Override public Object onCall(MuleEventContext inEventContext) throws Exception { MuleMessage theReceivedMsg = inEventContext.getMessage();

System.out.println("***** Logging service received a message:"); logMessage(theReceivedMsg);

return theReceivedMsg;

} /** * Logs information, including payload and properties in the * INBOUND and OUTBOUND scopes, of the supplied Mule message. * Also logs any exception payload associated with the supplied * Mule message. *

* @param inMuleMessage Message to log information about. */ public static void logMessage(final MuleMessage inMuleMessage) { Map<String, Object> theReceivedMsgProperties; long theCurrentTime = System.currentTimeMillis();

System.out.println(" Current time: " + theCurrentTime); try { System.out.println(" Message payload: " + inMuleMessage.getPayloadAsString()); } catch (final Exception theException) { // Ignore exceptions }

/* Log message properties in the inbound and outbound scopes. */ System.out.println(" Message properties: "); theReceivedMsgProperties = LoggingService.retrieveMessageProperties(inMuleMessage, PropertyScope.INBOUND); System.out.println(" INBOUND: " + theReceivedMsgProperties); theReceivedMsgProperties = LoggingService.retrieveMessageProperties(inMuleMessage,

PropertyScope.OUTBOUND); System.out.println(" OUTBOUND: " + theReceivedMsgProperties);

!58

Page 146: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 146/354

Page 147: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 147/354

: * The Callable %nterfaceAll %$ree service implemen%a%ion classes implemen%ed in %$e previous sec%ion implemen% %$eorg.mule.api.lifec$cle. allable in%er)ace $ile implemen%in# %$is in%er)ace in%roduces adependency on Mule, i% can be use)ul )or %$e )ollo@in# reasons:

end even%s sync$ronously and async$ronously )rom %$e service($e Mule&vent ontext in%er)ace con%ains send&vent and send&ventAs$nc me%$ods• Re0ues% even%s sync$ronously

Bsin# %$ere uest&vent me%$od in %$e Mule&vent ontext in%er)ace• %op )ur%$er processin# o) %$e messa#e

Bsin# Mule&vent ontext.setStop,urtherProcessing • Manipula%e %$e curren% %ransac%ion, i) any

Bse %$e get'ransaction me%$od %o re%rieve %$e %ransac%ion )or %$e curren% even%Bse %$emar 'ransction,or#ollbac %o mar/ %$e curren% %ransac%ion, i) any, )or rollbac/

>o%$ %$ese me%$ods can be )ound in %$e Mule&vent ontext in%er)ace($e on all me%$od o) a service implemen%in# %$e allable in%er)ace is %o re%urn an ob4ec% uc$ anob4ec% can be one o) %$e )ollo@in# %ypes:

• An ob4ec% implemen%in# MuleMessage($e re%urned ob4ec% @ill be %$e resul% o) %$e service invoca%ion

• An ins%ance o)2oid#esult ($e ori#inal messa#e, as received by %$e service, @ill be %$e resul% o) %$e service invoca%ion

• A non&null re)erence %o any o%$er ob4ec%($e ob4ec% @ill become %$e payload o) a Mule messa#e, @$ic$ @ill be %$e resul% o) %$e

service invoca%ion

!5

Page 148: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 148/354

: , Create the Starter ClassesIn %$is c$ap%er's example @e @ill use s%ar%er classes, one )or Mule 2 x and ano%$er )or Mule 3 x, as%o be able %o c$oose %$e endpoin% @$ic$ %o invo/e, cons%ruc% messa#es @i%$ %$e desired payload andmessa#e proper%ies and, )inally, %o examine %$e resul%s o) %$e service invoca%ions

.e%ails on %$e implemen%a%ion o) %$ese s%ar%er classes @ill be discussed in %$e subse0uen% c$ap%er onMule *ro#ramma%ic Bse

Create the Mule 2.x Starter Class

($e Mule 2 x s%ar%er class is, no% en%irely surprisin#, %o be used @i%$ %$e Mule 2 x run%ime• In %$e pac/a#e com.ivan.starter9 implemen% %$e class

Mule%&xceptionHandling&xampleStarter :package com.ivan.starter;

import org.mule.DefaultMuleMessage;import org.mule.api.MuleContext;import org.mule.api.MuleException;import org.mule.api.MuleMessage;import org.mule.config.spring.SpringXmlConfigurationBuilder;import org.mule.context.DefaultMuleContextFactory;import org.mule.module.client.MuleClient;import com.ivan.services.LoggingService;

/** * Starter program for the exception handling example. * Starts Mule, sends a message to the Mule configuration and finally * examines the result. * Version for Mule 2.x. * * @author Ivan A Krizsan */public class Mule2ExceptionHandlingExampleStarter

{ private final static String MULE_CONFIG_FILE = "com/ivan/muleconfig/mule2-config.xml"; private final static String MULE_SERVICE1_URL = "vm://receiverServiceURL"; private final static String MULE_SERVICE2_URL = "vm://gotExceptionStrategyServiceURL";

public static void main(String[] args) throws Exception { (new Mule2ExceptionHandlingExampleStarter()).doExample(); }

private void doExample() { MuleContext theContext = null; try { MuleClient theMuleClient; /* Create a new message, setting its payload. */ MuleMessage theMuleMessage = new DefaultMuleMessage("I am a Mule message!");

/* Start Mule context with the first configuration file. */ theContext = startMule(MULE_CONFIG_FILE);

/* Create a Mule client and send the message to it. */ theMuleClient = createMuleClient(theContext); theMuleMessage = sendMessageToMule(theMuleMessage, theMuleClient);

System.out.println("*** Finished invoking Mule configuration!"); LoggingService.logMessage(theMuleMessage); } catch (final Exception theException) { theException.printStackTrace();

} finally

!5;

Page 149: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 149/354

{ /* * Dispose of the Mule contexts. Disposing the context * automatically stops it and we do not have to bother * with the exception declared by the stop method. * Must dispose if we wish the application to terminate, * otherwise there will be unreleased resources. */ if (theContext != null)

{ theContext.dispose(); } } }

private MuleContext startMule(final String inMuleConfigFile) throws MuleException { String[] theConfigFiles = { inMuleConfigFile };

DefaultMuleContextFactory theContextFactory = new DefaultMuleContextFactory(); SpringXmlConfigurationBuilder theConfigBuilder = new SpringXmlConfigurationBuilder(theConfigFiles); MuleContext theMuleContext =

theContextFactory.createMuleContext(theConfigBuilder); theMuleContext.start();

return theMuleContext; }

private MuleClient createMuleClient(final MuleContext inMuleContext)throws MuleException

{ MuleClient theMuleClient = new MuleClient(inMuleContext); return theMuleClient; }

private MuleMessage sendMessageToMule(final MuleMessage inMessage, final MuleClient inMuleClient) throws Exception { MuleMessage theReceivedMsg;

/* Modify the URL of the service which to send the message here. */ theReceivedMsg = inMuleClient.send(MULE_SERVICE1_URL, inMessage); return theReceivedMsg; }}

9o%e %$a%:• ($e )lo@ o) %$e s%ar%er class is 0ui%e simple:

& %ar% Mule @i%$ a con)i#ura%ion )ile& "rea%e a Mule messa#e& end %$e Mule messa#e %o a service in %$e con)i#ura%ion& Examine %$e resul%

• ($ere are %@o di))eren% Mule endpoin% BR-s de)ined as cons%an%s in %$e class1ne is MB-EP ERVI"E!, %$e o%$er is MB-EP ERVI"E2 ($e )irs% endpoin% does no%direc%ly cause an excep%ion %o be %$ro@n, bu% invo/es a service %$a% causes an excep%ion %o

be %$ro@n ($e second endpoin% @ill cause an excep%ion %o be %$ro@n in %$e servicecon%ainin# %$e endpoin% e @ill see @$a% %$is means %o %$e clien% invo/in# %$e di))eren%services

• ($e BR- o) %$e service @$ic$ %o send %$e messa#e %o can be c$an#ed in %$e sendMessage'oMule me%$od

• A#ain, )ur%$er de%ails on %$e s%ar%er classes are available in %$enex% c$ap%er

!5+

Page 150: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 150/354

Create the Mule 3.x Starter Class

($e Mule 3 x s%ar%er class is %o be used @i%$ %$e Mule 3 x run%ime• In %$e pac/a#e com.ivan.starter9 implemen% %$e class

Mule)&xceptionHandling&xampleStarter :package com.ivan.starter;

import org.mule.DefaultMuleMessage;import org.mule.api.MuleContext;import org.mule.api.MuleException;import org.mule.api.MuleMessage;import org.mule.api.config.ConfigurationBuilder;import org.mule.api.context.MuleContextBuilder;import org.mule.api.context.MuleContextFactory;import org.mule.config.spring.SpringXmlConfigurationBuilder;import org.mule.context.DefaultMuleContextBuilder;import org.mule.context.DefaultMuleContextFactory;import org.mule.module.client.MuleClient;import com.ivan.services.LoggingService;

/** * Starter program for the exception handling example. * Starts Mule, sends a message to the Mule instance and finally examines the result. * Version for Mule 3.x. *

* @author Ivan Krizsan */public class Mule3ExceptionHandlingExampleStarter{ private final static String MULE_CONFIG_FILE = "com/ivan/muleconfig/mule3-config.xml"; private final static String MULE_SERVICE1_URL = "vm://receiverServiceURL"; private final static String MULE_SERVICE2_URL = "vm://gotExceptionStrategyServiceURL";

public static void main(String[] args) throws Exception { (new Mule3ExceptionHandlingExampleStarter()).doExample(); }

private void doExample() { MuleContext theContext = null; try { MuleClient theMuleClient; /* * Start the first Mule context with the configuration file. * Need to do this, since the context will be used when * creating a new message. */ theContext = startMule(MULE_CONFIG_FILE);

/* Create the message to send. */ MuleMessage theMuleMessage = new DefaultMuleMessage( "I am a Mule message!", theContext);

/* Create a Mule client and send the message to it. */ theMuleClient = createMuleClient(theContext); theMuleMessage = sendMessageToMule(theMuleMessage, theMuleClient);

System.out.println("*** Finished invoking Mule configuration!"); LoggingService.logMessage(theMuleMessage); } catch (final Exception theException) { theException.printStackTrace(); } finally { /* * Dispose of the Mule contexts. Disposing the context * automatically stops it and we do not have to bother * with the exception declared by the stop method.

* Must dispose if we wish the application to terminate, * otherwise there will be unreleased resources.

!8

Page 151: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 151/354

*/ if (theContext != null) { theContext.dispose(); } } }

private MuleContext startMule(final String inMuleConfigFile) throws MuleException

{ /* Starts an instance of Mule using the supplied configuration file. */ String[] theConfigFiles = { inMuleConfigFile };

MuleContextFactory theContextFactory = new DefaultMuleContextFactory(); ConfigurationBuilder theConfigBuilder = new SpringXmlConfigurationBuilder(theConfigFiles); MuleContextBuilder theContextBuilder = new DefaultMuleContextBuilder(); MuleContext theMuleContext = theContextFactory.createMuleContext( theConfigBuilder, theContextBuilder); theMuleContext.start();

return theMuleContext; }

private MuleClient createMuleClient(final MuleContext inMuleContext) throws MuleException { MuleClient theMuleClient = new MuleClient(inMuleContext); return theMuleClient; }

private MuleMessage sendMessageToMule(final MuleMessage inMessage, final MuleClient inMuleClient) throws Exception { /* Modify the URL of the service which to send the message here. */ MuleMessage theReceivedMsg = inMuleClient.send(MULE_SERVICE2_URL, inMessage); return theReceivedMsg; }}

9o%e %$a%:• ($e )lo@ o) %$e s%ar%er class is iden%ical %o %$e Mule 2 x version:

& %ar% Mule @i%$ a con)i#ura%ion )ile& "rea%e a Mule messa#e& end %$e Mule messa#e %o a service in %$e con)i#ura%ion& Examine %$e resul%

• ($ere are %@o di))eren% Mule endpoin% BR-s de)ined as cons%an%s in %$e class1ne is MB-EP ERVI"E!, %$e o%$er is MB-EP ERVI"E2 ($e )irs% endpoin% does no%direc%ly cause an excep%ion %o be %$ro@n, bu% invo/es a )lo@ %$a% causes an excep%ion %o be%$ro@n ($e second endpoin% @ill cause an excep%ion %o be %$ro@n in %$e )lo@ con%ainin# %$e

endpoin% e @ill see @$a% %$is means %o %$e clien% invo/in# %$e di))eren% services• ($e BR- o) %$e service @$ic$ %o send %$e messa#e %o can be c$an#ed in %$e

sendMessage'oMule me%$od• In %$edo&xample me%$od, @e a%%emp% %o dispose %$e Mule con%ex% in order %o s$u% do@n

Mule($is @or/s as expec%ed in versions o) Mule 3 x prior %o version 3 2 , bu% %$ere seem %o besome problem in more recen% versions Dou may need %o s$u% do@n ins%ances o) %$e example

pro#ram manually• A#ain, )ur%$er de%ails on %$e s%ar%er classes are available in %$enex% c$ap%er

!8!

Page 152: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 152/354

: 2 Create the Exception Listeners6or %$e cus%om excep%ion s%ra%e#ies used in %$is c$ap%er's example, @e @ill need an excep%ionlis%ener class ince Mule 2 x and Mule 3 x use di))eren% in%er)aces )or excep%ion lis%eners, @e mus%implemen% one lis%ener class )or Mule 2 x and ano%$er )or Mule 3 x

Create the Mule 2.x Exception $istener

$en implemen%in# a cus%om excep%ion s%ra%e#y, @e need %o implemen% an excep%ion lis%ener class($ere are a )e@ c$oices on $o@ %o implemen% suc$ a class:

• .irec%ly implemen% %$e 4ava.beans.&xceptionListener in%er)ace<avin# your excep%ion lis%ener implemen% %$is in%er)ace is %$e mos% basic approac$ %oimplemen%in# an excep%ion lis%ener

• In$eri% )rom %$eorg.mule.Abstract&xceptionListener class($e Abstract&xceptionListener class implemen%s lo##in#, %ransac%ion $andlin# and messa#erou%in# me%$ods Abs%rac% me%$ods exis% )or $andlin# di))eren% %ypes o) excep%ions, suc$ asmessa#in#&, rou%in#& and s%andard&excep%ions

• In$eri% )rom %$eorg.mule.+efault&xceptionStrateg$ class($e +efault&xceptionStrateg$ class provides de)aul% implemen%a%ions o) %$e abs%rac%me%$ods in %$e Abstract&xceptionListener class

• In$eri% )rom %$eorg.mule.tc .functional.:uiet&xceptionStrateg$ class1nly produce .E>BC&level lo# ou%pu% )or %$e di))eren% %ypes o) excep%ions

• In$eri% )rom %$eorg.mule.service.+efaultService&xceptionStrateg$ classubclass o) %$e +efault&xceptionStrateg$ class, %$is class also main%ains per&service

s%a%is%ics abou% errors and rou%ed messa#es

In %$is example, @e @ill use %$e second al%erna%iveJ ex%endin# %$e Abstract&xceptionListener class• In %$e pac/a#e com.ivan.mule , implemen% %$e class M$Mule%&xceptionListener :

package com.ivan.mule;

import org.mule.AbstractExceptionListener;import org.mule.api.MuleMessage;import org.mule.api.endpoint.ImmutableEndpoint;

import com.ivan.services.LoggingService;

/**

* Custom Mule 2 exception listener. * To alter the logging-behaviour of the default exception strategy, * override the logException(Throwable) method. *

* @author Ivan A Krizsan */public class MyMule2ExceptionListener extends AbstractExceptionListener{ private String mListenerName;

@Override protected void logException(final Throwable inException) { /* Never log exceptions here. */ }

@Override public void handleMessagingException(final MuleMessage inMessage,

!82

Page 153: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 153/354

final Throwable inException) { System.out.println("*** MyMule2ExceptionListener.handleMessagingException: " + mListenerName); LoggingService.logMessage(inMessage);

/* * Route the exception. Also handles transactions. */

routeException(inMessage, null, inException); }

@Override public void handleRoutingException(final MuleMessage inMessage, final ImmutableEndpoint inEndpoint, final Throwable inException) { System.out.println("*** MyMule2ExceptionListener.handleRoutingException: " + mListenerName); System.out.println(" Message id: " + inMessage.getUniqueId()); System.out.println(" Endpoint: " + inEndpoint.getName()); System.out.println(" Exception: " + inException.getMessage());

/* * Route the exception. Also handles transactions. */ routeException(inMessage, inEndpoint, inException); }

@Override public void handleLifecycleException(final Object inComponent, final Throwable inException) { System.out.println("*** MyMule2ExceptionListener.handleLifecycleException: " + mListenerName); System.out.println(" Component: " + inComponent); System.out.println(" Exception: " + inException.getMessage());

/* * Route the exception. Also handles transactions. */ routeException(null, null, inException); }

@Override public void handleStandardException(final Throwable inException) { System.out.println("*** MyMule2ExceptionListener.handleStandardException: " + mListenerName); System.out.println(" Exception: " + inException.getMessage());

/* * Route the exception. Also handles transactions. */ routeException(null, null, inException); }

public String getListenerName() { return mListenerName; }

public void setListenerName(final String inListenerName) { mListenerName = inListenerName; }}

9o%e %$a%:• ($e M$Mule%&xceptionListener class ex%ends %$e Abstract&xceptionListener class

1ne conse0uence is %$a% %$ere are )our excep%ion&$andlin# me%$ods %$a% mus% beimplemen%ed, since %$ey are declared as abs%rac% in %$e paren% class

!83

Page 154: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 154/354

• ($e log&xception me%$od is overriddenince @e'll implemen% our o@n, more de%ailed, lo##in# o) excep%ions, %$e ori#inal lo##in# o)

%$e excep%ion $andler is comple%ely disabled by replacin# %$elog&xception me%$od @i%$ anemp%y me%$od

• Eac$ o) %$e excep%ion $andlin# me%$ods lo#s in)orma%ion %o %$e console($e in)orma%ion lo##ed depends on %$e %ype o) excep%ion $andled

• Eac$ o) %$e excep%ion $andlin# me%$ods calls %$eroute&xception me%$od($e route&xception me%$od rou%es %$e messa#e causin# %$e excep%ion %o one or moreendpoin%s declared in %$e cus%om&excep%ion&s%ra%e#yL elemen%I) %$ere are no endpoin%s %o rou%e %$e messa#e %o, any curren% %ransac%ion @ill be mar/ed )orrollbac/ by %$e route&xception me%$od

• ($e class $as an ins%ance variable, mListener1ame , @i%$ associa%ed #e%%er and se%%erme%$ods($is exposes a proper%y named Flis%ener9ameG, @$ic$ is used %o iden%i)y an ins%ance o) %$eexcep%ion lis%ener in lo# messa#es

Create the Mule 3.x Exception $istener

$en usin# Mule 3 x, %$ere are more op%ions available @$en implemen%in# a cus%om excep%ionlis%ener class ($e separa%ion be%@een messa#in# excep%ions and sys%em excep%ions are made moreclear and %$ere are more )ine&#rained op%ions available($e )ollo@in# op%ions are available implemen%in# an excep%ion lis%ener @$en usin# Mule 3 2

• .irec%ly in$eri% )rom org.mule.exception.Abstract&xceptionStrateg$"ommon paren% )or bo%$ messa#in# and sys%em excep%ion $andlers %$a% implemen%scommon be$aviour o) excep%ion $andlersI% is recommended %o consider %$e %ype o) excep%ion $andler one @an%s %o develop andin$eri% )rom ei%$er AbstractS$stem&xceptionStrateg$ or

AbstractMessaging&xceptionStrateg$• .irec%ly implemen% %$eorg.mule.api.exception.S$stem&xceptionHandler in%er)ace

ys%em excep%ions are considered %o be excep%ions %$a% are no% connec%ed %o %$e processin#o) a Mule messa#e 9o%e %$a% %$ehandle&xception me%$od only %a/es one sin#le parame%er an excep%ion

• .irec%ly in$eri% )rom org.mule.exception.AbstractS$stem&xceptionStrateg$"$ild class o) Abstract&xceptionStrateg$($is excep%ion s%ra%e#y class implemen%s basic $andlin# o) sys%em excep%ions

.irec%ly in$eri% )rom %$eorg.mule.exception.+efaultS$stem&xceptionStrateg$($is class implemen%s %$e de)aul% excep%ion s%ra%e#y )or sys%em excep%ions"$ild class o) AbstractS$stem&xceptionStrateg$

• .irec%ly implemen% %$eorg.mule.api.exception.Messaging&xceptionHandler in%er)aceA messa#in# excep%ion is an excep%ion %$a% occurred in connec%ion %o %$e processin# o) aMule messa#e($e even% ob4ec% re%urned by %$ehandle&xception me%$od is %$e even% %$a% is %o con%inue %o

be rou%ed %$rou#$ %$e remainin# par% o) %$e )lo@ ($is #ives %$e excep%ion $andler anoppor%uni%y %o modi)y, or even replace, %$e even% %$a% is passed on %$rou#$ %$e remainin#

par% o) %$e )lo@ a)%er %$e excep%ion occurred• .irec%ly in$eri% )rom org.mule.exception.AbstractMessaging&xceptionStrateg$

!85

Page 155: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 155/354

"$ild class o) Abstract&xceptionStrateg$Implemen%s basic $andlin# o) messa#in# excep%ions e%s %$e messa#e payload %o %$e null

payload and se%s %$e excep%ion payload %o %$e %$ro@n excep%ion• .irec%ly in$eri% )rom org.mule.exception.+efaultMessaging&xceptionStrateg$

"$ild class o) AbstractMessaging&xceptionStrateg$Implemen%s %$e de)aul% excep%ion s%ra%e#y )or sys%em excep%ions

• In$eri% )rom %$eorg.mule.tc .functional.:uiet&xceptionStrateg$ class1nly produce .E>BC&level lo# ou%pu% )or %$e di))eren% %ypes o) excep%ions

In %$e example pro#ram, @e @ill implemen% a messa#in# excep%ion s%ra%e#y by in$eri%in# )rom AbstractMessaging&xceptionStrateg$

• In %$e pac/a#e com.ivan.mule , implemen% %$e class M$Mule)&xceptionListener :

package com.ivan.mule;

import org.mule.api.MuleContext;import org.mule.api.MuleEvent;import org.mule.api.MuleMessage;

import org.mule.api.exception.RollbackSourceCallback;import org.mule.exception.AbstractMessagingExceptionStrategy;

import com.ivan.services.LoggingService;

/** * Custom Mule 3 exception listener. *

* @author Ivan Krizsan */public class MyMule3ExceptionListener extends AbstractMessagingExceptionStrategy{ private String mListenerName;

public MyMule3ExceptionListener(final MuleContext inMuleContext) { super(inMuleContext); }

@Override protected void logException(final Throwable inException) { /* Never log exceptions here. */ }

/* (non-Javadoc) * @seeorg.mule.exception.AbstractMessagingExceptionStrategy#doHandleException(java.lang.Exception, org.mule.api.MuleEvent, org.mule.api.exception.RollbackSourceCallback) */ @Override protected void doHandleException(final Exception inException, final MuleEvent inEvent,

final RollbackSourceCallback inRollbackMethod) { MuleMessage theMessage = inEvent.getMessage();

System.out.println("*** MyMule3ExceptionListener.doHandleException: " + mListenerName); LoggingService.logMessage(theMessage);

/* * Let the superclass handle transactions, routing etc of the * exception. */ super.doHandleException(inException, inEvent, inRollbackMethod); }

public String getListenerName() { return mListenerName;

!88

Page 156: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 156/354

}

public void setListenerName(String inListenerName) { mListenerName = inListenerName; }}

9o%e %$a%:• ($e excep%ion lis%ener class M$Mule)&xceptionListener ex%ends %$e class

AbstractMessaging&xceptionStrateg$"on%rary %o %$e Mule 2 x excep%ion lis%ener superclass, %$e Mule 3 x class

AbstractMessaging&xceptionStrateg$ does no% $ave any abs%rac% me%$ods %$a% @e arere0uired %o implemen% so @e 4us% override %$e me%$ods @$ic$ be$aviour @e @an% %o modi)y

• ($e log&xception me%$od is overriddenince @e'll implemen% our o@n, more de%ailed, lo##in# o) excep%ions, %$e ori#inal lo##in# o)

%$e excep%ion $andler is comple%ely disabled by replacin# %$elog&xception me%$od @i%$ anemp%y me%$od

• ($e doHandle&xception me%$od is overridden($e messa#e ou%pu%s in)orma%ion %o %$e console and invo/es %$e superclassdoHandle&xception me%$od ($e superclass me%$od $andles upda%in# o) any s%a%is%ics,rollbac/ o) an ac%ive %ransac%ion e%c

e could also $ave overridden %$e handle&xception me%$od @i%$ %$ree parame%ers• ($e class $as an ins%ance variable, mListener1ame , @i%$ associa%ed #e%%er and se%%er

me%$ods($is exposes a proper%y named Flis%ener9ameG, @$ic$ is used %o iden%i)y an ins%ance o) %$eexcep%ion lis%ener in lo# messa#es

!87

Page 157: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 157/354

: 8 Create the (ule Configuration )iles($is c$ap%er's example pro#ram uses a %o%al o) %$ree Mule con)i#ura%ion )ilesJ %@o )or %$e Mule 2 xversion and one )or %$e Mule 3 x version

($e Mule 2 x version uses, no% surprisin#ly, %$e Mule 2 x con)i#ura%ion s%yle, @i%$ a model %$a%

con%ains a number o) services ince model in$eri%ance is possible, a paren% model $as been de)inedin a separa%e )ile ($e paren% model de)ine a model&#lobal excep%ion lis%ener and %$e error lo##in#serviceIn %$e o%$er con)i#ura%ion )ile, amon# o%$er %$in#s, %$ere is a service @i%$ a service&local excep%ions%ra%e#y

($e Mule 3 x version uses a )lo@, ins%ead o) a model and services ince )lo@s do no% suppor%in$eri%ance, @e canno% declare a common excep%ion lis%ener In addi%ion, @$en usin# )lo@s, %$e only

place @$ere an excep%ion s%ra%e#y can be declared is inside a )lo@ $ile %$is can be seen as alimi%a%ion, i% does reduce ambi#ui%y

Create the Mule 2.x Configuration Files($e %@o Mule 2 x con)i#ura%ion )iles are %o be loca%ed in %$e pac/a#ecom.ivan.muleconfig

• "rea%e a )ile named Fmule2&excep%ions%ra%e#ymodel xmlG in %$e above men%ioned pac/a#eI% $as %$e )ollo@in# con%en%s:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.2" xmlns:spring="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd

http://www.mulesource.org/schema/mule/vm/2.2 http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- This model declares the model-global exception strategy and the error-logging service used by the exception strategy to log messages causing exceptions. --> <model name="MainModel"> <!-- An exception strategy, custom or default, is declared first in a model. Such an exception strategy is applied to all the services and components in the model.

If no customization of the exception strategy is required, use the <default-service-exception-strategy> element. --> <custom-exception-strategy class="com.ivan.mule.MyMule2ExceptionListener"> <!-- Messages caught by the exception strategy are sent to the endpoint(s) listed below. With Mule 2.x, multiple endpoints may be specified. --> <outbound-endpoint address="vm://errorLoggingServiceInbound"> <message-properties-transformer> <add-message-property key="exceptionListener" value="model-global"/> </message-properties-transformer> </outbound-endpoint> <!-- Name of the exception listener instance. Used when writing log messages to tell the different listeners apart.

!8

Page 158: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 158/354

--> <spring:property name="listenerName" value="Listener 1"/> </custom-exception-strategy>

<!-- Service that logs errors by printing the message payload and properties to the console. --> <service name="errorLoggingService">

<inbound> <vm:inbound-endpoint path="errorLoggingServiceInbound"synchronous="true"/> </inbound> <component class="com.ivan.services.LoggingService"/> </service> </model></mule>

9o%e %$a%:• ($e con)i#ura%ion )ile de)ine a model named FMainModelG• ($e model con%ains a cus%om excep%ion s%ra%e#y

A cus%om excep%ion s%ra%e#y allo@s us %o supply a cus%om implemen%a%ion class, usin# %$eclass a%%ribu%e o) %$e cus%om&excep%ion&s%ra%e#yL elemen% ($e excep%ion s%ra%e#y applies%o all services and componen%s in %$e model

• ($e cus%om excep%ion s%ra%e#y is implemen%ed by %$e classcom.ivan.mule.M$Mule%&xceptionListener

• ($e cus%om excep%ion s%ra%e#y is declared )irs% in %$e model• ($e cus%om&excep%ion&s%ra%e#yL elemen% con%ains a ou%bound&endpoin%L elemen%

($is causes messa#es cau#$% by %$e excep%ion s%ra%e#y %o be sen% %o %$e speci)ied endpoin%$en usin# Mule 2 x, mul%iple endpoin%s may be speci)ied

• ($e ou%bound&endpoin%L elemen% con%ains a messa#e&proper%ies&%rans)ormerL elemen%($e messa#e proper%ies %rans)ormer adds a messa#e proper%y @i%$ %$e nameFexcep%ion-is%enerG and %$e value Fmodel&#lobalG %o %$e Mule messa#e be)ore i% is passedon %o %$e ou%bound endpoin% ee %$e sec%ion onMessa#e *roper%ies in %$e re)erence par% o)%$is boo/ )or addi%ional de%ails on messa#e proper%ies

• ($e cus%om&excep%ion&s%ra%e#yL elemen% also con%ains a sprin#:proper%yL elemen%($e cus%om excep%ion s%ra%e#y is a specialized %ype o) prin# bean and @e can, in re#ular

prin# manner, in4ec% proper%y values in%o suc$ a bean• A service named Ferror-o##in# erviceG is declared in %$e model

($is service is a sync$ronous service %$a% receives messa#es over %$e VM %ranspor%,implemen%ed by %$e LoggingService class @e implemen%ed earlier

e are no@ ready %o crea%e %$e second Mule 2 x con)i#ura%ion )ile, @$ic$ con%ains %@o receivin#services, %$e <ello service and %$e service %$a% al@ays causes an excep%ion %o be %$ro@n

!8;

Page 159: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 159/354

Page 160: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 160/354

--> <service name="helloService"> <inbound> <vm:inbound-endpoint path="helloServiceInbound"/> </inbound> <component> <singleton-object class="com.ivan.services.HelloService"/> </component> </service>

<!-- Service with its own exception strategy. The service will throw an exception when being invoked. --> <service name="gotExceptionStrategyService"> <inbound> <inbound-endpoint address="vm://gotExceptionStrategyServiceURL"/> </inbound>

<component> <singleton-object class="com.ivan.services.ExceptionService"/> </component>

<!-- An exception strategy, custom or default, is declared last in a service and applies to all components in the service. This strategy will handle the exceptions that occur in the service, but not in other services to which messages are passed on. If no customization of the exception strategy is required, use the <default-service-exception-strategy> element. The exception strategy local to the service will be given precedence over the exception strategy defined on the model level. --> <custom-exception-strategy class="com.ivan.mule.MyMule2ExceptionListener"> <!-- Messages caught by the exception strategy are sent to the endpoint listed below. With Mule 2.x, multiple endpoints may be specified. --> <outbound-endpoint address="vm://errorLoggingServiceInbound"> <!-- Upon passing the message to the error logging service, set a message property. --> <message-properties-transformer> <add-message-property

key="exceptionListener" value="service-local"/> </message-properties-transformer> </outbound-endpoint> <!-- Name of the exception listener instance. Used when writing log, to tell the different listeners apart. --> <spring:property name="listenerName" value="Listener 2"/> </custom-exception-strategy> </service> </model></mule>

9o%e %$a%:• ($e above Mule con)i#ura%ion impor%s %$e Mule con)i#ura%ion %$a% @e previously de)ined• ($e con)i#ura%ion )ile de)ines a model named FMainModelG

($e model also $as %$e inherit a%%ribu%e se% %o %rue In order %o be able %o in$eri% )rom a paren% model, %$e paren% and c$ild models mus% $ave %$e same name

• ($e model con%ains a service named Freceiver erviceG($is service exposes an inbound endpoin% %$a% uses %$e VM %ranspor%($is is one o) %$e %@o endpoin%s %$a% can be seen in %$e )i#ure describin# %$eMule 2 x

!7

Page 161: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 161/354

con)i#ura%ion s%ruc%ure depic%ed in %$e be#innin# o) %$is c$ap%er• ($e ou%boundL elemen% o) %$e Freceiver erviceG uses an excep%ion based rou%er

An excep%ion based rou%er a%%emp%s %o send a messa#e %o %$e ou%bound endpoin%s lis%ed in aexcep%ion&based&rou%erL elemen%, %ryin# one endpoin% a% a %ime un%il i% )inds one %$a%

succeeds 6or de%ails on %$e excep%ion&based rou%er, please re)er %o %$e sec%ion onExcep%ion&.ependen% Messa#e Rou%in# in %$e re)erence sec%ion o) %$is boo/I% s$ould be no%ed %$a% all ou%bound endpoin%s in an excep%ion&based&rou%erL elemen%,excep% %$e las%, @ill be )orced %o be sync$ronous

• ($e las% ou%bound&endpoin%L elemen% in %$e excep%ion&based&rou%erL elemen% $as %$e s$nchronous a%%ribu%e se% %o %rue($e clien% o) a service, in %$is case %$e ou%bound&endpoin%L elemen% o) %$e excep%ion&

based&rou%erL, de%ermine @$e%$er %$e service is invo/ed sync$ronously or async$ronously($e sync$ronous a%%ribu%e on %$e service's inbound endpoin% is disre#arded($is is also %rue )or %$e services %$a% are exposed %o pro#ramma%ic clien%s

• ($e model con%ains a service named Fexcep%ion erviceG

($is is an ordinary service bac/ed by a sin#le ins%ance o) %$e class implemen%in# %$e serviceAs %$e Freceiver erviceG, i% also exposes an inbound endpoin% %$a% uses %$e VM %ranspor%e recall )rom @$en @e crea%ed %$e service implemen%a%ion class %$a% %$is service @ill

al@ays %$ro@ an excep%ion• ($e nex% service in %$e model is %$e F$ello erviceG

($is is %$e service %$a% @ill ex%end a #ree%in#, @i%$ou% a name, @$en invo/edI% also exposes an endpoin% %$a% uses %$e VM %ranspor%

• ($e nex% service, F#o%Excep%ion %ra%e#y erviceG, is similar %o %$e Fexcep%ion erviceGI% exposes an inbound endpoin% %$a% uses %$e VM %ranspor%, albei% @i%$ a di))eren% address($e %@o services uses %$e sameservice implemen%a%ion classJ %$a% @$ic$ @ill al@ays %$ro@

an excep%ion @$en invo/ed• ($ere is a cus%om&excep%ion&s%ra%e#yL in %$e F#o%Excep%ion %ra%e#y erviceG

($e F#o%Excep%ion %ra%e#y erviceG service de)ines a service&local cus%om excep%ions%ra%e#y uc$ an excep%ion s%ra%e#y is declared las% in %$e service and applies %o allcomponen%s in %$e service Any service&local excep%ion s%ra%e#y %a/es precedence over anyexcep%ion s%ra%e#y de)ined on %$e model level

• ($e cus%om excep%ion s%ra%e#y is implemen%ed by %$e classcom.ivan.mule.M$Mule%&xceptionListener

• ($e cus%om&excep%ion&s%ra%e#yL elemen% con%ains a ou%bound&endpoin%L elemen%

($is causes messa#es cau#$% by %$e excep%ion s%ra%e#y %o be sen% %o %$e speci)ied endpoin%$en usin# Mule 2 x, mul%iple endpoin%s may be speci)ied• ($e ou%bound&endpoin%L elemen% con%ains a messa#e&proper%ies&%rans)ormerL elemen%

($e messa#e proper%ies %rans)ormer adds a messa#e proper%y @i%$ %$e nameFexcep%ion-is%enerG and %$e value Fservice&localG %o %$e Mule messa#e be)ore i% is passed on%o %$e ou%bound endpoin% ee %$e sec%ion onMessa#e *roper%ies in %$e re)erence par% o) %$is

boo/ )or addi%ional de%ails on messa#e proper%ies• ($e cus%om&excep%ion&s%ra%e#yL elemen% also con%ains a sprin#:proper%yL elemen%

($e cus%om excep%ion s%ra%e#y is a specialized %ype o) prin# bean and @e can, in re#ularprin# manner, in4ec% proper%y values in%o suc$ a bean

!7!

Page 162: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 162/354

Create the Mule 3.x Configuration File

($e Mule 3 x con)i#ura%ion uses a number o) )lo@s, exposin# services )or ex%ernal and in%ernal useAll endpoin%s in %$e con)i#ura%ion )ile uses %$e re0ues%&response messa#e exc$an#e pa%%ern ($ereason )or %$is is mainly %o increase %$e readabili%y o) %$e ou%pu% #enera%ed by %$e example pro#ram

"rea%e a )ile named Fmule3&con)i# xmlG in %$e pac/a#ecom.ivan.muleconfig I% $as %$e)ollo@in# con%en%s:<?xml version="1.0" encoding="UTF-8"?><mule

xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:spring="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd

http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.2/mule-vm.xsd

http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- Flow that receives messages from an external source and passes them on for further processing. In Mule 3.x the exchange-pattern used by an endpoint is one-way as per default, except for in HTTP(S), TCP, SSL and servlet endpoints. As opposed to Mule 2.x, the exchange-pattern declared on the inbound endpoint decides which message exchange pattern will be used. --> <flow name="ReceiverFlow"> <inbound-endpoint address="vm://receiverServiceURL" exchange-pattern="request-response"/> <!-- The first-successful message processor attempts to send the message to the the contained endpoints in the order listed until having encountered an endpoint that succeeds. The exchange-pattern attribute of the outbound endpoints in the first-successful message processor can be set to "request-response" for better reliability, but may be "one-way" as well. --> <first-successful> <vm:outbound-endpoint path="exceptionServiceInbound"

exchange-pattern="request-response"/> <vm:outbound-endpoint path="helloServiceInbound"

exchange-pattern="request-response"/> </first-successful> </flow>

<!-- Flow that exposes a service that, each time a message is sent to it, will cause an exception to be thrown. --> <flow name="AlwaysExceptionFlow"> <inbound-endpoint address="vm://exceptionServiceInbound" exchange-pattern="request-response"/> <component> <singleton-object class="com.ivan.services.ExceptionService"/> </component> </flow>

<!-- Flow that exposes the Hello service. --> <flow name="HelloFlow"> <vm:inbound-endpoint path="helloServiceInbound" exchange-pattern="request-response"/>

!72

Page 163: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 163/354

<component> <singleton-object class="com.ivan.services.HelloService"/> </component> </flow>

<!-- Flow that exposes the error logging service. --> <flow name="ErrorLoggingFlow">

<vm:inbound-endpoint path="errorLoggingServiceInbound" exchange-pattern="request-response"/>

<component class="com.ivan.services.LoggingService"/> </flow>

<!-- Flow that exposes a service that will always throw an exception when being invoked. This flow has a custom exception listener. --> <flow name="GotExceptionStrategyFlow"> <inbound-endpoint address="vm://gotExceptionStrategyServiceURL" exchange-pattern="request-response"/>

<component> <singleton-object class="com.ivan.services.ExceptionService"/> </component>

<!-- This flow has a custom exception listener that produces more detailed information in the log about the message that was processed when the exception occurred. Exception strategies are declared in the end of a flow declaration. --> <custom-exception-strategy class="com.ivan.mule.MyMule3ExceptionListener"> <!-- Messages caught by the exception strategy are sent to the endpoint listed below. With Mule 3.x, only one single endpoint may be specified. --> <outbound-endpoint address="vm://errorLoggingServiceInbound"

exchange-pattern="request-response"> <!-- Add a message property to the message before sending it to the log service. --> <message-properties-transformer scope="outbound"> <add-message-property key="exceptionListener" value="In GotExceptionStrategyFlow"/> </message-properties-transformer> </outbound-endpoint> <!-- Name of the exception listener instance. Used when writing log messages to tell the different listeners apart. --> <spring:property name="listenerName" value="Listener 1"/> </custom-exception-strategy> </flow></mule>

9o%e %$a%:• ($e Mule con)i#ura%ion )ile con%ains )ive )lo@L elemen%s

e could re)ac%or %$e con)i#ura%ion %o $ave )e@er )lo@s, bu% %$is solu%ion @as deemed %o beappropria%e )or %$is example

• ($e )irs% )lo@ is named FReceiver6lo@G• ($e FReceiver6lo@G )lo@L elemen% con%ains an inbound&endpoin%L elemen%

!73

Page 164: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 164/354

($is elemen% exposes an inbound endpoin% usin# %$e VM %ranspor% @i%$ %$e addressFvm:==receiver erviceBR-G($is is one o) %$e %@o endpoin%s %$a% can be seen in %$e )i#ure describin# %$eMule 3 xcon)i#ura%ion s%ruc%ure depic%ed in %$e be#innin# o) %$is c$ap%er

• ($e inbound&endpoin%L elemen% discussed in above $as a exchange-pattern a%%ribu%e @i%$%$e value Fre0ues%&responseGIn Mule 3 x, %$e exc$an#e pa%%ern declared on %$e service and %$e exc$an#e pa%%ern used by%$e clien% mus% ma%c$, o%$er@ise %$ere may be unexpec%ed resul%sAlso, @e @an% %$e exposed service %o be sync$ronous, in order )or %$e clien% %o @ai% un%il%$ere is a resul% )rom %$e re0ues% available

• ($e FReceiver6lo@G )lo@L elemen% con%ains a )irs%&success)ulL elemen%($e )irs%&success)ulL elemen% de)ines a messa#e processor @$ic$ is similar %o %$eexcep%ion&based rou%er used in %$e Mule 2 x con)i#ura%ion in %$is c$ap%er6or de%ails on %$e )irs%&success)ul messa#e processor, please re)er %o %$e sec%ion onExcep%ion&.ependen% Messa#e Rou%in# in %$e re)erence sec%ion o) %$is boo/1u%bound in %$e )irs%&success)ul messa#e processor may be async$ronous or sync$ronous,%$ou#$ %$e la%%er @ill provide a $i#$er de#ree o) reliabili%y

• ($e )irs%&success)ulL elemen% con%ains %@o ou%bound endpoin%sMule 3 x does no% impose sync$ronici%y on %$e endpoin%s con%ained in %$e )irs%&success)ulLelemen% ($e endpoin%s in %$is example $ave %$eexchange-pattern a%%ribu%e se% %o Fre0ues%&responseG %o increase reliabili%y

• ($e nex% )lo@ is named FAl@aysExcep%ion6lo@GAs %$e name indica%es, i% exposes a service %$a% @ill %$ro@ an excep%ion eac$ %ime a messa#eis sen% %o i% ($e address o) %$e service is Fvm:==excep%ion erviceInboundG

• ($e nex% )lo@ is named F<ello6lo@G

($is )lo@ provides a service %$a% @ill ex%end a #ree%in#, @i%$ou% a name, @$en invo/edI% exposes an endpoin% %$a% uses %$e VM %ranspor%• ($e nex% )lo@, FError-o##in#6lo@G, provides a service %$a% lo#s errors

A#ain, %$e service exposed uses %$e VM %ranspor%• ($e las% )lo@ is named FCo%Excep%ion %ra%e#y6lo@G• ($e FCo%Excep%ion %ra%e#y6lo@G )lo@L elemen% con%ains an inbound&endpoin%L

elemen%($is elemen% exposes am inbound endpoin% usin# %$e VM %ranspor% @i%$ %$e addressFvm:==#o%Excep%ion %ra%e#y erviceBR-G

($is is one o) %$e %@o endpoin%s %$a% can be seen in %$e )i#ure describin# %$eMule 3 xcon)i#ura%ion s%ruc%ure depic%ed in %$e be#innin# o) %$is c$ap%er• ($e service in %$e FCo%Excep%ion %ra%e#y6lo@G @ill cause an excep%ion %o be %$ro@n eac$

%ime a messa#e is sen% %o %$e service• ($ere is a cus%om&excep%ion&s%ra%e#yL elemen% in %$e F Co%Excep%ion %ra%e#y6lo@G )lo@

($e excep%ion s%ra%e#y is declared a% %$e end o) %$e )lo@ and applies %o componen%s andservices o) %$e )lo@

• ($e cus%om excep%ion s%ra%e#y is implemen%ed by %$e classcom.ivan.mule.M$Mule)&xceptionListener

($e cus%om&excep%ion&s%ra%e#yL elemen% con%ains an ou%bound&endpoin%L elemen%

!75

Page 165: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 165/354

($is causes messa#es cau#$% by %$e excep%ion s%ra%e#y %o be sen% %o %$e speci)ied endpoin%$en usin# Mule 3 x, only one sin#le endpoin% may be speci)ied

• ($e ou%bound&endpoin%L elemen% con%ains a messa#e&proper%ies&%rans)ormerL elemen%($e messa#e proper%ies %rans)ormer adds a messa#e proper%y @i%$ %$e nameFexcep%ion-is%enerG and %$e value FIn Co%Excep%ion %ra%e#y6lo@G %o %$e Mule messa#e

be)ore i% is passed on %o %$e ou%bound endpoin% ee %$e sec%ion onMessa#e *roper%ies in %$ere)erence par% o) %$is boo/ )or addi%ional de%ails on messa#e proper%ies

• ($e cus%om&excep%ion&s%ra%e#yL elemen% also con%ains a sprin#:proper%yL elemen%($e cus%om excep%ion s%ra%e#y is a specialized %ype o) prin# bean and @e can in4ec%

proper%y values in%o suc$ a bean

!78

Page 166: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 166/354

: : +un the Example Programi%$ all %$e di))eren% par%s o) %$is c$ap%er's example pro#ram, @e are no@ ready %o run i% and

examine i%s be$aviour .ue %o %$e di))erences be%@een Mule 2 x and Mule 3 x, bo%$ versions o) %$eexample pro#ram @ill be examined in de%ail In addi%ion, eac$ version o) %$e pro#ram @ill be run%@ice, sendin# messa#es %o di))eren% endpoin%sRecall %$a% @e are usin# s%ar%er classes %o s%ar% %$e Mule 2 x and Mule 3 x versions o) %$e example

pro#ram

Run the Mule 2.x ersion of the Exa&ple 'rogra&

In %$is sec%ion, @e'll run %$e Mule 2 x version o) %$e example pro#ramEnsure %$a% your example pro#ram pro4ec% is con)i#ured @i%$ %$e Mule 2 x dis%ribu%ion on %$eclasspa%$ as described in appendix > , be)ore proceedin#

Sen/ a (essage to the )irst En/point

>e)ore s%ar%in# %$e Mule 2 x version o) %$e example pro#ram )or %$e )irs% %ime, open %$e class Mule%&xceptionHandling&xampleStarter and examine %$e sendMessage'oMule me%$od ($e BR- passed as %$e )irs% parame%er %o %$e send me%$od called is %o be MB-EP ERVI"E!PBR-, as in %$iscode snippe%:

... private MuleMessage sendMessageToMule(final MuleMessage inMessage, final MuleClient inMuleClient) throws Exception { MuleMessage theReceivedMsg;

/* Modify the URL of the service which to send the message here. */ theReceivedMsg = inMuleClient.send( MULE_SERVICE1_URL, inMessage); return theReceivedMsg; }...

($is BR- is vm:==receiver erviceBR-, @$ic$, i) you remember %$e )i#ure a% %$e s%ar% o) %$isc$ap%er , is %$e endpoin% exposed by %$e Freceiver erviceG service

• Ri#$%&clic/ on %$e Mule%&xceptionHandling&xampleStarter class and selec% Run As &L ?avaApplica%ion($ere @ill be a si#ni)ican% amoun% o) ou%pu% on %$e console, @e @ill loo/ a% %$e relevan%

par%s one&by&one

Examine the Output($is )irs% line indica%es %$a% %$e excep%ion service @as called and %$a% %$e excep%ion %$ro@n )rom %$eservice @ill con%ain %$e id ! in i%s messa#e*** In ExceptionService.onCall(): 1

($is sec%ion indica%e %$a% %$e excep%ion lis%ener implemen%ed in %$e M$Mule%&xceptionListenerclass received a no%i)ica%ion ($e no%i)ica%ion @as received by %$ehandleMessaging&xception ,@$ic$ means %$a% an excep%ion occurred durin# %$e processin# o) a messa#e ($e messa#e payload,messa#e proper%ies in %$e inbound and ou%bound scopes and excep%ion payload are lis%ed ($eexcep%ion lis%ener %$a% received %$e no%i)ica%ion $as %$e name F-is%ener !G & %$is is %$e excep%ion

lis%ener de)ined on %$e model&level

!77

Page 167: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 167/354

*** MyMule2ExceptionListener.handleMessagingException: Listener 1 Current time: 1328803879326 Message payload: I am a Mule message! Message properties:

INBOUND: {} OUTBOUND: {MULE_CORRELATION_GROUP_SIZE=2, MULE_ENCODING=UTF-8,MULE_CORRELATION_ID=b14c5888-5338-11e1-acc1-c97990807652,MULE_ENDPOINT=vm://exceptionServiceInbound,MULE_ORIGINATING_ENDPOINT=endpoint.vm.exceptionServiceInbound}

Exception payload: null NO EXCEPTION PAYLOAD AVAILABLE

($e )ollo@in# lo# ou%pu% is produced by Mule @$en rou%in# %$e excep%ione can no%e %$e )ollo@in# abou% %$e messa#e lo##ed:

• ($ere is no proper%y @i%$ %$e name Fexcep%ion-is%enerG in %$e messa#e's ou%bound scopeApparen%ly %$e messa#e proper%ies %rans)ormer $as no% been applied ye%

• ($e messa#e payload is a s%rin# H$i#$li#$%ed in yello@• ($e las% endpoin% %$a% received %$e messa#e $as %$e address

Fvm:==excep%ion erviceInboundG($is is indica%ed by %$e MB-EPE9.*1I9( messa#e proper%y in %$e ou%bound scope

($e MB-EP1RICI9A(I9CPE9.*1I9( messa#e proper%y also supply %$e samein)orma%ion, bu% in a di))eren% )orm[02-09 17:11:19] ERROR MyMule2ExceptionListener [main]: Message being processed is:org.mule.transport.DefaultMessageAdapter/org.mule.transport.DefaultMessageAdapter@15364ee5{id=b14c5888-5338-11e1-acc1-c97990807652, payload=java.lang.String,properties=Properties{invocation:{}, inbound:{}, outbound:{MULE_ENCODING=UTF-8,MULE_CORRELATION_GROUP_SIZE=2, MULE_CORRELATION_ID=b14c5888-5338-11e1-acc1-c97990807652,MULE_ENDPOINT=vm://exceptionServiceInbound,MULE_ORIGINATING_ENDPOINT=endpoint.vm.exceptionServiceInbound}, session:{}, },correlationId=b14c5888-5338-11e1-acc1-c97990807652, correlationGroup=2, correlationSeq=-1, encoding=UTF-8, exceptionPayload=null}

9ex%, %$e lo##in# service receives %$e messa#e and lo#s i%s con%en%s 9o%e %$a%:• ($ere no@ is a messa#e proper%y @i%$ %$e name Fexcep%ion-is%enerG in %$e messa#e's

ou%bound scope H$i#$li#$%ed in blue ($e proper%y $as %$e value Fmodel&#lobalG ($is %ellsus %$a% %$e excep%ion no%i)ica%ion @as received by %$e cus%om excep%ion s%ra%e#y de)ined in%$e model

• ($e messa#e payload is no@ an ins%ance o) &xceptionMessage H$i#$li#$%ed in yello@($is %ype o) messa#e @raps %$e messa#e %$a% caused %$e excep%ion and $olds in)orma%ion on%$e componen% in @$ic$ %$e excep%ion occurred and %$e endpoin% %$a% received %$e messa#eimmedia%ely prior %o %$e excep%ion @as %$ro@n

***** Logging service received a message: Current time: 1328803879336 Message payload: ExceptionMessage{message=I am a Mule message!,context={MULE_ENCODING=UTF-8, MULE_CORRELATION_GROUP_SIZE=2,MULE_CORRELATION_ID=b14c5888-5338-11e1-acc1-c979908

07652, MULE_ENDPOINT=vm://exceptionServiceInbound,MULE_ORIGINATING_ENDPOINT=endpoint.vm.exceptionServiceInbound}exception=org.mule.api.service.ServiceException: Component that caused exception is:SedaService{exceptionService}. Message payload is of type: String,componentName='exceptionService', endpointUri=vm://exceptionServiceInbound,timeStamp=Thu Feb 09 17:11:19 CET 2012} Message properties:

INBOUND: {} OUTBOUND: {MULE_ENCODING=UTF-8, MULE_CORRELATION_GROUP_SIZE=2,MULE_CORRELATION_ID=b14c5888-5338-11e1-acc1-c97990807652,MULE_ENDPOINT=vm://errorLoggingServiceInbound, MULE_REMOTE_SYNC=true,MULE_ORIGINATING_ENDPOINT=endpoint.vm.errorLoggingServiceInbound,exceptionListener=model-global} Exception payload: null NO EXCEPTION PAYLOAD AVAILABLE

($e )ollo@in# line %ells us %$a% %$e <ello service $as received and processed a messa#e e can %$us

!7

Page 168: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 168/354

conclude %$a% %$e excep%ion&based rou%er did i%s 4obJ )irs% i% a%%emp%ed %o send %$e messa#e %o %$eexcep%ion service $en %$a% )ailed, i% sen% %$e messa#e %o %$e <ello service*** In HelloService.onCall(): Hello. The time is now: Thu Feb 09 17:11:19 CET 2012

($is ou%pu% indica%es %$a% our s%ar%er&pro#ram received %$e response messa#e )rom Mule anddisplays %$e con%en%s o) %$e response messa#e

e can see %$a%:• ($e messa#e $as a payload H$i#$li#$%ed in yello@

($e payload is %$e s%rin# %$a% @as produced by %$e <ello service• ($ere is no excep%ion payload• ($e messa#e proper%y @i%$ %$e name Fexcep%ion-is%enerG is no% presen% in %$e messa#e

*** Finished invoking Mule configuration! Current time: 1328803879352 Message payload: Hello. The time is now: Thu Feb 09 17:11:19 CET 2012 Message properties:

INBOUND: {} OUTBOUND: {MULE_CORRELATION_GROUP_SIZE=2, MULE_ENCODING=UTF-8,

MULE_CORRELATION_ID=b14c5888-5338-11e1-acc1-c97990807652,MULE_ENDPOINT=vm://helloServiceInbound,MULE_ORIGINATING_ENDPOINT=endpoint.vm.helloServiceInbound}

Exception payload: null NO EXCEPTION PAYLOAD AVAILABLE

!7;

Page 169: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 169/354

Sen/ing (essage to the Secon/ En/point

A#ain, open %$e class Mule%&xceptionHandling&xampleStarter and examine %$esendMessa#e(oMule me%$od "$an#e %$e BR- passed as %$e )irs% parame%er %o %$e send me%$odcalled is %o be MB-EP ERVI"E2PBR-, as in %$is code snippe%:

... private MuleMessage sendMessageToMule(final MuleMessage inMessage, final MuleClient inMuleClient) throws Exception { MuleMessage theReceivedMsg;

/* Modify the URL of the service which to send the message here. */ theReceivedMsg = inMuleClient.send(MULE_SERVICE2_URL, inMessage); return theReceivedMsg; }...

($is BR- is vm:==#o%Excep%ion %ra%e#y erviceBR-, @$ic$, i) you remember %$e )i#ure a% %$e s%ar%o) %$is c$ap%er , is %$e endpoin% exposed by %$e F#e%Excep%ion %ra%e#y erviceG service

• Ri#$%&clic/ on %$e Mule2Excep%ion<andlin#Example %ar%er class and selec% Run As &L ?avaApplica%ion

e @ill loo/ a% %$e relevan% par%s o) %$e console ou%pu% one&by&one

Examine the Output

($is )irs% line indica%es %$a% %$e excep%ion service @as called and %$a% %$e excep%ion %$ro@n )rom %$eservice @ill con%ain %$e id ! in i%s messa#e*** In ExceptionService.onCall(): 1

($is sec%ion indica%e %$a% %$e me%$odhandleMessaging&xception in %$e excep%ion lis%enerimplemen%ed in %$e M$Mule%&xceptionListener class received a no%i)ica%ion ($e me%$od name %ellsus %$a% an excep%ion occurred durin# %$e processin# o) a messa#e ($e messa#e payload, messa#e

proper%ies in %$e inbound and ou%bound scopes and excep%ion payload are lis%edIns%ead o) %$e model&#lobal excep%ion lis%ener F-is%ener !G, %$e excep%ion lis%ener F-is%ener 2Gde)ined in %$e service F#o%Excep%ion %ra%e#y erviceG*** MyMule2ExceptionListener.handleMessagingException: Listener 2 Current time: 1328851964543 Message payload: I am a Mule message! Message properties:

INBOUND: {} OUTBOUND: {MULE_ENCODING=UTF-8,

MULE_ENDPOINT=vm://gotExceptionStrategyServiceURL,MULE_ORIGINATING_ENDPOINT=endpoint.vm.gotExceptionStrategyServiceURL}

Exception payload: null NO EXCEPTION PAYLOAD AVAILABLE

($e )ollo@in# lo# ou%pu% is produced by Mule @$en rou%in# %$e excep%ione can no%e %$e )ollo@in# abou% %$e messa#e lo##ed:

• ($ere is no proper%y @i%$ %$e name Fexcep%ion-is%enerG in %$e messa#e's ou%bound scope($e messa#e proper%ies %rans)ormer $as no% been applied ye%

• ($e messa#e payload is a s%rin# H$i#$li#$%ed in yello@• ($e las% endpoin% %$a% received %$e messa#e $as %$e address

Fvm:==#o%Excep%ion erviceBR-G($is is indica%ed by %$e MB-EPE9.*1I9( messa#e proper%y in %$e ou%bound scope($e MB-EP1RICI9A(I9CPE9.*1I9( messa#e proper%y also supply %$e same

!7+

Page 170: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 170/354

in)orma%ion, bu% in a di))eren% )orm[02-10 06:32:44] ERROR MyMule2ExceptionListener [main]: Message being processed is:org.mule.transport.DefaultMessageAdapter/org.mule.transport.DefaultMessageAdapter@34f34071{id=a5ae9aee-53a8-11e1-a213-9d6be1359575, payload=java.lang.String,properties=Properties{invocation:{}, inbound:{}, outbound:{MULE_ENCODING=UTF-8,MULE_ENDPOINT=vm://gotExceptionStrategyServiceURL,MULE_ORIGINATING_ENDPOINT=endpoint.vm.gotExceptionStrategyServiceURL}, session:{}, },correlationId=null, correlationGroup=-1, correlationSeq=-1, encoding=UTF-8,exceptionPayload=null}

<ere, %$e lo##in# service $as received %$e messa#e and lo#s i%s con%en%s 9o%e %$a%:• ($e messa#e payload is no@ an ins%ance o) &xceptionMessage H$i#$li#$%ed in yello@

($is %ype o) messa#e @raps %$e messa#e %$a% caused %$e excep%ion and $olds in)orma%ion on%$e componen% in @$ic$ %$e excep%ion occurred H$i#$li#$%ed in #reen and %$e endpoin% %$a%received %$e messa#e immedia%ely prior %o %$e excep%ion @as %$ro@n

• ($ere no@ is a messa#e proper%y @i%$ %$e name Fexcep%ion-is%enerG in %$e messa#e'sou%bound scope H$i#$li#$%ed in blue ($e proper%y $as %$e value Fservice&localG ($is %ellsus %$a% %$e excep%ion no%i)ica%ion @as received by %$e cus%om excep%ion s%ra%e#y de)ined in%$e service

***** Logging service received a message: Current time: 1328851964596 Message payload: ExceptionMessage{message=I am a Mule message!,context={MULE_ENCODING=UTF-8, MULE_ENDPOINT=vm://gotExceptionStrategyServiceURL,MULE_ORIGINATING_ENDPOINT=endpoint.vm.gotExceptionStrategyServiceURL}exception=org.mule.api.service.ServiceException: Component that caused exception is:SedaService{gotExceptionStrategyService}.Message payload is of type: String, componentName='gotExceptionStrategyService',endpointUri=vm://gotExceptionStrategyServiceURL, timeStamp=Fri Feb 10 06:32:44 CET 2012} Message properties:

INBOUND: {} OUTBOUND: {MULE_ENCODING=UTF-8, MULE_ENDPOINT=vm://errorLoggingServiceInbound,

MULE_REMOTE_SYNC=true,MULE_ORIGINATING_ENDPOINT=endpoint.vm.errorLoggingServiceInbound,exceptionListener=service-local}

Exception payload: null NO EXCEPTION PAYLOAD AVAILABLE

($e )inal par% o) %$e ou%pu% indica%es %$a% our s%ar%er&pro#ram received a response messa#e )romMule e can see %$a%:

• ($e messa#e $as a null payload H$i#$li#$%ed in yello@• ($ere is an excep%ion payload )rom @$ic$ @e also can ob%ain an excep%ion s%ac/%race

($e excep%ion payload %ype is $i#$li#$%ed in oran#e• ($e messa#e proper%y @i%$ %$e name Fexcep%ion-is%enerG is s%ill in %$e messa#e's ou%bound

scope H$i#$li#$%ed in blue• ($ere are messa#es, $i#$li#$%ed in #reen in %$e ou%pu% belo@, in %$e excep%ions

($is indica%e %$a% %$e excep%ion @i%$ id ! caused %$e excep%ion payload o) %$e messa#e

received by %$e s%ar%er pro#rame can also see %$a% %$e ou%er and inner excep%ions %$ro@n )rom %$e excep%ion service $as

re%ained %$eir rela%ion, @i%$ %$e ou%er excep%ion @rappin# %$e inner excep%ion*** Finished invoking Mule configuration! Current time: 1328851964623 Message payload: {NullPayload} Message properties:

INBOUND: {} OUTBOUND: {MULE_ENCODING=UTF-8, MULE_ENDPOINT=vm://errorLoggingServiceInbound,

MULE_REMOTE_SYNC=true,MULE_ORIGINATING_ENDPOINT=endpoint.vm.errorLoggingServiceInbound,exceptionListener=service-local}

Exception payload: org.mule.message.DefaultExceptionPayload@976484e*** EXCEPTION STACKTRACE START:org.mule.api.service.ServiceException: Component that caused exception is:SedaService{gotExceptionStrategyService}. Message payload is of type: String

!

Page 171: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 171/354

atorg.mule.component.DefaultLifecycleAdapter.invoke(DefaultLifecycleAdapter.java:216)

...at org.mule.module.client.MuleClient.send(MuleClient.java:595)at

com.ivan.starter.Mule2ExceptionHandlingExampleStarter.sendMessageToMule(Mule2ExceptionHandlingExampleStarter.java:99)

atcom.ivan.starter.Mule2ExceptionHandlingExampleStarter.doExample(Mule2ExceptionHandlingEx

ampleStarter.java:47)atcom.ivan.starter.Mule2ExceptionHandlingExampleStarter.main(Mule2ExceptionHandlingExampleStarter.java:28)Caused by: org.mule.api.DefaultMuleException: I am an outer exception with id 1

at com.ivan.services.ExceptionService.onCall(ExceptionService.java:26)... many more

Caused by: java.lang.Exception: I am a nested exception with id 1at com.ivan.services.ExceptionService.onCall(ExceptionService.java:24)... many more

*** EXCEPTION STACKTRACE END.

e $ave seen %$a% %$e excep%ions )rom a service can be $andled a% %@o di))eren% levels @$en usin#modelL and serviceL con)i#ura%ionsJ ei%$er on %$e model&level or on %$e service&level

e $ave also seen %$e di))eren% s%a#es o) %$e processin# o) an excep%ion, @i%$ any excep%ion lis%enerreceivin# %$e unal%ered messa#e, as i% @as a% %$e %ime @$en %$e excep%ion occurred, and enric$men%o) %$e messa#e be)ore i% @as sen% %o a lo##in# service by %$e excep%ion $andler

9o% direc%ly rela%ed %o excep%ion $andlin#, bu% never%$eless o) impor%ance is %$e observa%ion %$a% %$esender o) a messa#e decides @$e%$er %$e in%erac%ion @i%$ %$e endpoin% is %o be sync$ronous orasync$ronous ($is, o) course, under %$e assump%ion %$a% %$e %ranspor% used suppor%s bo%$ modes o)communica%ion

! !

Page 172: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 172/354

Run the Mule 3.x ersion of the Exa&ple 'rogra&

In %$is sec%ion, @e'll run %$e Mule 3 x version o) %$e example pro#ram>e)ore proceedin#, ensure %$a% your example pro#ram pro4ec% is con)i#ured @i%$ %$e Mule 3 xdis%ribu%ion on %$e classpa%$, as described inappendix > , be)ore proceedin#

Sen/ a (essage to the )irst En/point

>e)ore s%ar%in# %$e Mule 3 x version o) %$e example pro#ram )or %$e )irs% %ime, open %$e class Mule)&xceptionHandling&xampleStarter and examine %$e sendMessage'oMule me%$od ($e BR- passed as %$e )irs% parame%er %o %$e send me%$od called is %o be MB-EP ERVI"E!PBR-, as s$o@nin %$is code snippe%:

... private MuleMessage sendMessageToMule(final MuleMessage inMessage, final MuleClient inMuleClient) throws Exception { MuleMessage theReceivedMsg;

/* Modify the URL of the service which to send the message here. */ theReceivedMsg = inMuleClient.send( MULE_SERVICE1_URL, inMessage); return theReceivedMsg; }...

($is BR- is vm:==receiver erviceBR-, @$ic$, i) you recall %$e )i#ure a% %$e s%ar% o) %$is c$ap%er , is%$e endpoin% exposed by %$e Freceiver erviceG service

• Ri#$%&clic/ on %$e Mule)&xceptionHandling&xampleStarter class and selec% Run As &L ?avaApplica%ion($ere @ill be some ou%pu% on %$e console & @e @ill loo/ a% %$e relevan% par%s one&by&one

! 2

Page 173: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 173/354

Examine the Output

($is )irs% line indica%es %$a% %$e excep%ion service @as called and %$a% %$e excep%ion %$ro@n )rom %$eservice @ill con%ain %$e id ! in i%s messa#e

*** In ExceptionService.onCall(): 1

ince %$e Receiver6lo@ does no% declare an excep%ion s%ra%e#y, %$e de)aul% excep%ion s%ra%e#y isused @$en an excep%ion occurs %ryin# %o send a messa#e %o %$e service %$a% al@ays %$ro@s anexcep%ion 9o%e %$a%:

• ($e excep%ion s%ac/ is displayed($is is %$e $ierarc$y re%rieved @$en callin# get ause )irs% on %$e excep%ion and %$en onevery subse0uen% ob4ec% re%rieved by %$e get ause me%$od

• ($e roo% excep%ion s%ac/ %race is prin%ed($e roo% excep%ion is %$e excep%ion %$a% re%urn null )rom %$e get ause me%$od

e see %$a% %$is is %$e excep%ion %$a% @as @rapped @$en %$ro@n )rom %$e Excep%ion service[02-09 16:32:16] ERROR DefaultMessagingExceptionStrategy [main]:********************************************************************************Message : I am an outer exception with id 1Code : MULE_ERROR-10999--------------------------------------------------------------------------------Exception stack is:1. I am a nested exception with id 1 (java.lang.Exception) com.ivan.services.ExceptionService:24 (null)2. I am an outer exception with id 1 (org.mule.api.DefaultMuleException) com.ivan.services.ExceptionService:26(http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/DefaultMuleException.html)--------------------------------------------------------------------------------Root Exception stack trace:java.lang.Exception: I am a nested exception with id 1

at com.ivan.services.ExceptionService.onCall(ExceptionService.java:24)at

org.mule.model.resolvers.CallableEntryPointResolver.invoke(CallableEntryPointResolver.ja

va:50) atorg.mule.model.resolvers.DefaultEntryPointResolverSet.invoke(DefaultEntryPointResolverSet.java:39) + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' foreverything)********************************************************************************

($e )ollo@in# line %ells us %$a% %$e <ello service $as received and processed a messa#e e can %$usconclude %$a% %$e )irs%&success)ul messa#e processor did i%s 4obJ )irs% i% a%%emp%ed %o send %$emessa#e %o %$e excep%ion service $en %$a% )ailed, i% sen% %$e messa#e %o %$e <ello service*** In HelloService.onCall(): Hello. The time is now: Thu Feb 09 16:32:16 CET 2012

($is ou%pu% indica%es %$a% our s%ar%er&pro#ram received %$e response messa#e )rom Mule anddisplays %$e con%en%s o) %$e response messa#e ($e Mule session iden%i)ier $as been replaced @i%$F G %o conserve space

e can see %$a%:• ($e messa#e $as a payload H$i#$li#$%ed in yello@

($e payload is %$e s%rin# %$a% @as produced by %$e <ello service• ($ere is no excep%ion payload

*** Finished invoking Mule configuration! Current time: 1328801536926 Message payload: Hello. The time is now: Thu Feb 09 16:32:16 CET 2012 Message properties:

INBOUND: {MULE_SESSION=..., MULE_CORRELATION_SEQUENCE=-1,

MULE_CORRELATION_GROUP_SIZE=-1, MULE_ENCODING=UTF-8} OUTBOUND: {MULE_SESSION=..., MULE_CORRELATION_SEQUENCE=-1,

! 3

Page 174: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 174/354

MULE_CORRELATION_GROUP_SIZE=-1, MULE_ENCODING=UTF-8} Exception payload: null NO EXCEPTION PAYLOAD AVAILABLE

Sen/ing (essage to the Secon/ En/point

A#ain, open %$e class Mule)&xceptionHandling&xampleStarter and examine %$esendMessa#e(oMule me%$od "$an#e %$e BR- passed as %$e )irs% parame%er %o %$e send me%$odcalled is %o be MB-EP ERVI"E2PBR-, as in %$is code snippe%:

... private MuleMessage sendMessageToMule(final MuleMessage inMessage, final MuleClient inMuleClient) throws Exception { MuleMessage theReceivedMsg;

/* Modify the URL of the service which to send the message here. */ theReceivedMsg = inMuleClient.send(MULE_SERVICE2_URL, inMessage); return theReceivedMsg; }...

($is BR- is vm:==#o%Excep%ion %ra%e#y erviceBR-, @$ic$, i) you remember %$e )i#ure a% %$e s%ar%o) %$is c$ap%er , is %$e endpoin% exposed by %$e F#e%Excep%ion %ra%e#y erviceG service

• Ri#$%&clic/ %$e Mule)&xceptionHandling&xampleStarter class and selec% Run As &L ?avaApplica%ion

e @ill loo/ a% %$e relevan% par%s o) %$e console ou%pu% par%&by&par%

Examine the Output

($e Mule session iden%i)ier $as been replaced @i%$ F G in all %$e subse0uen% ou%pu% %o conservespace

($is )irs% line indica%es %$a% %$e excep%ion service @as called and %$a% %$e excep%ion %$ro@n )rom %$eservice @ill con%ain %$e id ! in i%s messa#e*** In ExceptionService.onCall(): 1

($is sec%ion indica%e %$a% %$e me%$oddoHandle&xception in %$e cus%om excep%ion lis%enerimplemen%ed in %$e M$Mule)&xceptionListener class received a no%i)ica%ion ($e messa#e payload,messa#e proper%ies in %$e inbound and ou%bound scopes and excep%ion payload are lis%ed

• ($e cus%om excep%ion lis%ener $as %$e name F-is%ener !Gince %$ere is only one sin#le )lo@ @i%$ a cus%om excep%ion s%ra%e#y, %$is is o) less

si#ni)icance•

($e Mule messa#e payload @i%$ i%s proper%ies are exac%ly as a% %$e %ime o) %$e excep%ione see %$a% %$e messa#e proper%y @i%$ %$e name Fexcep%ion-is%enerG is no% presen% in %$emessa#e, @$ic$ means %$a% %$e messa#e proper%ies %rans)ormer $as no% ye% been applied

• ($e las% endpoin% %$a% received %$e messa#e is %$e vm:==#o%Excep%ion %ra%e#y erviceBR-endpoin%, @$ic$ can be seen in %$e MB-EPE9.*1I9( andMB-EP1RICI9A(I9CPE9.*1I9( messa#e proper%ies

*** MyMule3ExceptionListener.doHandleException: Listener 1 Current time: 1329151241026 Message payload: I am a Mule message! Message properties:

INBOUND: {MULE_SESSION=..., MULE_ENDPOINT=vm://gotExceptionStrategyServiceURL,MULE_ORIGINATING_ENDPOINT=endpoint.vm.gotExceptionStrategyServiceURL}

OUTBOUND: {MULE_CORRELATION_SEQUENCE=-1, MULE_CORRELATION_GROUP_SIZE=-1,MULE_ENCODING=UTF-8}

Exception payload: null

! 5

Page 175: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 175/354

NO EXCEPTION PAYLOAD AVAILABLE

($e )ollo@in# lo# ou%pu% is produced by Mule @$en rou%in# %$e excep%ion[02-13 17:40:41] ERROR MyMule3ExceptionListener [main]: Message being processed is: I ama Mule message!

($is ou%pu% s$o@s %$a% %$e lo##in# service $as received %$e messa#e 9o%e %$a%:• ($e messa#e payload is no@ an ins%ance o) &xceptionMessage H$i#$li#$%ed in yello@

($is %ype o) messa#e @raps %$e messa#e %$a% caused %$e excep%ion and $olds in)orma%ion on%$e componen% in @$ic$ %$e excep%ion occurred H$i#$li#$%ed in #reen and %$e endpoin% %$a%received %$e messa#e immedia%ely prior %o %$e excep%ion @as %$ro@n H$i#$li#$%ed in #rey

• ($ere no@ is a messa#e proper%y @i%$ %$e name Fexcep%ion-is%enerG in %$e messa#e'sou%bound scope H$i#$li#$%ed in blue ($e proper%y $as %$e value FInCo%Excep%ion %ra%e#y6lo@G ($is %ells us %$a% %$e excep%ion no%i)ica%ion @as received by %$ecus%om excep%ion s%ra%e#y de)ined in %$e )lo@ FCo%Excep%ion %ra%e#y6lo@ F

***** Logging service received a message: Current time: 1329151241050 Message payload: ExceptionMessage{payload=I am a Mule message!,context={MULE_CORRELATION_SEQUENCE=-1, MULE_CORRELATION_GROUP_SIZE=-1,MULE_ENCODING=UTF-8}exception=org.mule.component.ComponentException: Component thatcaused exception is:DefaultJavaComponent{GotExceptionStrategyFlow.commponent.486001617}.Message payload is of type: String, componentName='GotExceptionStrategyFlow',endpointUri=vm://gotExceptionStrategyServiceURL, timeStamp=Mon Feb 13 17:40:41 CET 2012} Message properties:

INBOUND: {MULE_SESSION=..., MULE_CORRELATION_SEQUENCE=-1,MULE_CORRELATION_GROUP_SIZE=1, MULE_ENCODING=UTF-8,MULE_ENDPOINT=vm://errorLoggingServiceInbound,MULE_ORIGINATING_ENDPOINT=endpoint.vm.errorLoggingServiceInbound,exceptionListener=In GotExceptionStrategyFlow}

OUTBOUND: {MULE_CORRELATION_SEQUENCE=-1, MULE_CORRELATION_GROUP_SIZE=1,MULE_ENCODING=UTF-8} Exception payload: null NO EXCEPTION PAYLOAD AVAILABLE

As @i%$ %$e console ou%pu% @e $ave seen previously, %$e )inal par% o) %$e ou%pu% indica%es %$a% %$es%ar%er&pro#ram received a response messa#e )rom Mule e can see %$a%:

• ($e messa#e $as a null payload H$i#$li#$%ed in yello@• ($ere is an excep%ion payload )rom @$ic$ @e also can ob%ain an excep%ion s%ac/%race

($e excep%ion payload %ype is $i#$li#$%ed in oran#e• ($e messa#e proper%y @i%$ %$e name Fexcep%ion-is%enerG is no lon#er in %$e messa#e's

ou%bound scope($e ori#inal messa#e $as been replaced @i%$ a messa#e @i%$ a null payload and anexcep%ion payload

• ($ere are messa#es, $i#$li#$%ed in #reen in %$e ou%pu% belo@, in %$e excep%ions($is indica%e %$a% %$e excep%ion @i%$ id ! caused %$e excep%ion payload o) %$e messa#ereceived by %$e s%ar%er pro#ram

e can also see %$a% %$e ou%er and inner excep%ions %$ro@n )rom %$e excep%ion service $asre%ained %$eir rela%ion, @i%$ %$e ou%er excep%ion @rappin# %$e inner excep%ion

*** Finished invoking Mule configuration! Current time: 1329151241112 Message payload: {NullPayload} Message properties:

INBOUND: {MULE_CORRELATION_SEQUENCE=-1, MULE_CORRELATION_GROUP_SIZE=-1,MULE_ENCODING=UTF-8}

OUTBOUND: {MULE_SESSION=..., MULE_CORRELATION_SEQUENCE=-1,MULE_CORRELATION_GROUP_SIZE=-1, MULE_ENCODING=UTF-8}

Exception payload: org.mule.message.DefaultExceptionPayload@6c267f18*** EXCEPTION STACKTRACE START:

! 8

Page 176: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 176/354

org.mule.component.ComponentException: Component that caused exception is:DefaultJavaComponent{GotExceptionStrategyFlow.commponent.486001617}. Message payload isof type: String

atorg.mule.component.DefaultComponentLifecycleAdapter.invoke(DefaultComponentLifecycleAdapter.java:359)

...at

com.ivan.starter.Mule3ExceptionHandlingExampleStarter.sendMessageToMule(Mule3ExceptionHa

ndlingExampleStarter.java:112)atcom.ivan.starter.Mule3ExceptionHandlingExampleStarter.doExample(Mule3ExceptionHandlingExampleStarter.java:57)

atcom.ivan.starter.Mule3ExceptionHandlingExampleStarter.main(Mule3ExceptionHandlingExampleStarter.java:35)Caused by: org.mule.api.DefaultMuleException: I am an outer exception with id 1

at com.ivan.services.ExceptionService.onCall(ExceptionService.java:26)... many more

Caused by: java.lang.Exception: I am a nested exception with id 1at com.ivan.services.ExceptionService.onCall(ExceptionService.java:24)... many more

*** EXCEPTION STACKTRACE END.

e $ave observed %$a% a )lo@ @ill al@ays $ave an excep%ion s%ra%e#y i) i% is no% explici%ly de)ined,%$en i% @ill be a de)aul% excep%ion s%ra%e#ye $ave also seen %$e di))eren% s%a#es o) %$e processin# o) an excep%ion, @i%$ any excep%ion lis%ener

receivin# %$e unal%ered messa#e, as i% @as a% %$e %ime @$en %$e excep%ion occurred, and enric$men%o) %$e messa#e be)ore i% @as sen% %o a lo##in# service by %$e excep%ion $andler

In Mule 3 x, nei%$er sender nor receiver o) a messa#e sin#le&$andedly decide %$e messa#e exc$an#e pa%%ern

: 1; Exercises($ere are some di))erences re#ardin# $o@ Mule 2 x and Mule 3 x in%erpre% messa#e exc$an#e

pa%%erns )or endpoin%s A su##es%ed exercise is %o experimen% @i%$ di))eren% messa#e exc$an#e pa%%erns on inbound& and ou%bound&endpoin%s and no%e %$e order in @$ic$ %$e console ou%pu%appears 9o%e @$e%$er %$e exc$an#e&pa%%ern in %$e sender and receiver mus% ma%c$ in order )or %$eservice %o be invo/edMule 2 x uses %$e s$nchronous a%%ribu%e, @$ile Mule 3 x $as %$e e xchange-pattern a%%ribu%e

! 7

Page 177: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 177/354

18. Mule 'rogra&&atic 5se and Message 'roperties 9o% only can @e use Mule as an E > %$a%, a)%er $avin# rou%ed, %rans)ormed and )il%ered a messa#e,invo/es a service @e $ave developed Mule can also be used as a buildin#&bloc/ o) applica%ions@$ere %$e applica%ion invo/es di))eren% Mule con)i#ura%ions a% di))eren% s%a#es($e )ollo@in# pro#ram s$o@s $o@ %@o di))eren% Mule con)i#ura%ions are pro#ramma%icallycombined:

• A messa#e is sen% %o %$e )irs% Mule con)i#ura%ion• ($e resul%in# messa#e is used as inpu% %o %$e second Mule con)i#ura%ion• ($e messa#e received as a resul% o) invo/in# %$e second Mule con)i#ura%ion is

pro#ramma%ically examined and %$e resul% prin%ed %o %$e console

In addi%ion, @e @ill also loo/ a% messa#e proper%ies and $o@ %$eir be$aviour di))er be%@een Mule2 x and Mule 3 x

1; 1 %ntro/uction to (essage PropertiesMessa#e proper%ies are addi%ional in)orma%ion consis%in# o) a /ey and a value %$a% can be enclosed@i%$ Mule messa#es, li/e <((* $eaders @i%$ <((* re0ues%s I% is even %$e case %$a% Mule messa#e

proper%ies @ill be %rans)ormed in%o <((* $eaders @$en a Mule messa#e is send over %$e <((*%ranspor% pro%ocolIn a Mule messa#e %$ere are )ive di))eren% scopes, as de)ined in %$e Propert$Scope class:

• A**-I"A(I19Read&only scope providin# access %o proper%ies in %$e Mule re#is%ry

9o% enabled by de)aul% 6or more in)orma%ion, please re)er %o %$e Mule A*I documen%a%iono) %$e Mule#egistr$ in%er)ace

• I9>1B9.*roper%ies )rom clien% re0ues%s

• I9V1"A(I19-as%s durin# %$e processin# o) a service invoca%ion (ypically only used in%ernally by Mule

• 1B(>1B9.*roper%ies re%urned )rom clien% re0ues%s

• E I19-as%s durin# a session spannin# mul%iple re0ues%s I) a %ranspor% pro%ocol @i%$ sessionseman%ics is used, Mule @ill use %$is mec$anism )or session mana#emen%J )or example<((* sessions 1%$er@ise an in%ernal session mec$anism @ill be used

In Mule 2 x, Mule @ill no% move proper%ies be%@een %$e di))eren% scopes ($e opposi%e is %rue )orMule 3 xJ proper%ies @ill be moved be%@een scopes by Mule e @ill see examples o) bo%$ /inds o)

be$aviour in %$is c$ap%er's example pro#ram

!

Page 178: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 178/354

1; " Create the Pro!ect"rea%e %$e pro4ec% as described in %$e appendix"rea%e a Mule *ro4ec%, namin# i%FMule*ro#ramma%icBseG ($e Mule 3 $o% deploymen% can be s@i%c$ed o)) )rom %$e s%ar%, as %$is)ea%ure is no% o) use @$en runnin# Mule embedded

1; & Create the (ule Configuration )iles($is example con%ains %@o Mule con)i#ura%ion )iles per Mule version, )or a %o%al o) )our Mulecon)i#ura%ion )iles($e %@o con)i#ura%ion )iles )or a Mule version are 0ui%e similar %$e only di))erences are %$e valuess%ored in %$e messa#e proper%y and %$e da%a appended %o %$e messa#e payload ($us only one o) %$econ)i#ura%ion )iles @ill be commen%ed upon

All %$e Mule con)i#ura%ion )iles reside in one and %$e same pac/a#e in %$e source direc%ory:• "rea%e %$e pac/a#e com.ivan.muleconfig in %$e roo% o) %$e pro4ec%'s source direc%ory

Mule 2.x Configuration Files

($e )irs% Mule 2 x con)i#ura%ion )ile is named Fmule2&con)i# xmlG and is loca%ed in %$e pac/a#ecom.ivan.muleconfig crea%ed earlier

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.2" xsi:schemaLocation=" http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd

http://www.mulesource.org/schema/mule/vm/2.2 http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd">

<model name="myMuleModel"> <service name="myMuleService"> <inbound> <inbound-endpoint address="vm://myMuleServiceURL"> <message-properties-transformer> <!-- The expression in the value attribute is used to append the "[set by config1]" string to the current value of the message property with the name "MuleProperty2". --> <add-message-property key="MuleProperty2" value="#[string:#[header:MuleProperty2]#[string:[set byconfig1]]]"/>

</message-properties-transformer> <!-- Append a string to the payload of the message. --> <append-string-transformer message="[string appended by first config]"/> </inbound-endpoint> </inbound> </service> </model></mule>

! ;

Page 179: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 179/354

9o%e %$a%:• ($e con)i#ura%ion )ile con%ains one module @i%$ a sin#le service• ($e address a%%ribu%e o) %$e service's inbound endpoin% is Fvm:==myMule erviceBR-G

($is is %$e BR- @e @ill la%er use in our pro#ram %o send messa#es %$a% are %o be processed by %$e service de)ined in %$is con)i#ura%ion )ile

• ($e vm:inbound&endpoin%L elemen% may be used ins%ead o) %$e inbound&endpoin%Lelemen% @i%$ %$e same )unc%ionali%y

• ($e inbound&endpoin%L elemen% con%ains a messa#e&proper%ies&%rans)ormerL elemen%As seen in an earlier example, %$is %rans)ormer is used %o manipula%e messa#e proper%ies

• ($e messa#e&proper%ies&%rans)ormerL does no% speci)y a scope on @$ic$ %o opera%e($e Mule 2 2 ! messa#e proper%ies %rans)ormer only opera%es on %$e 1B(>1B9. scope,re#ardless o) $o@ i% is con)i#ured

• In %$e messa#e&proper%ies&%rans)ormerL elemen%, %$ere is a add&messa#e&proper%yLelemen% %$a% adds a proper%y @i%$ %$e name FMule*roper%y2G %o %$e proper%ies o) a messa#e

• ($e value a%%ribu%e o) %$e add&messa#e&proper%yL elemen% con%ains a lon# expression %$a%s$ould be in%erpre%ed as )ollo@s:"as% %$e )ollo@in# as a s%rin#: %$e value o) %$e messa#e $eader proper%y namedFMule*roper%y2G )ollo@ed by cas% %$e )ollo@in# as a s%rin#: FUse% by con)i#! G

• 6inally, %$e inbound&endpoin%L elemen% con%ains a append&s%rin#&%rans)ormerL elemen%As %$e name o) %$e elemen% indica%e, %$is is a messa#e %rans)ormer %$a% appends a s%rin# ($es%rin# is appended %o %$e payload o) messa#es passin# %$rou#$ %$e %rans)ormer

($e second Mule 2 x con)i#ura%ion )ile is named Fmule2&con)i#2 xmlG and is also loca%ed in %$ecom.ivan.muleconfig pac/a#e ($e con)i#ura%ion is iden%ical %o %$e )irs% Mule 2 x con)i#ura%ion )ileexcep% )or some values In addi%ion, commen%s $ave been le)% ou%

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.2" xsi:schemaLocation=" http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd

http://www.mulesource.org/schema/mule/vm/2.2 http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd">

<model name="myMuleModel">

<service name="myMuleService"> <inbound> <inbound-endpoint address="vm://myMuleServiceURL"> <message-properties-transformer> <add-message-property key="MuleProperty2" value="#[string:#[header:MuleProperty2]#[string:[set byconfig2]]]" /> </message-properties-transformer> <append-string-transformer message="[string appended by second config]"/> </inbound-endpoint> </inbound> </service> </model></mule>

! +

Page 180: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 180/354

Mule 3.x Configuration Files

In %$e Mule 3 x con)i#ura%ion )iles a )lo@ is used ins%ead o) %$e model&service cons%ruc% seen in %$eMule 2 x con)i#ura%ion )iles($e )irs% Mule 3 x con)i#ura%ion )ile is named Fmule3&con)i# xmlG and is loca%ed in %$e pac/a#ecom.ivan.muleconfig :

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd

http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.2/mule-vm.xsd">

<flow name="myMuleFlow"> <!-- In Mule 3.2 the exchange-pattern must explicitly be set to request-response, since the default is one-way. If the one-way exchange pattern is used, null messages will be received as "response". --> <inbound-endpoint address="vm://myMuleServiceURL" exchange-pattern="request-response"> <!-- Note how a scope, in which message properties are manipulated, can be specified on the message properties transformer. The default scope is the outbound scope. --> <message-properties-transformer scope="outbound"> <!-- The expression in the value attribute is used to append the "[set by config1]" string to the current value of the message property with the name "MuleProperty2". Note how a message property scope now can be specified in the expression retrieving the property value. The default scope, if none is supplied, is OUTBOUND. --> <add-message-property key="MuleProperty2"

value="#[string:#[header:INBOUND:MuleProperty2]#[string:[set byconfig1]]]" /> </message-properties-transformer> <!-- Append a string to the payload of the message. --> <append-string-transformer message="[string appended by first config]"/> </inbound-endpoint> <echo-component/> </flow></mule>

9o%e %$a%:• ($e )lo@L elemen% con%ains an inbound&endpoin%L elemen% @$ic$address a%%ribu%e $as

%$e value Fvm:==myMule erviceBR-G($is is %$e BR- @e @ill la%er use in our pro#ram %o send messa#es %$a% are %o be processed

by %$e )lo@ de)ined in %$is con)i#ura%ion )ile• ($e inbound&endpoin%L elemen% $as a proper%y namedexchange-pattern @i%$ %$e value

Fre0ues%&responseG*er de)aul%, inbound endpoin%s in Mule 3 2 are are one&@ay, %$a% is, @ill no% re%urn anyresponse I) @e do no% modi)y %$e exc$an#e pa%%ern, @e @ill receive a null messa#e asresponse ($e de)aul% value may vary be%@een di))eren% versions o) Mule, so @$en in doub%,

!;

Page 181: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 181/354

speci)y a value• ($e inbound&endpoin%L elemen% con%ains a messa#e&proper%ies&%rans)ormerL elemen%

As seen in an earlier example, %$is %rans)ormer is used %o manipula%e messa#e proper%ies• ($e vm:inbound&endpoin%L elemen% may be used ins%ead o) %$e inbound&endpoin%L

elemen% @i%$ %$e same )unc%ionali%y• ($e messa#e&proper%ies&%rans)ormerL $as a scope a%%ribu%e @i%$ %$e value Fou%boundG

As opposed %o %$e Mule 2 x messa#e proper%ies %rans)ormer, %$e Mule 3 x version is scope&a@are Bsin# %$e scope a%%ribu%e, @e can speci)y %$e messa#e proper%ies scope on @$ic$ %$eelemen%s con%ained in %$e %rans)ormer opera%es on($e de)aul% scope is 1B(>1B9. scope

• In %$e messa#e&proper%ies&%rans)ormerL elemen%, %$ere is a add&messa#e&proper%yLelemen% %$a% adds a proper%y @i%$ %$e name FMule*roper%y2G %o %$e proper%ies o) a messa#e

• ($e value a%%ribu%e o) %$e add&messa#e&proper%yL elemen% con%ains a lon# expression %$a%s$ould be in%erpre%ed as )ollo@s:

"as% %$e )ollo@in# as a s%rin#: %$e value o) %$e messa#e $eader proper%y namedFMule*roper%y2G )rom %$e I9>1B9. scope )ollo@ed by cas% %$e )ollo@in# as a s%rin#:FUse% by con)i#! G

• ($e expression in %$e value a%%ribu%e o) %$e add&messa#e&proper%yL speci)ies )rom @$ic$messa#e proper%y scope %$e value s$ould be read ($is is ne@ )or Mule 3 x($e de)aul% scope is 1B(>1B9. scope

• 6inally, %$e inbound&endpoin%L elemen% con%ains a append&s%rin#&%rans)ormerL elemen%As %$e name o) %$e elemen% indica%e, %$is is a messa#e %rans)ormer %$a% appends a s%rin# ($es%rin# is appended %o %$e payload o) messa#es passin# %$rou#$ %$e %rans)ormer

($e second Mule 3 x con)i#ura%ion )ile is named Fmule3&con)i#2 xmlG and is also loca%ed in %$ecom.ivan.muleconfig pac/a#e ($e con)i#ura%ion is iden%ical %o %$e )irs% Mule 3 x con)i#ura%ion )ileexcep% )or some values appended %o %$e messa#e proper%y and %$e payload In addi%ion, commen%s$ave been le)% ou%

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd

http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.2/mule-vm.xsd">

<flow name="myMuleFlow"> <inbound-endpoint address="vm://myMuleServiceURL" exchange-pattern="request-response"> <message-properties-transformer> <add-message-property key="MuleProperty2" value="#[string:#[header:INBOUND:MuleProperty2]#[string:[set byconfig2]]]" /> </message-properties-transformer> <append-string-transformer message="[string appended by second config]"/> </inbound-endpoint> <echo-component/> </flow>

</mule>

!;!

Page 182: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 182/354

1; ' Create the Starter Classes($is c$ap%er's example pro#ram @ill use %@o classes, one )or eac$ version o) Mule, %$a% s%ar%s %$e%@o con)i#ura%ions up and sends a messa#e %o %$e )irs% one and %$en %o %$e second oneAs @e @ill see, %$ere are some si#ni)ican% di))erences be%@een %$e %@o s%ar%er classes rela%ed %o $o@messa#e proper%ies are $andled in Mule 2 x and Mule 3 x6irs% @e'll crea%e a pac/a#e )or %$e s%ar%er classes:

• "rea%e %$e pac/a#e com.ivan.starter in %$e roo% o) %$e pro4ec%'s source direc%ory

Create the Mule 2.x Starter Class

($e implemen%a%ion o) %$e Mule 2 x s%ar%er class is 0ui%e s%rai#$%)or@ard• "rea%e a class named MuleProgrammatic0seStarter% implemen%ed as %$is:

package com.ivan.starter;

import org.mule.DefaultMuleMessage;import org.mule.api.MuleContext;import org.mule.api.MuleException;import org.mule.api.MuleMessage;import org.mule.api.transport.PropertyScope;import org.mule.config.spring.SpringXmlConfigurationBuilder;import org.mule.context.DefaultMuleContextFactory;import org.mule.module.client.MuleClient;

/** * This class invokes two Mule configurations in sequence, * passing the result of the first invocation as input to the * second. * Version for Mule 2.x. *

* @author Ivan Krizsan */public class MuleProgrammaticUseStarter2

{ /* Constant(s): */ private final static String MULE_CONFIG_FILE1 = "com/ivan/muleconfig/mule2-config.xml"; private final static String MULE_CONFIG_FILE2 = "com/ivan/muleconfig/mule2-config2.xml"; private final static String MULE_SERVICE_URL = "vm://myMuleServiceURL"; private final static String MESSAGE_PROPERTY_NAME = "MuleProperty2";

/* Instance variable(s): */

public static void main(String[] args) throws Exception { (new MuleProgrammaticUseStarter2()).doExample(); }

private void doExample() { MuleContext theContext1 = null; MuleContext theContext2 = null; try { MuleClient theMuleClient; /* * Create a new message, setting its payload and one * message property. * The message property is placed in the OUTBOUND scope. */ MuleMessage theMuleMessage = new DefaultMuleMessage( "[original message payload]"); theMuleMessage.setProperty(MESSAGE_PROPERTY_NAME, "[Set by program]", PropertyScope.OUTBOUND);

/* Start Mule context with the first configuration file. */

!;2

Page 183: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 183/354

theContext1 = startMule(MULE_CONFIG_FILE1);

/** Examine the message before the message has been processed

* by the first Mule configuration. * This is done after having started the Mule server, in order * for a Mule server context to be present. */ System.out.println("***** Before first invocation:");

logMessageContents(theMuleMessage); /* Create a Mule client and send the message to it. */

theMuleClient = createMuleClient(theContext1); theMuleMessage = sendMessageToMule(theMuleMessage, theMuleClient);

/** Examine the message after the message has been processed

* by the first Mule configuration. */ System.out.println("***** After first invocation:"); logMessageContents(theMuleMessage);

/* * Start a new Mule context with the second configuration file, * create a client and send the message received from the * above invocation to it. * No need to move message properties between scopes with * Mule 2.x. */ theContext2 = startMule(MULE_CONFIG_FILE2); theMuleClient = createMuleClient(theContext2); theMuleMessage = sendMessageToMule(theMuleMessage, theMuleClient);

/** Examine the message after the message has been processed

* by the second Mule configuration. */ System.out.println("***** After second invocation:"); logMessageContents(theMuleMessage); } catch (final Exception theException) { theException.printStackTrace(); } finally { /* * Dispose of the Mule contexts. Disposing the context * automatically stops it and we do not have to bother * with the exception declared by the stop method. * Must dispose if we wish the application to terminate, * otherwise there will be unreleased resources. */ if (theContext1 != null) { theContext1.dispose(); } if (theContext2 != null) { theContext2.dispose(); } }

}

private MuleContext startMule(final String inMuleConfigFile) throws MuleException { /* * Starts an instance of Mule configured according to the supplied * configuration file. */ String[] theConfigFiles = { inMuleConfigFile };

DefaultMuleContextFactory theContextFactory = new DefaultMuleContextFactory(); SpringXmlConfigurationBuilder theConfigBuilder = new SpringXmlConfigurationBuilder(theConfigFiles); MuleContext theMuleContext =

theContextFactory.createMuleContext(theConfigBuilder);

!;3

Page 184: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 184/354

theMuleContext.start();

return theMuleContext; }

private MuleClient createMuleClient(final MuleContext inMuleContext) throws MuleException { MuleClient theMuleClient = new MuleClient(inMuleContext);

return theMuleClient; }

private MuleMessage sendMessageToMule(final MuleMessage inMessage, final MuleClient inMuleClient) throws Exception { MuleMessage theReceivedMsg;

theReceivedMsg = inMuleClient.send(MULE_SERVICE_URL, inMessage); return theReceivedMsg; }

private void logMessageContents(final MuleMessage inMessage) throws Exception { /* * The regular approach to programmatically retrieving message * properties with Mule 2.x is: * theMsgPropertyValue = theMuleMessage.getStringProperty( * MESSAGE_PROPERTY_NAME, "default value"); * This would search for the particular property in all the * Mule scopes. * However, to stress the difference with Mule 3.x, the code * below is used which includes specifying a property scope. */ String theMsgPropertyValue = (String)inMessage.getProperty( MESSAGE_PROPERTY_NAME, PropertyScope.INBOUND); System.out.println(" INBOUND property value: " + theMsgPropertyValue); theMsgPropertyValue = (String)inMessage.getProperty( MESSAGE_PROPERTY_NAME, PropertyScope.OUTBOUND); System.out.println(" OUTBOUND property value: " + theMsgPropertyValue);

String theMsgPayload = inMessage.getPayloadAsString(); System.out.println(" Message payload: " + theMsgPayload); }}

9o%e %$a%:• ($e cons%an% s%rin# MB-EP ERVI"EPBR- %$a% $olds %$e address o) %$e Mule service %o

@$ic$ @e send messa#es $as %$e pre)ix Fvm:==G($is pre)ix %ells Mule %$a% i% s$ould use %$e VM %ranspor%, @$ic$ is a %ranspor% used @i%$inone and %$e same ?VM 1mi%%in# %$is %ranspor% pre)ix @ill cause an error

• In %$edo&xample me%$od, %$ere are %@o variables o) %$e %ype Mule ontext 6or eac$ Mule 2 x con)i#ura%ion @e crea%e one Mule ins%ance, represen%ed by a Mulecon%ex%

• In %$edo&xample me%$od, a Mule messa#e is crea%ed @i%$ only a messa#e payload, a s%rin#,as parame%erAs @e @ill see la%er, a Mule ontext ob4ec% mus% be supplied @$en crea%in# messa#es @$enusin# Mule 3 x Bsin# Mule 2 x, no Mule con%ex% need %o be supplied @$en crea%in#messa#es

• ($e messa#e proper%y @i%$ %$e name FMule*roper%y2G is se% %o %$e value FU e% by pro#ram G

• A Mule ins%ance is s%ar%ed usin# %$e )irs% Mule con)i#ura%ion )ile

!;5

Page 185: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 185/354

Civen %$e name o) a con)i#ura%ion )ile, %$e startMule me%$od is responsible )or s%ar%in# aMule ins%ance con)i#ured accordin# %o %$e con)i#ura%ion )ile

• ($e con%en%s o) %$e Mule messa#e is lo##ed %o %$e console be)ore i% is bein# sen% %o Mule )or processin#

• A Mule lient clien% ob4ec% is crea%ed )or %$e )irs% Mule con%ex%

Mule clien%s are used %o send and receive messa#es )rom a Mule ins%ance Mul%iple clien%smay be crea%ed )or one con%ex%, allo@in# mul%iple %$reads %o in%erac% @i%$ one Mule ins%anceconcurren%lyCiven a Mule con%ex%, %$ecreateMule lient me%$od is responsible )or crea%in# a Mule clien%)or %$e supplied con%ex%

• ($e Mule messa#e is sen% %o %$e )irs% Mule ins%ance usin# %$e Mule clien% 4us% crea%ed$en sendin# a messa#e, an BR- mus% be suppled ($is BR- speci)ies %$e %ranspor% and %$e

des%ina%ion o) %$e messa#e• Eac$ messa#e sen% %o a Mule clien% is processed in a session o) i%s o@n

($is allo@s a clien% %o send more messa#es %o a Mule ins%ance @$ile previously sen%messa#es are bein# processed($is is $andled in%ernally by Mule and is no% made apparen% by %$e code o) %$is example*lease re)er %o %$e MuleSession in%er)ace in %$e Mule A*I documen%a%ion )or in)orma%ion

• ($e BR- %o @$ic$ %$e messa#e is sen%, Fvm:==myMule erviceBR-G, ma%c$es %$e value o)%$e address a%%ribu%e in %$e inbound&endpoin%L elemen% in %$e Mule con)i#ura%ion )ile

• ($e con%en%s o) %$e Mule messa#e received as resul% o) sendin# %$e )irs% Mule messa#e %o %$e)irs% Mule ins%ance is lo##ed %o %$e console

• A second Mule ins%ance is s%ar%ed con)i#ured accordin# %o %$e second con)i#ura%ion )ileA#ain, %$is is accomplis$ed usin# %$e startMule me%$od

• Ano%$er Mule clien% is crea%ed, in order %o send and receive messa#es %o=)rom %$e secondMule ins%ance

• In %$e same manner as %$e messa#e @as sen% %o %$e )irs% Mule ins%ance, %$e messa#e is sen% %o%$e second Mule ins%ance($e BR- is %$e same as @$en sendin# %$e messa#e %o %$e )irs% ins%ance ($is is because %$einbound endpoin% address in %$e %@o Mule con)i#ura%ion )iles is %$e same

• ($e Mule messa#e is lo##ed %o %$e console• ($e Mule con%ex%s represen%in# %$e )irs% and second Mule ins%ance are disposed

%oppin# a Mule con%ex% is no% enou#$, i) @e @an% %$e applica%ion %o %ermina%e properly@$en our s%ar%er class $as )inis$ed execu%in#.isposin# a Mule con%ex% )irs% s%ops i% and %$en releases %$e resources $eld by %$e Muleins%ance

!;8

Page 186: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 186/354

Create the Mule 3.x Starter Class

($e implemen%a%ion o) %$e Mule 3 x s%ar%er class is no% as s%rai#$%)or@ard as %$e Mule 2 x s%ar%erclassJ messa#e proper%ies need %o be moved %o %$e proper scope since Mule 3 x @ill move messa#e

proper%ies be$ind scopes .e%ails o) %$is be$aviour @ill be observed @$en @e run %$e di))eren%versions o) %$e example pro#ram

• "rea%e a class named MuleProgrammatic0seStarter) implemen%ed as %$is:

package com.ivan.starter;

import org.mule.DefaultMuleMessage;import org.mule.api.MuleContext;import org.mule.api.MuleException;import org.mule.api.MuleMessage;import org.mule.api.config.ConfigurationBuilder;import org.mule.api.context.MuleContextBuilder;import org.mule.api.context.MuleContextFactory;import org.mule.api.transport.PropertyScope;import org.mule.config.spring.SpringXmlConfigurationBuilder;import org.mule.context.DefaultMuleContextBuilder;import org.mule.context.DefaultMuleContextFactory;

import org.mule.module.client.MuleClient;/** * This class invokes two Mule configurations in sequence, * passing the result of the first invocation as input to the * second. * Version for Mule 3.x. *

* @author Ivan Krizsan */public class MuleProgrammaticUseStarter3{ /* Constant(s): */ private final static String MULE_CONFIG_FILE1 = "com/ivan/muleconfig/mule3-config.xml"; private final static String MULE_CONFIG_FILE2 = "com/ivan/muleconfig/mule3-config2.xml"; private final static String MULE_SERVICE_URL = "vm://myMuleServiceURL"; private final static String MESSAGE_PROPERTY_NAME = "MuleProperty2";

/* Instance variable(s): */

public static void main(String[] args) throws Exception { (new MuleProgrammaticUseStarter3()).doExample(); }

private void doExample() { MuleContext theContext1 = null; MuleContext theContext2 = null; try {

/* * Start the first Mule context with the first configuration * file. Need to do this, since the context will be used * when creating a new message. */ theContext1 = startMule(MULE_CONFIG_FILE1);

/* * Create the message to send to the two different Mule * configurations. * Set both message payload and a message property. */ MuleMessage theMuleMessage = new DefaultMuleMessage( "[original message payload]", theContext1); theMuleMessage.setProperty(MESSAGE_PROPERTY_NAME, "[Set by program]", PropertyScope.OUTBOUND);

/*

!;7

Page 187: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 187/354

* Examine the message before the message has been processed * by the first Mule configuration. */ System.out.println("***** Before first invocation:"); logMessageContents(theMuleMessage);

/* Create a client and send the message to it. */ MuleClient theMuleClient = createMuleClient(theContext1); theMuleMessage = sendMessageToMule(theMuleMessage, theMuleClient);

/** Examine the message after the message has been processed

* by the first Mule configuration. */ System.out.println("***** After first invocation:"); logMessageContents(theMuleMessage);

/* * Copy the message property to the outbound scope, or else * it will be cleared. * Clear the INBOUND scope to make presentation of message * look more clear. */ String theMsgPropertyValue = theMuleMessage.getProperty( MESSAGE_PROPERTY_NAME, PropertyScope.INBOUND); theMuleMessage.setProperty(MESSAGE_PROPERTY_NAME,

theMsgPropertyValue, PropertyScope.OUTBOUND); theMuleMessage.clearProperties(PropertyScope.INBOUND);

/** Examine the message before the message has been processed

* by the second Mule configuration. */ System.out.println("***** Before second invocation:"); logMessageContents(theMuleMessage);

/* * Start a new Mule context with the second configuration file, * create a client and send the message received from the * above invocation to it. */ theContext2 = startMule(MULE_CONFIG_FILE2); theMuleClient = createMuleClient(theContext2); theMuleMessage = sendMessageToMule(theMuleMessage, theMuleClient);

/** Examine the message after the message has been processed

* by the second Mule configuration. */ System.out.println("***** After second invocation:"); logMessageContents(theMuleMessage); } catch (final Exception theException) { theException.printStackTrace(); } finally { /* * Dispose of the Mule contexts. Disposing the context * automatically stops it and we do not have to bother * with the exception declared by the stop method. * Must dispose if we wish the application to terminate, * otherwise there will be unreleased resources. */ if (theContext1 != null) { theContext1.dispose(); } if (theContext2 != null) { theContext2.dispose(); } }

}

private MuleContext startMule(final String inMuleConfigFile) throws MuleException {

!;

Page 188: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 188/354

/* * Starts an instance of Mule configured according to the supplied * configuration file. */ String[] theConfigFiles = { inMuleConfigFile };

MuleContextFactory theContextFactory = new DefaultMuleContextFactory(); ConfigurationBuilder theConfigBuilder = new SpringXmlConfigurationBuilder(theConfigFiles);

MuleContextBuilder theContextBuilder = new DefaultMuleContextBuilder(); MuleContext theMuleContext = theContextFactory.createMuleContext( theConfigBuilder, theContextBuilder); theMuleContext.start();

return theMuleContext; }

private MuleClient createMuleClient(final MuleContext inMuleContext) throws MuleException { MuleClient theMuleClient = new MuleClient(inMuleContext); return theMuleClient; }

private MuleMessage sendMessageToMule(final MuleMessage inMessage, final MuleClient inMuleClient) throws Exception { MuleMessage theReceivedMsg; theReceivedMsg = inMuleClient.send(MULE_SERVICE_URL, inMessage); return theReceivedMsg; }

private void logMessageContents(final MuleMessage inMessage) throws Exception { /* * Note how a message scope is supplied when retrieving * message properties. * Property retrieval methods which do not require a scope * to be specified have been deprecated in Mule 3.x. */ String theMsgPropertyValue = (String)inMessage.getProperty( MESSAGE_PROPERTY_NAME, PropertyScope.INBOUND); System.out.println(" INBOUND property value: " + theMsgPropertyValue); theMsgPropertyValue = (String)inMessage.getProperty( MESSAGE_PROPERTY_NAME, PropertyScope.OUTBOUND); System.out.println(" OUTBOUND property value: " + theMsgPropertyValue);

String theMsgPayload = inMessage.getPayloadAsString(); System.out.println(" Message payload: " + theMsgPayload); }}

9o%e %$a%:• ($e cons%an% s%rin# MB-EP ERVI"EPBR- %$a% $olds %$e address o) %$e Mule service %o

@$ic$ @e send messa#es $as %$e pre)ix Fvm:==G($is pre)ix %ells Mule %$a% i% s$ould use %$e VM %ranspor%, @$ic$ is a %ranspor% used @i%$inone and %$e same ?VM 1mi%%in# %$is %ranspor% pre)ix @ill cause an error

• In %$edo&xample me%$od, %$ere are %@o variables o) %$e %ype Mule ontext 6or eac$ Mule 3 x con)i#ura%ion @e crea%e one Mule ins%ance, represen%ed by a Mulecon%ex%

• In %$edo&xample me%$od, a Mule ins%ance is s%ar%ed usin# %$e )irs% Mule con)i#ura%ion )ileCiven %$e name o) a con)i#ura%ion )ile, %$e startMule me%$od is responsible )or s%ar%in# aMule ins%ance con)i#ured accordin# %o %$e con)i#ura%ion )ile

• 9ex%, a Mule messa#e is crea%ed @i%$ a messa#e payload, a s%rin#, and a Mule con%ex% as

!;;

Page 189: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 189/354

parame%ersIn Mule 3 x, a Mule ontext ob4ec% mus% al@ays be supplied @$en crea%in# messa#es

• ($e messa#e proper%y @i%$ %$e name FMule*roper%y2G is se% %o %$e value FU e% by pro#ram G

• ($e con%en%s o) %$e Mule messa#e is lo##ed %o %$e console be)ore i% is bein# sen% %o Mule )or

processin#• A Mule lient clien% ob4ec% is crea%ed )or %$e )irs% Mule con%ex%

Mule clien%s are used %o send and receive messa#es )rom a Mule ins%ance Mul%iple clien%smay be crea%ed )or one con%ex%, allo@in# mul%iple %$reads %o in%erac% @i%$ one Mule ins%anceconcurren%lyCiven a Mule con%ex%, %$ecreateMule lient me%$od is responsible )or crea%in# a Mule clien%)or %$e supplied con%ex%

• ($e Mule messa#e is sen% %o %$e )irs% Mule ins%ance usin# %$e Mule clien% 4us% crea%ed$en sendin# a messa#e, an BR- mus% be suppled ($is BR- speci)ies %$e %ranspor% and %$e

des%ina%ion o) %$e messa#e• ($e BR- %o @$ic$ %$e messa#e is sen%, Fvm:==myMule erviceBR-G, ma%c$es %$e value o)

%$e address a%%ribu%e in %$e inbound&endpoin%L elemen% in %$e Mule con)i#ura%ion )ile• As @i%$ Mule 2 x, eac$ messa#e sen% %o a Mule clien% is processed in a session o) i%s o@n

($is allo@s a clien% %o send more messa#es %o a Mule ins%ance @$ile previously sen%messa#es are bein# processed($is is $andled in%ernally by Mule and is no% made apparen% by %$e code o) %$is example*lease re)er %o %$e MuleSession in%er)ace in %$e Mule A*I documen%a%ion )or in)orma%ion

• Immedia%ely a)%er $avin# received %$e response messa#e a)%er $avin# invo/ed %$e )irs% Muleins%ance, %$e messa#e con%en%s is lo##ed %o %$e console

As be)ore, Mule 3 x moves messa#e proper%ies so more lo##in# @as implemen%ed, in order%o ma/e %$is visible• ($e FMule*roper%y2G messa#e proper%y o) %$e response messa#e is copied )rom %$e

I9>1B9. messa#e scope %o %$e 1B(>1B9. messa#e scope• ($e I9>1B9. messa#e scope in %$e response messa#e is cleared

($e I9>1B9. messa#e scope @ill be cleared @$en %$e messa#e is sen% %o a Mule 3 xins%ance, so @e clear i% no@ %o ma/e %$e pro#ram ou%pu% more clear

• ($e con%en%s o) %$e Mule messa#e received as resul% o) sendin# %$e )irs% Mule messa#e %o %$e)irs% Mule ins%ance is lo##ed %o %$e console

• A second Mule ins%ance is s%ar%ed con)i#ured accordin# %o %$e second con)i#ura%ion )ileA#ain, %$is is accomplis$ed usin# %$e startMule me%$od

• Ano%$er Mule clien% is crea%ed, in order %o send and receive messa#es %o=)rom %$e secondMule ins%ance

• In %$e same manner as %$e messa#e @as sen% %o %$e )irs% Mule ins%ance, %$e messa#e is sen% %o%$e second Mule ins%ance($e BR- is %$e same as @$en sendin# %$e messa#e %o %$e )irs% ins%ance ($is is because %$einbound endpoin% address in %$e %@o Mule con)i#ura%ion )iles is %$e same

• ($e Mule messa#e is lo##ed %o %$e console• ($e Mule con%ex%s represen%in# %$e )irs% and second Mule ins%ance are disposed

!;+

Page 190: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 190/354

%oppin# a Mule con%ex% is no% enou#$, i) @e @an% %$e applica%ion %o %ermina%e properly@$en our s%ar%er class $as )inis$ed execu%in#.isposin# a Mule con%ex% )irs% s%ops i% and %$en releases %$e resources $eld by %$e Muleins%ance

.evelopin# %$e %@o s%ar%er classes $as, besides s$o@in# $o@ %o use Mule pro#ramma%ically, #ivenus a )eelin# %$a% Mule 2 x and Mule 3 x $andles messa#e proper%ies di))eren%lyBsin# Mule 2 x and 3 x pro#ramma%ically is 0ui%e similar %$e only di))erence %$is c$ap%er'sexample pro#ram ma/es obvious is %$a% @i%$ Mule 3 x, a Mule con%ex% mus% be supplied @$encrea%in# a Mule messa#e

!+

Page 191: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 191/354

1; * +un the Example Program 9o@ i% is %ime %o run %$e Mule 2 x and Mule 3 x versions o) %$e example pro#ram and compare %$eou%pu% ($e )ocus is on Mule messa#e proper%ies and messa#e scopes

9o%e %$a% our example pro#ram only lo#s one par%icular messa#e proper%y, no% all %$e messa#e proper%ies in a cer%ain scope

Remember %$a% @e are %o run %$e s%ar%er classes o) %$e example pro#ram, 91( %$e Mulecon)i#ura%ion )iles

I) %$ere are any problems runnin# %$e s%ar%er classes, clean and rebuild %$e pro4ec% in Eclipse as a)irs% a%%emp% a% a remedy

Run the Mule 2.x ersion of the Exa&ple 'rogra&

>e)ore runnin# %$e Mule 2 x version o) %$e s%ar%er class, please assure %$a% %$e pro4ec% is indeedcon)i#ured @i%$ %$e Mule 2 x libraries on %$e classpa%$, as described inappendix >

In %$e *ac/a#e Explorer in Eclipse, ri#$%&clic/ %$e FMule*ro#ramma%icBse %ar%er2G classand selec% Run As &L ?ava Applica%ion• ($e applica%ion s$ould produce %$e )ollo@in# console ou%pu% and %$en %ermina%e:

***** Before first invocation: INBOUND property value: null OUTBOUND property value: [Set by program] Message payload: [original message payload]***** After first invocation: INBOUND property value: null OUTBOUND property value: [Set by program][set by config1] Message payload: [original message payload][string appended by first config]***** After second invocation: INBOUND property value: null OUTBOUND property value: [Set by program][set by config1][set by config2]

Message payload: [original message payload][string appended by first config][stringappended by second config]

9o%e %$a%:• ($e proper%y value in %$e 1B(>1B9. messa#e proper%ies scope is appended by eac$ Mule

con)i#ura%ion• ($e proper%y value never appears in %$e I9>1B9. messa#e proper%ies scope

$en %ryin# %o re%rieve %$e value o) %$e FMule*roper%y2G messa#e proper%y )rom %$eI9>1B9. scope, %$e resul% is al@ays nullRecall %$e Mule 2 x con)i#ura%ion )iles @e developedearlier $en appendin# %$e s%rin# %o%$e value o) our messa#e proper%y, no messa#e scope @as speci)ied, nei%$er in %$e expressionHno% suppor%ed in Mule 2 x nor in %$e messa#e&proper%ies&%rans)ormerL ($us %$e scopeused is al@ays %$e 1B(>1B9. messa#e proper%y scope

• ($e messa#e payload is appended by eac$ Mule con)i#ura%ion

!+!

Page 192: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 192/354

($e )ollo@in# pic%ure visualizes %$e con%en%s o) %$e Mule messa#e passin# %$rou#$ %$e %@o Mule2 x ins%ances:

"on%en% o) a Mule messa#e passin# %$rou#$ %$e %@o Mule 2 x ins%ances o) %$e example pro#ram

e can conclude %$a% messa#e proper%ies are no% moved by Mule 2 x and %$a% our messa#e proper%yis placed in %$e 1B(>1B9. scope and remains %$ere )or %$e dura%ion o) %$e en%ire execu%ion o) %$eexample pro#ram

!+2

Page 193: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 193/354

Running the Mule 3.x ersion of the Exa&ple 'rogra&• "on)i#ure %$e FMule*ro#ramma%icBseG Eclipse pro4ec% so %$a% i% $as %$e Mule 3 x libraries

on %$e classpa%$, accordin# %o %$e procedure described inappendix >• In %$e *ac/a#e Explorer in Eclipse, ri#$%&clic/ %$e FMule*ro#ramma%icBse %ar%er3G class

and selec% Run As &L ?ava Applica%ion• ($e applica%ion s$ould produce %$e )ollo@in# console ou%pu% and %$en %ermina%e Hlo#

produced by Mule $as been removed )or sa/e o) clari%y :

***** Before first invocation: INBOUND property value: null OUTBOUND property value: [Set by program] Message payload: [original message payload]***** After first invocation: INBOUND property value: [Set by program][set by config1] OUTBOUND property value: null Message payload: [original message payload][string appended by first config]***** Before second invocation: INBOUND property value: null OUTBOUND property value: [Set by program][set by config1] Message payload: [original message payload][string appended by first config]***** After second invocation: INBOUND property value: [Set by program][set by config1][set by config2] OUTBOUND property value: null Message payload: [original message payload][string appended by first config][stringappended by second config]

9o%e %$a%:• >e)ore sendin# %$e messa#e %o %$e )irs% Mule ins%ance, our messa#e proper%y @as )ound in

%$e 1B(>1B9. messa#e scope• In %$e response messa#e )rom %$e )irs% Mule ins%ance, our messa#e proper%y @as )ound in %$e

I9>1B9. messa#e scope($us, Mule 3 x $as moved %$e messa#e proper%y )rom %$e 1B(>1B9. scope %o %$eI9>1B9. scope

o @$en is %$e messa#e proper%y moved I) @e loo/ a% %$e Mule 3 x con)i#ura%ion )iledeveloped earlier , @e can see %$a% %$e messa#e proper%y is re%rieved )rom %$e I9>1B9.scope, processed by appendin# a s%rin# and %$en placed in %$e 1B(>1B9. scope<o@ever, @$en %$e example pro#ram prin%s %$e messa#e con%en%s a)%er $avin# invo/ed %$e)irs% Mule ins%ance, %$e messa#e proper%y is )ound in %$e I9>1B9. scope($us Mule 3 x moves %$e messa#e proper%y %@iceJ once be)ore %$e messa#e is processed by%$e con)i#ura%ion )ile and once a)%er %$e messa#e $as been processed by %$e con)i#ura%ion)ile

• A)%er %$e )irs% invoca%ion, %$e messa#e proper%y is )ound in %$e I9>1B9. messa#e scope, bu% be)ore sendin# %$e messa#e %o %$e second Mule 3 x ins%ance, %$e messa#e proper%y isonce a#ain in %$e 1B(>1B9. scopeRecall %$a% %$e Mule 3 x s%ar%er class developedearlier con%ained code %$a% moved %$emessa#e proper%y )rom %$e I9>1B9. %o %$e 1B(>1B9. messa#e scope be%@een %$e )irs%and second invoca%ion

• ($e messa#e payload is appended by eac$ Mule con)i#ura%ion in %$e same @ay as seen in%$e Mule 2 x version o) %$e example pro#ram

!+3

Page 194: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 194/354

($e )ollo@in# pic%ure visualizes %$e con%en%s o) %$e Mule messa#e passin# %$rou#$ %$e %@o Mule3 x ins%ances:

"on%en% o) a Mule messa#e passin# %$rou#$ %$e %@o Mule 3 x ins%ances o) %$e example pro#ram

e can conclude %$a% Mule 3 x be$aves in %$e )ollo@in# @ay concernin# movin# messa#e proper%ies be%@een %$e di))eren% scopes:

• $en sendin# a messa#e %o a Mule con)i#ura%ion, %$e messa#e proper%ies in %$e1B(>1B9. scope are moved %o %$e I9>1B9. scope

• Messa#e proper%ies %$a% are %o be made available %o %$e clien% sendin# %$e messa#e are placed in %$e 1B(>1B9. scope by %$e Mule con)i#ura%ion

• <avin# )inis$ed execu%in# %$e Mule con)i#ura%ion, %$e messa#e proper%ies o) %$e response

messa#e are moved )rom %$e 1B(>1B9. scope %o %$e I9>1B9. scope• >e)ore movin# messa#e proper%ies )rom one scope %o ano%$er, %$e %ar#e% scope is cleared and

all in)orma%ion in i% los%

1; , ExercisesDou are su##es%ed %o experimen% @i%$ Mule messa#e proper%ies and messa#e proper%y scopes ee@$a% is available a% %$e di))eren% s%a#es durin# %$e li)e%ime o) a Mule messa#e6or %$e advanced, %racin# in%o %$e Mule code and examinin# $o@ Mule messa#e proper%ies are

processed can be an in%eres%in# exercise

!+5

Page 195: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 195/354

11. %esting Mule ConfigurationsIn %$is c$ap%er @e @ill loo/ a% $o@ %o %es% a scenario @$ic$ involves a sin#le Mule con)i#ura%ion )ileand one service componen% implemen%ed in ?ava ($e Mule li%era%ure calls %$is /ind o) %es%s)unc%ional %es%sI% is possible %o %es% individual service componen%s, %rans)ormers, )il%ers e%c usin# uni% %es%s uc$%es%in# is considered %o be re#ular %es%&driven developmen% and @ill no% be discussed in %$is boo/

($e con)i#ura%ion @e are %o %es% in bo%$ %$e Mule 2 x and Mule 3 x versions o) %$is c$ap%er'sexample pro#ram consis%s o) a sin#le <((* endpoin% %$a% receives re0ues%s I) a re0ues% con%ains%$e parame%er @i%$ %$e name Fda%aG and %$e value F!23G in %$e BR-, i% is considered %o be a validre0ues% and an A"K response messa#e @ill be #iven 1%$er@ise %$e re0ues% is considered invalidand @ill yield a 9A"K response messa#e

11 1 Create the Pro!ect"rea%e %$e pro4ec% as described in %$e appendix"rea%e a Mule *ro4ec%, namin# i%FMuleBni%(es%in#G ($e Mule 3 $o% deploymen% can be s@i%c$ed o)) )rom %$e s%ar%, as %$is )ea%ure

@ill no% be used

11 " Create the TestsIn %$e spiri% o) %es%&driven developmen%, @e crea%e %$e %es%s )irs% ($e %es%s implemen%a%ions consis%o) one class implemen%in# common %es% code )or bo%$ %$e Mule 2 x and 3 x versions o) %$e example,as @ell as one Mule 2 x speci)ic and one Mule 3 x speci)ic %es% case

Create the Co&&on %est Class

($e common %es% class consis%s o) %$e %@o %es%s %$a% @e apply %o %$e valida%ion serviceJ one %es% %$a

issues a re0ues% con%ainin# %$e sou#$%&a)%er parame%er @i%$ %$e speci)ic value and ano%$er %es% %$a%issue a re0ues% %$a% does con%ain %$e sou#$%&a)%er parame%er, bu% @i%$ ano%$er value($e reason in %$is example )or ex%rac%in# %$e %es%s %o %$is common class is 4us% %o avoid bla%an% codeduplica%ion

• In %$e pac/a#e com.ivan.tests , implemen% %$e ommon2alidator'est ase class li/e s$o@n in%$e )ollo@in# lis%in#:

package com.ivan.tests;

import java.util.HashMap;import java.util.Map;

import junit.framework.Assert;

import org.mule.api.MuleException;import org.mule.api.MuleMessage;import org.mule.module.client.MuleClient;import org.mule.tck.FunctionalTestCase;

/** * Implements common code used to test the validation service * for both Mule 2.x and Mule 3.x. *

* @author Ivan Krizsan */public abstract class CommonValidatorTestCase extends FunctionalTestCase{ /** * Tests sending a good request to the validator service and ensure * that we get the proper response. *

!+8

Page 196: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 196/354

* @throws Exception If error occurs during test. * Indicates test failure. */ public void testGoodRequest() throws Exception { MuleMessage theResponseMessage = sendHttpRequest( "http://localhost:8080/validator?data=123");

/* Check the response message and its payload. */

Assert.assertNotNull(theResponseMessage); Assert.assertNotNull(theResponseMessage.getPayload()); Assert.assertNull(theResponseMessage.getExceptionPayload()); Assert.assertTrue(theResponseMessage.getPayloadAsString().contains( "good message")); }

/** * Tests sending a bad request to the validator service and ensure * that we get the proper response. *

* @throws Exception If error occurs during test. * Indicates test failure. */ public void testBadRequest() throws Exception { MuleMessage theResponseMessage = sendHttpRequest( "http://localhost:8080/validator?data=1");

/* Check the response message and its payload. */ Assert.assertNotNull(theResponseMessage); Assert.assertNotNull(theResponseMessage.getPayload()); Assert.assertNull(theResponseMessage.getExceptionPayload()); Assert.assertTrue(theResponseMessage.getPayloadAsString().contains( "NACK")); }

private MuleMessage sendHttpRequest(final String inUrl) throws MuleException { /* Configure Mule message properties. */ Map<String, String> theMessageProperties = new HashMap<String, String>(); theMessageProperties.put("http.method", "GET");

/* * Create the Mule client and use it to send the message, * which in this example is null. * Note that, using the Mule client, we can send messages * to endpoints using different transports by altering the * URL of the endpoint. * Example of transports are http and vm. */ MuleClient theClient = new MuleClient(muleContext); MuleMessage theResponseMessage = theClient.send(inUrl, null, theMessageProperties); return theResponseMessage; }}

9o%e %$a%:• ($e ommon2alidator'est ase class ex%ends %$e Mule class ,unctional'est ase

($e class ,unctional'est ase is a ?Bni% %es% case and serves as a base class )or %es%s %$a%ini%ializes Mule usin# a Mule con)i#ura%ion )ile($e class is )ound in bo%$ Mule 2 x and Mule 3 x, so %$e above class is used by bo%$versions o) %$e example pro#ram @e are developin#

• ($e test5ood#e uest me%$od sends a re0ues% %$a% is expec%ed %o produce an A"K resul% %o a<((* endpoin% and veri)ies %$e resul%

9o%e %$e presence o) %$e s%rin# Fda%aW!23G in %$e BR-• ($e test"ad#e uest me%$od sends a re0ues% %$a% is expec%ed %o produce a 9A"K resul% %o

!+7

Page 197: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 197/354

%$e same <((* endpoin% and veri)ies %$e resul% 9o%e %$a% %$e BR- no@ con%ains %$e s%rin# Fda%aW!G

• ($e sendHttp#e uest me%$od crea%es a Mule lient ob4ec% and uses %$is ob4ec% %o send are0ues%($e re0ues% can be sen% %o endpoin%s usin# di))eren% /inds o) %ranspor%s, )or ins%ance %$eMule VM %ranspor% or <((* %ranspor%, by al%erin# %$e pre)ix o) %$e BR-

• $en sendin# a re0ues% usin# a Mule lient ob4ec%, @e could $ave enclosed a Mule messa#eIn order %o /eep %$e example simple and )ocus on %$e %es%in# par%, a simple Mulecon)i#ura%ion %$a% does no% receive Mule messa#es is used e can %$us pass null ins%ead o)a Mule messa#e @$en sendin# %$e re0ues%s

• ($e sendHttp#e uest me%$od se%s up a map con%ainin# a sin#le /ey&value pair($ese are Mule messa#e proper%ies %$a% @e can, despi%e sendin# a null Mule messa#e,enclose @i%$ %$e re0ues%In %$is example, usin# %$e <((* %ranspor%, %$ese proper%ies @ill be enclosed as <((*re0ues% $eaders

i%$ %$e above class in place, %$e Mule 2 x and Mule 3 x classes become %rivial 4us% a ma%%er o)speci)yin# %$e appropria%e Mule con)i#ura%ion )ile %o use @$en ini%ializin# Mule

Create the Mule 2.x %est

($e Mule 2 x %es% class in$eri%s )rom %$e common %es% class devised above and %$e only %$in# @eneed %o do is %o speci)y @$ic$ Mule con)i#ura%ion )ile %$e %es% is %o use

• In %$e pac/a#e com.ivan.tests , implemen% %$e2alidator%x'est ase li/e %$is:

package com.ivan.tests;

/** * Tests the Mule 2.x validator configuration. *

* @author Ivan Krizsan */public class Validator2xTestCase extends CommonValidatorTestCase{ /** * Retrieves the name of the configuration file to be used * with the test case. *

* @return Mule configuration file name. */ @Override protected String getConfigResources() { return "com/ivan/muleconfig/mule-config221.xml"; }}

9o%e %$a%:• ($e 2alidator%x'est ase class implemen%s %$e get onfig#esources me%$od

($is is %$e me%$od @$ic$ resul% %ell %$e %es% case @$ic$ Mule con)i#ura%ion )ile %o use @$ense%%in# up Mule )or %$e %es% ($e me%$od is abs%rac% in %$e ,unction'est ase paren% class,@$ic$ is %$e reason %$e ommon2alidator'est ase class is abs%rac%

!+

Page 198: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 198/354

Create the Mule 3.x %est

($e only di))erence be%@een %$e Mule 3 x %es% class and %$e Mule 2 x %es% class @e 4us% developed is%$e use o) a di))eren% Mule con)i#ura%ion )ile

• In %$e pac/a#e com.ivan.tests , implemen% %$e2alidator)x'est ase as )ollo@s:

package com.ivan.tests;

/** * Tests the Mule 3.x validator configuration. *

* @author Ivan Krizsan */public class Validator3xTestCase extends CommonValidatorTestCase{

/** * Retrieves the name of the configuration file to be used * with the test case. *

* @return Mule configuration file name. */

@Override protected String getConfigResources() { return "com/ivan/muleconfig/mule-config321.xml"; }}

9o%e %$a%:• ($e 2alidator)x'est ase class implemen%s %$e get onfig#esources me%$od

($is is %$e me%$od @$ic$ resul% %ell %$e %es% case @$ic$ Mule con)i#ura%ion )ile %o use @$ense%%in# up Mule )or %$e %es% ($e me%$od is abs%rac% in %$e ,unction'est ase paren% class,@$ic$ is %$e reason %$e ommon2alidator'est ase class is abs%rac%

!+;

Page 199: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 199/354

11 & Create the (ule Configuration )ilesIn an a%%emp% %o crea%e iden%ical con)i#ura%ion )iles, excep% )or %$e namespace declara%ions, I crea%eda Mule 3 x con)i#ura%ion )ile %$a% uses a model and a service Re#re%)ully, Mule 2 x lac/s %$e%rans)ormer %$a% %rans)orms <((* re0ues% parame%ers in %$e BR- %o a map in %$e messa#e payload,so %$e Mule 2 x con)i#ura%ion does di))er sli#$%ly )rom %$e Mule 3 x di%o

Create the Mule 2.x Configuration File

($e Mule 2 x con)i#ura%ion )ile is 0ui%e s%rai#$%)or@ard and s$ould by no@ be )amiliar %o readers o)%$is boo/

• In %$e pac/a#e com.ivan.muleconfi #, crea%e a Mule con)i#ura%ion )ile named Fmule&con)i#22! xmlG @i%$ %$e )ollo@in# con%en%s:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:http="http://www.mulesource.org/schema/mule/http/2.2" xmlns:https="http://www.mulesource.org/schema/mule/https/2.2" xsi:schemaLocation="

http://www.mulesource.org/schema/mule/http/2.2 http://www.mulesource.org/schema/mule/http/2.2/mule-http.xsd

http://www.mulesource.org/schema/mule/https/2.2 http://www.mulesource.org/schema/mule/https/2.2/mule-https.xsd

http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd">

<model name="validationModel"> <service name="validationService"> <inbound> <!-- Endpoint receiving requests that are to be validated. This particular endpoint receives HTTP requests and extracts HTTP request parameters from the URL to a map. Note that this endpoint is synchronous, in order to be able to deliver a ACK or NACK as the result of the request. --> <http:inbound-endpoint address="http://localhost:8080/validator" synchronous="true"> </http:inbound-endpoint> </inbound> <component> <singleton-object class="com.ivan.services.ValidationService2x"/> </component> </service> </model></mule>

9o%e %$a%:• ($ere is one sin#le model %$a% con%ains a sin#le service• ($e service $as an inbound endpoin%

($e endpoin% uses %$e <((* %ranspor%, is sync$ronous and $as %$e address$%%p:==local$os%:; ; =valida%or

• ($e service $as a componen% %$a% is served by a sin#le ins%ance o) %$e2alidationService%xclass %$a% @e @ill implemen% in %$e nex% sec%ion o) %$is c$ap%er

• ($ere are no %rans)orma%ions applied %o incomin# messa#esAs @e @ill see @$en implemen%in# %$e2alidationService%x class, %$e <((* parame%ers can,@i%$ou% any special e))or% on our side, be )ound in %$e messa#e proper%ies o) incomin# Mulemessa#es

!++

Page 200: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 200/354

Create the Mule 3.x Configuration File

($e Mule 3 x con)i#ura%ion )ile loo/s a lo% li/e %$e Mule 2 x con)i#ura%ion )ile because o) reasonsmen%ioned in %$e in%roduc%ion %o %$is sec%ion

• In %$e pac/a#e com.ivan.muleconfi #, crea%e a Mule con)i#ura%ion )ile named Fmule&con)i#32! xmlG @i%$ %$e )ollo@in# con%en%s:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:http="http://www.mulesoft.org/schema/mule/http" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.1/mule.xsd

http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/3.2/mule-http.xsd">

<model name="validationModel"> <service name="validationService"> <inbound> <!-- Endpoint receiving requests that are to be validated.

This particular endpoint receives HTTP requests and extracts HTTP request parameters from the URL to a map. Note that this endpoint uses the request-response message exchange pattern, in order to be able to deliver a ACK or NACK as the result of the request. --> <http:inbound-endpoint address="http://localhost:8080/validator" exchange-pattern="request-response"> <!-- The body-to-parameter-map transformer transforms a HTTP request message to a map containing all the request parameters and their values. --> <http:body-to-parameter-map-transformer/> </http:inbound-endpoint> </inbound> <component> <singleton-object class="com.ivan.services.ValidationService3x"/> </component> </service> </model></mule>

9o%e %$a%:• ($ere is one sin#le model %$a% con%ains a sin#le service• ($e service $as an inbound endpoin%

($e endpoin% uses %$e <((* %ranspor%, is sync$ronous and $as %$e address$%%p:==local$os%:; ; =valida%or

($e service $as a componen% %$a% is served by a sin#le ins%ance o) %$e2alidationService)xclass %$a% @e @ill implemen% in %$e nex% sec%ion o) %$is c$ap%er

• ($ere is a body&%o&parame%er&map %rans)ormer in %$e inbound endpoin%($is %rans)ormer inser%s %$e <((* re0ues% parame%ers )rom %$e BR- in%o a map, @$ic$ %$en

becomes %$e payload o) %$e Mule messa#e %$a% is passed on %o %$e service's componen%

2

Page 201: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 201/354

11 ' Create the Ser$ice %mplementation Classes($e example ma/es use o) %@o service implemen%a%ion classes, one )or Mule 2 x and one )or Mule3 x, @$ic$ implemen% %$e valida%ion service .ue %o %$e di))erences in %$e con)i#ura%ion )iles seenabove, %$e service implemen%a%ions di))er sli#$%ly in $o@ parame%er in)orma%ion is re%rieved

Create the Mule 2.x Service )&ple&entation Class

($e Mule 2 x service implemen%a%ion class c$ec/s %$e messa#e proper%ies )or %$e presence o) %$e<((* parame%er and re%urns an A"K or 9A"K messa#e dependin# on %$e presence o) %$e expec%ed

parame%er @i%$ %$e expec%ed value• In %$e pac/a#e com.ivan.services , implemen% %$e class2alidationService%x as %$is:

package com.ivan.services;

import java.util.Date;import java.util.Map;import org.mule.api.MuleEventContext;import org.mule.api.MuleMessage;import org.mule.api.lifecycle.Callable;

/** * Service that validates received messages and produces a ACK or NACK * message depending on the contents of the messages. *

* @author Ivan Krizsan */public class ValidationService2x implements Callable{ private final static String ACK_MESSAGE = "ACK good message "; private final static String NACK_MESSAGE = "NACK bad message "; private final static String DATA_PARAM = "data"; private final static String EXPECTED_DATA_VALUE = "123";

@Override public Object onCall(final MuleEventContext inEventContext) throws Exception { Date theCurrentDate = new Date(); String theResponseString = null; MuleMessage theRequestMessage = inEventContext.getMessage();

/** Check if the "data" parameter is present in the message properties

* and has the correct value. */ String theDataValue = (String)theRequestMessage.getProperty(DATA_PARAM); if (EXPECTED_DATA_VALUE.equals(theDataValue)) { theResponseString = ACK_MESSAGE; } else { theResponseString = NACK_MESSAGE; }

return theResponseString + theCurrentDate; }}

9o%e %$a%:• 9o special processin# o) %$e parame%ers in %$e BR- is done

Mule messa#e proper%ies are au%oma%ically crea%ed )or %$e BR- parame%ers• ($e value o) %$e Fda%aG parame%er is re%rieved )rom %$e messa#e proper%ies o) %$e incomin#

re0ues% messa#e

2 !

Page 202: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 202/354

Create the Mule 3.x Service )&ple&entation Class

($e Mule 3 x version o) %$e service implemen%a%ion class expec%s %$e payload o) messa#es arrivin#%o %$e service %o be a map %$a% con%ains %$e <((* parame%ers )rom %$e BR- o) %$e ori#inal re0ues%

• In %$e pac/a#e com.ivan.services , implemen% %$e class2alidationService)x li/e %$is:

package com.ivan.services;

import java.util.Date;import java.util.Map;

import org.mule.api.MuleEventContext;import org.mule.api.MuleMessage;import org.mule.api.lifecycle.Callable;

/** * Service that validates received messages and produces a ACK or NACK * message depending on the contents of the messages. *

* @author Ivan Krizsan */public class ValidationService3x implements Callable

{ private final static String ACK_MESSAGE = "ACK good message "; private final static String NACK_MESSAGE = "NACK bad message "; private final static String DATA_PARAM = "data"; private final static String EXPECTED_DATA_VALUE = "123";

@Override public Object onCall(final MuleEventContext inEventContext) throws Exception { Date theCurrentDate = new Date(); String theResponseString = null; MuleMessage theRequestMessage = inEventContext.getMessage();

/** Check if the "data" parameter is present in the payload and

* has the correct value. */ Map<String, String> thePayloadMap =

(Map<String, String>)theRequestMessage.getPayload(); String theDataValue = thePayloadMap.get(DATA_PARAM); if (EXPECTED_DATA_VALUE.equals(theDataValue)) { theResponseString = ACK_MESSAGE; } else { theResponseString = NACK_MESSAGE; }

return theResponseString + theCurrentDate; }}

9o%e %$a%:• ($e payload o) re0ues% messa#es reac$in# %$e service is a map in @$ic$ bo%$ /eys and

values are s%rin#sAs @e @ill see la%er @$en developin# %$e Mule 3 x con)i#ura%ion )ile, %$is is due %o a%rans)orma%ion $avin# been applied %o %$e incomin# re0ues%

• ($e value o) %$e Fda%aG parame%er is re%rieved )rom %$e messa#e payload map

2 2

Page 203: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 203/354

11 * +un the Example Programi%$ %$e %es%s, %$e service implemen%a%ions and %$e Mule con)i#ura%ion )iles in place, @e are no@

ready %o run %$is c$ap%er's example pro#ram, @$ic$ consis%s o) %$e %@o %es%s

Run the Mule 2.x Exa&ple 'rogra&I) %$e pro4ec% is no% con)i#ured @i%$ %$e Mule 2 x dis%ribu%ion on %$e classpa%$, please re)er %o%$issec%ion in appendix > on $o@ %o con)i#ure @$ic$ Mule dis%ribu%ion %o use

• In %$e *ac/a#e or *ro4ec% Explorer, ri#$%&clic/ %$e2alidator%x'est ase class and selec% RunAs &L ?Bni% (es%

• A)%er some %ime, @e s$ould see %$e #reen bar in %$e ?Bni% vie@ in Eclipse($is indica%es %$a% all %$e %es%s in %$e %es% class passed

($e %es%s o) %$e2alidator%x'est ase $avin# passed

• I) @e loo/ in %$e console, %$ere is 0ui%e some ou%pu%, @$ic$ @ill no% be reproduced $ere 9o%ice %$a% %$e Mule Fsplas$ screenG %$a% is prin%ed @$en Mule $as success)ully s%ar%ed is prin%ed %@o %imes ($is means %$a% Mule is s%ar%ed and s$u% do@n once )or eac$ %es%&me%$odin %$e %es% class

2 3

Page 204: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 204/354

Run the Mule 3.x Exa&ple 'rogra&

($e Mule 3 x version o) %$e example pro#ram is similar, i) no% iden%ical, %o %$e Mule 2 x version• "on)i#ure %$e pro4ec% %o use Mule 3 x, as described in%$is sec%ion in appendix >• In %$e *ac/a#e or *ro4ec% Explorer, ri#$%&clic/ %$e2alidator)x'est ase class and selec% Run

As &L ?Bni% (es%• A)%er some %ime, @e s$ould see %$e #reen bar in %$e ?Bni% vie@ in Eclipse

($is indica%es %$a% all %$e %es%s in %$e %es% class passed

($e %es%s o) %$e2alidator)x'est ase $avin# passed

• In %$e console @e can see %$a%, as in %$e Mule 2 x version o) %$e example pro#ram, Mule iss%ar%ed and s%opped )or eac$ %es%&me%$od in %$e %es% class

11 , -//itional Exercisesince @e $ave a %es% in place %$a% is able %o de%ermine i) a service be$aves as expec%ed, one

su##es%ed addi%ional exercise is %o develop a version o) %$e Mule 3 x con)i#ura%ion )ile %$a% uses a)lo@

2 5

Page 205: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 205/354

12. Create Mule 'ro(ects ith MavenApac$e Maven is a @idely used so)%@are pro4ec% mana#emen% %ool %$a% aids in s%ruc%urin# so)%@are

pro4ec%s and mana#in# dependencies, amon# o%$er %$in#s Maven arc$e%ypes are pro4ec% %empla%es%$a% can be used %o crea%e di))eren% /inds o) so)%@are pro4ec%s %$a% use Maven

$en developin# applica%ions %$a% use Mule, you usually %ar#e% one par%icular version o) MuleBnder suc$ circums%ances, Maven can aid in crea%in# a pro4ec% @i%$ %$e relevan% dependencies

$en developin# @i%$ Mule, %$e )ollo@in# Maven arc$e%ypes are available:• *ro4ec%

*ro4ec% %empla%e )or crea%in# s%andalone Mule applica%ions• Module

*ro4ec% %empla%e )or crea%in# a ne@, or upda%e an exis%in#, Mule module• (ranspor%

*ro4ec% %empla%e )or crea%in# a ne@ Mule %ranspor%

• Example*ro4ec% %empla%e )or crea%in# a Mule example pro4ec%

• "a%alo#*ro4ec% %empla%e )or crea%in# ne@ con)i#ura%ion pa%%erns and ca%alo#s o) pa%%erns

In %$is c$ap%er, @e @ill use Maven %o crea%e %$e basis )or a s%andalone Mule pro4ec% usin# %$e *ro4ec%%empla%e and learn $o@ %o build and deploy %$e pro4ec% )rom @i%$in Eclipse usin# Maven($e c$ap%er's example @ill only s$o@ crea%ion o) a Mule 3 x pro4ec% ($e procedure )or crea%in# aMule 2 x pro4ec% is iden%ical, apar% )rom %$e version number o) %$e Mule server %$e applica%ion is%ar#e%ed a%

I) you are ne@ %o Maven, %$is c$ap%er may serve as an in%roduc%ion, describin# eac$ procedure inde%ail Dou are $o@ever advised %o consul% addi%ional Maven documen%a%ionI) you are an experienced Maven user, mainly %$e %@o )irs% sec%ions may be o) in%eres% and %$esubse0uen% sec%ions can be s/ipped

6or more in)orma%ion abou% Maven, please re)er %o %$eApac$e Maven pro4ec% @ebpa#e and %$e)ree boo/s )rom ona%ype

2 8

Page 206: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 206/354

1" 1 Prerequisitese @ill @or/ @i%$ Maven )rom a %erminal, or command&line, @indo@ so %$e mvn command mus%

be ins%alled 6or %$e example in %$is c$ap%er I $ave used Maven 3 3, bu% o%$er versions may @or/e0ually @ell

(o de%ermine i) %$e Maven command is available, )ollo@ %$ese s%eps:• 1pen a (erminal="ommand&line @indo@• En%er Fmvn &versionG H@i%$ou% 0uo%es and press re%urn• I) %$e command is no% )ound, you need %o ins%all Maven as described on %$eMaven

do@nload @ebpa#e• 1n my sys%em, %$e ou%pu% )rom %$e command loo/s li/e %$is:

Apache Maven 3.0.3 (r1075438; 2011-03-01 01:31:09+0800)Maven home: /usr/share/maven...

1) mos% in%eres% is %$e )irs% ro@, in @$ic$ %$e Maven version is s$o@n ($e o%$er ro@s @illvary dependin# on your opera%in# sys%em, %$e ?ava run%ime ins%alled e%c

In addi%ion %o %$e Maven command, you also need %o be connec%ed %o %$e in%erne%, in order )orMaven %o be able %o do@nload libraries and o%$er ar%i)ac%s needed @$en crea%in# %$e pro4ec%,

buildin# i% e%c

2 7

Page 207: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 207/354

1" " Create the Pro!ecti%$ Maven ins%alled, @e are no@ ready %o crea%e a Maven pro4ec% )or a Mule s%andalone

applica%ion• 1pen a (erminal="ommand&line @indo@• In %$e %erminal @indo@, #o %o %$e loca%ion in %$e )ile sys%em @$ere you @an% %o crea%e %$e

pro4ec%($is can be, )or ins%ance, in your Eclipse @or/space

• Execu%e %$e )ollo@in# command in %$e %erminal @indo@:

mvn mule-project-archetype:create -DgroupId=com.ivan.mule -DartifactId=MuleMavenProject-Dversion=1.0.0-SNAPSHOT -DmuleVersion=3.2.1

($is crea%es a Maven pro4ec% @i%$ %$e )ollo@in# proper%ies:& Bses %$e Mule pro4ec% arc$e%ype& Maven ar%i)ac% #roup id Fcom ivan muleG& Maven ar%i)ac% id, @$ic$ also @ill become %$e Eclipse pro4ec% name, FMuleMaven*ro4ec%G& Maven ar%i)ac% version F! & 9A* <1(G& ($e pro4ec% @ill use Mule version 3 2 !

• Ans@er %$e )ollo@in# 0ues%ions as/ed by Maven in %$e %erminal=command&line @indo@Hsu##es%ed ans@ers in paren%$eses :& *ro4ec% descrip%ion& Mule version pro4ec% is %ar#e%ed a% Huse de)aul% value& $e%$er %$e pro4ec% @ill be $os%ed in %$e Mule6or#e Maven reposi%ory Hno& *ro4ec% base ?ava pac/a#e Hcom=ivan=mule 9o%e slas$es be%@een pac/a#e names& Mule %ranspor%s used by %$e pro4ec% Huse de)aul% %ranspor%s

& Mule modules used by %$e pro4ec% Huse de)aul% modules• "on)irm %$a% pro4ec% @as success)ully crea%ed:

...[INFO] Archetype created in dir: /Users/ivan/Desktop/MuleMavenProject[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 12:36.813s[INFO] Finished at: Tue Jan 03 22:42:21 CST 2012[INFO] Final Memory: 6M/81M...

I) %$ere are problems @$en crea%in# %$e pro4ec%, %ry addin# F&BG H@i%$ou% 0uo%es %o %$eabove Maven command ($is )orces Maven %o c$ec/ )or resource upda%es in remo%ereposi%ories and upda%es any local copy o) %$e Maven Mule pro4ec% arc$e%ype

• In %$e %erminal @indo@, #o in%o %$e ne@ly crea%ed pro4ec% direc%oryIn my case %$e name o) %$e direc%ory is FMuleMaven*ro4ec%G

• Examine %$e con%en%s o) %$e pro4ec% direc%ory($ere s$ould be %@o )iles, FMB-E&REA.ME %x%G and Fpom xmlG, and one direc%ory namedFsrcG

($e pro4ec% $as no@ been crea%ed, bu% i% is 4us% a s/ele%on&pro4ec% Keep %$e %erminal @indo@ open @e @ill use i% in %$e nex% sec%ion @$en preparin# %$e pro4ec% %o be impor%ed in%o Eclipse

2

Page 208: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 208/354

1" & %mport Pro!ect %nto Eclipse1ur nex% s%ep is %o impor% %$e ne@ Mule pro4ec% in%o Eclipse, so %$a% @e can develop %$e Muleapplica%ion and any associa%ed %es%s e%c

• In %$e %erminal @indo@, execu%e %$e )ollo@in# command:

mvn -DdownloadSources=true eclipse:eclipse

($is crea%es an Eclipse pro4ec% )rom %$e Maven pom xml )ile and do@nloads any availablesource code )or %$e libraries used by %$e pro4ec%

• Impor% %$e pro4ec% in%o EclipseIn %$e 6ile menu, selec% Impor% and %$en Exis%in# *ro4ec%s in%o or/space 6inally bro@se%o %$e roo% direc%ory o) %$e pro4ec% %o impor%, @$ic$ is %$e MuleMaven*ro4ec% direc%ory

($ere s$ould no@ be a ne@ pro4ec% in Eclipse @i%$ one Mule con)i#ura%ion )ile Hmule&con)i# xml ,one Mule %es%&case HMuleMaven*ro4ec%(es%"ase 4ava and one Mule con)i#ura%ion )ile )or %$e %es%Hmulemavenpro4ec%&)unc%ional&%es%&con)i# xml :

($e ne@ Mule pro4ec% crea%ed @i%$ Maven and impor%ed in%o Eclipse

2 ;

Page 209: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 209/354

1" ' Configure Pro!ect in EclipseI% s$ould be no%ed %$a% @$en %$e ne@ Mule pro4ec% 4us% $as been crea%ed and impor%ed in%o Eclipse,i% does no% use Maven dependency mana#emen%, nor is i% possible %o use Maven %o build and deploy%$e pro4ec%

e @ill no@ con)i#ure %$e Eclipse pro4ec% %o use Maven dependency mana#emen% and %o be able %ouse Maven )rom @i%$in Eclipse %o build and deploy %$e pro4ec%A prere0uisi%e )or bein# able %o use Maven )rom @i%$in Eclipse is some /ind o) Maven plu#in I)you use prin# ource (ool ui%e, as I $ave done in %$is sec%ion, %$ere @ill already be a Maven

plu#in ins%alled in your I.E• In %$e Eclipse *ac/a#e Explorer Hor in %$e *ro4ec% Explorer , ri#$%&clic/ on %$e pro4ec% node,

selec% "on)i#ure and, in %$e sub&menu %$a% appears, selec% "onver% %o Maven *ro4ec%

"on)i#urin# %$e Eclipse pro4ec% %o use Maven in Eclipse

2 +

Page 210: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 210/354

In %$e *ac/a#e Explorer, you s$ould no@ be able %o see a ne@ node named Maven .ependencies:

Eclipse pro4ec% a)%er Maven dependency mana#emen% $as been enabled

($ere may be an error in %$e Maven pom xml )ile s%a%in# %$a% F*lu#in execu%ion no% covered byli)ecycle con)i#ura%ionG ($is @ill no% a))ec% %$e pro4ec% and %$e messa#e may be i#nored

1p%ionally, @e can also add %$e Mule na%ure %o %$e Eclipse pro4ec% and, as par% o) %$is, disable %$eMule 3 $o% deploymen% builder %$a% %ends %o cause build )ailures

• In %$e Eclipse *ac/a#e Explorer Hor in %$e *ro4ec% Explorer , ri#$%&clic/ on %$e pro4ec% nodeand selec% (o##le Mule 9a%ure

(o##lin# %$e Mule na%ure o) an Eclipse pro4ec%

2!

Page 211: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 211/354

• In %$e Eclipse pro4ec% proper%ies, navi#a%e %o %$e >uilders node, deselec% %$e Mule 3 $o%deploymen% builder and clic/ %$e 1K bu%%on %o save %$e pro4ec% proper%ies

.isablin# %$e Mule 3 $o% deploymen% builder in an Eclipse pro4ec%

($e Eclipse pro4ec% is no@ con)i#ured %o use Maven dependency mana#emen% In %$e nex% sec%ion@e @ill loo/ a% $o@ %o build and deploy %$e pro4ec% usin# Maven

2!!

Page 212: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 212/354

1" * 6se (a$en in Eclipse 9o% only can Maven aid us in $andlin# %$e pro4ec% dependencies, bu% Maven can also $elp us %o build our pro4ec% and deploy i% %o a runnin# Mule ins%ance Bsin# %$e FmvnG command, @e can do%$is )rom a %erminal=command&line @indo@ In %$is sec%ion @e @ill also learn $o@ %o use Maven)rom @i%$in Eclipse

Maven 4oals

Maven #oals are similar %o commands %$a% can be issued %o Maven $en @or/in# @i%$ a Muleapplica%ion, %$e )ollo@in# Maven #oals are commonly used:

Maven Goal Description

pac/a#e "rea%e a zip&arc$ive %$a% con%ains %$e Muleapplica%ion

ins%all "rea%e a zip&arc$ive con%ainin# %$e Mule

applica%ion, s%ore i% in %$e local Mavenreposi%ory and deploy i% %o a Mule server

deploy "rea%e a zip&arc$ive %$a% con%ains %$e Muleapplica%ion and s%ore i% in %$e local Mavenreposi%ory

clean .ele%e %$e zip&arc$ive %$a% con%ains %$e Muleapplica%ion alon# @i%$ any o%$er ar%i)ac%s#enera%ed by Maven @$en buildin# %$e Muleapplica%ion

%es% Runs all %$e %es%s presen% in %$e pro4ec%

In addi%ion, all %$e Maven #oals lis%ed above, excep% )or FcleanG, causes %$e %es%s presen% in %$e pro4ec% %o be run I) any %es% )ails, %$e Maven command @ill )ail(es%s can be s/ipped by se%%in# %$e Fmaven %es% s/ipG parame%er @i%$ %$e value %rue @$en invo/in#Maven

2!2

Page 213: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 213/354

Page 214: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 214/354

• 1n %$e le)% side o) %$e Run "on)i#ura%ions dialo#, loca%e %$e Maven >uild #roup

-oca%in# %$e Maven >uild #roup amon# %$e run con)i#ura%ions in Eclipse

• Ri#$%&clic/ %$e Maven >uild node Hselec%ed in %$e above pic%ure and selec% 9e@A ne@ run con)i#ura%ion s$ould appear in %$e Maven >uild node and i%s se%%in#s appears on%$e ri#$% side in %$e dialo#

• In %$e run con)i#ura%ion, en%er FIns%all MuleMaven*ro4ec%G as %$e name o) %$e runcon)i#ura%ion

($is is an arbi%rary name %$a% $elps us %o iden%i)y %$e run con)i#ura%ion• "lic/ %$e >ro@se or/space bu%%on and selec% %$e Fmulemavenpro4ec%G pro4ec% as base

direc%ory o) %$e run con)i#ura%ion($is is %$e direc%ory %$a% con%ains %$e pro4ec%'s Maven pom xml )ile

Hcon%inued on nex% pa#e

2!5

Page 215: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 215/354

• In %$e Coals )ield, en%er Fins%allG($is is %$e Maven #oal %$a% is %o be execu%ed @$en %$is run con)i#ura%ion is used and i%s$ould be c$an#ed accordin# %o %$e purpose o) %$e run con)i#ura%ion($e Main %ab o) %$e run con)i#ura%ion se%%in#s s$ould no@ loo/ li/e in %$e pic%ure belo@:

"rea%in# a Maven run con)i#ura%ionJ main se%%in#s %ab

• @i%c$ %o %$e Environmen% %ab in %$e run con)i#ura%ion dialo#• "lic/ %$e 9e@ bu%%on %o crea%e an environmen% variable

En%er %$e variable&name FMB-EP<1MEG and %$e pa%$ %o %$e server direc%ory o) %$e Muleserver @e s%ar%ed earlier Environmen% variables lis%ed in %$is %ab @ill be se% prior %oexecu%in# %$e Maven #oal o) %$e run con)i#ura%ion

"rea%in# a Maven run con)i#ura%ionJ se%%in# %$e environmen% variable speci)yin# %$e

loca%ion o) %$e s%andalone Mule server %o @$ic$ %$e Mule applica%ion @ill be deployed

2!8

Page 216: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 216/354

• "lic/ %$e Apply bu%%on in %$e lo@er ri#$% par% o) %$e dialo#• "lic/ %$e Run bu%%on belo@ %$e Apply bu%%on

($e pro4ec% s$ould no@ be buil% and deployed %o %$e Mule server($e las% par% o) %$e console ou%pu% )rom Maven s$ould loo/ similar %o %$is:

...[INFO] Executing tasks [copy] Copying 1 file to /Volumes/.../mule-standalone-3.2.0/apps[INFO] Executed tasks[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 19.528s[INFO] Finished at: Tue Jan 10 18:05:21 CET 2012[INFO] Final Memory: 12M/81M[INFO] ------------------------------------------------------------------------

9o%e %$e ro@ indica%in# %$a% one )ile @as copied %o %$e FappsG direc%ory in %$e s%andaloneMule server direc%ory %$is is %$e Mule applica%ion bein# deployed %o %$e server

• Examine %$e console ou%pu% )orm %$e Mule s%andalone server, @$ic$ s$ould ou%pu% similar %o%$e )ollo@in#:

...INFO 2012-01-10 18:05:22,848 [Mule.app.deployer.monitor.1.thread.1]org.mule.module.launcher.DefaultMuleDeployer: Exploding a Mule application archive:file:/Volumes/.../mule-standalone-3.2.0/apps/mulemavenproject-1.0.0-SNAPSHOT.zipINFO 2012-01-10 18:05:22,852 [Mule.app.deployer.monitor.1.thread.1]org.mule.module.launcher.application.DefaultMuleApplication:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ New app 'mulemavenproject-1.0.0-SNAPSHOT' +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++INFO 2012-01-10 18:05:23,536 [Mule.app.deployer.monitor.1.thread.1]org.mule.module.launcher.DeploymentService:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Started app 'mulemavenproject-1.0.0-SNAPSHOT' +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

($e above ou%pu% indica%es %$a% our Mule applica%ion $as success)ully been deployed%o %$e Mule server and s%ar%ed

2!7

Page 217: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 217/354

Page 218: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 218/354

13. Mule Configuration 'atternsIn Mule 3 x %$ere are )our con)i#ura%ion pa%%erns %$a% represen% common scenarios ($e pa%%erns are:

• >rid#e• imple ervice• Valida%or • ebservice *roxy

In %$is c$ap%er @e @ill loo/ a% eac$ o) %$ese con)i#ura%ion pa%%erns 9o%e %$a% %$ese con)i#ura%ion pa%%erns can only be used @i%$ Mule 3 x and %$e minimum version is3 ! !, in order )or %$e examples in %$is c$ap%er %o @or/ as expec%ed

1& 1 Create the Pro!ectAll %$e examples )or %$e di))eren% con)i#ura%ion pa%%erns are developed in one and %$e same pro4ec%

ince %$e examples @ill only run on Mule 3 x, %$e pro4ec% is %o be con)i#ured )or Mule 3 x )rom %$es%ar%"rea%e %$e pro4ec% as described in %$e appendix"rea%e a Mule *ro4ec%, namin# i% FMule*a%%ernsG($e Mule 3 $o% deploymen% can be s@i%c$ed o)) )rom %$e s%ar%, as %$is )ea%ure @ill no% be used

In addi%ion, ma/e %$e )ollo@in# prepara%ions in %$e Eclipse pro4ec%:• "rea%e a source pac/a#e named com.ivan.exceptionhandlers• "rea%e a source pac/a#e named com.ivan.4axb6generated • "rea%e a source pac/a#e named com.ivan.muleconfig •

"rea%e a source pac/a#e named com.ivan.schemas• "rea%e a source pac/a#e named com.ivan.services• "rea%e a direc%ory named Fexample&da%aG in %$e roo% o) %$e Eclipse pro4ec%

2!;

Page 219: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 219/354

Page 220: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 220/354

S#nchronous ridge

($e )irs% example s$o@s a sync$ronous brid#e %$a%, @$en $avin# received a messa#e, passes i% onand @ai%s )or a response Apar% )rom sync$ronous communica%ion, %$is example also s$o@s %$e)ollo@in# )acili%ies o) %$e brid#e pa%%ern:

>rid#e in$eri%anceAn abs%rac% brid#e can be de)ined and in$eri%ed )rom by brid#es %$a% are %o s$are %$ecommon proper%ies o) %$e paren%

• (rans)orma%ion o) response messa#es• "on)i#ura%ion o) in& and ou%bound endpoin%s usin# a%%ribu%es o) %$e brid#eL elemen%

Create %nboun/ an/ Outboun/ Ser$ices

($is brid#e example ma/e use o) %@o )lo@s=services:

A receiver )lo@, @$ic$ receives a messa#e @$en a )ile is placed in a cer%ain direc%ory and passes i%on A separa%e receiver )lo@ is used in order %o be able %o lo# response messa#esA @ri%er )lo@ %$a% receives messa#es sync$ronously and lo#s %$em %o %$e console Responsemessa#es )rom %$is )lo@ are iden%ical %o %$e correspondin# re0ues% messa#e($e @ri%er )lo@ assumes %$a% %$e payload o) incomin# messa#es are con%ained in a by%e array

• Add %$e )ollo@in# )lo@ con)i#ura%ions %o %$e Fmule&brid#e&con)i# xmlG )ile, immedia%ely be)ore %$e closin# muleL %a#:

... <!-- First receiver flow. Receives contents of files placed in a certain directory, synchronously sending messages to the first bridge and,

when a response is received, logs the message payload to the console. --> <flow name="receiverFlow1"> <file:inbound-endpoint connector-ref="fileConnector" path="./receiver1-input-directory"> </file:inbound-endpoint>

<outbound-endpoint address="vm://bridgeInbound" exchange-pattern="request-response"/>

<logger level="WARN" message="Receiver 1: #[payload]"/> </flow>

<!-- First writer flow. Synchronously receives messages and logs their payload to the console. Assumes that message payloads are byte arrays and need to be converted to a string in order to produce a human-readable log. --> <flow name="writerFlow1"> <inbound-endpoint address="vm://writerService" exchange-pattern="request-response"/>

<logger level="ERROR" message="Writer 1: #[groovy:new String(payload)]"/> </flow>...

22

Page 221: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 221/354

9o%e %$a%:• ($e )irs% )lo@, Freceiver6lo@!G, receives messa#es )rom a )ile inbound endpoin% and

sync$ronously pass %$em on %o a VM ou%bound endpoin%($e name o) %$e direc%ory in @$ic$ %$e inbound endpoin% @ill loo/ )or )iles is Freceiver!&inpu%&direc%oryG

($e ou%bound endpoin% o) %$e Freceiver6lo@!G $as %$e messa#e exc$an#e pa%%ern Fre0ues%&responseG• ($e Freceiver6lo@!G lo#s response messa#es re%urned )rom %$e VM ou%bound endpoin%

($e expression in %$e lo# %$a% re%rieves %$e messa#e payload is TUpayload ($is means %$a%%$e messa#e payload mus% be a s%rin#, in order )or %$e payload %o be displayed in a $uman&readable )orm

• I) i% @eren'% )or %$e lo##in# o) response messa#es, %$e Freceiver6lo@!G could $ave beenreplaced by a brid#e

• ($e second )lo@, %$e F@ri%er6lo@!G, receives messa#es )rom a VM inbound endpoin% and

lo#s %$e messa#es• ($e inbound endpoin% o) %$e F@ri%er6lo@!G $as %$e messa#e exc$an#e pa%%ern Fre0ues%&

responseG• ($e lo# messa#e in %$e F@ri%er6lo@!G )lo@'s lo##er con%ains %$e expression TU#roovy:ne@

%rin#Hpayload($e F@ri%er6lo@!G )lo@ ma/es %$e %rans)orma%ion )rom by%e array payload %o a s%rin# inconnec%ion %o @ri%in# %$e lo#

• ($e F@ri%er6lo@!G )lo@ lo#s %$e messa#e payload assumin# i% is a by%e array, @$ile %$eFreceiver6lo@!G )lo@ lo#s %$e messa#e payload assumin# i% is a s%rin#($e brid#e %$a% @e inser% be%@een %$ese %@o )lo@s, or services, @ill $ave %o ensure %$a% %$emessa#e payload reac$in# %$e F@ri%er6lo@!G )lo@ is a by%e array and %$a% %$e messa#e

payload reac$in# %$e lo##er o) %$e Freceiver6lo@!G )lo@ is a s%rin#• ($e ou%bound endpoin% o) %$e Freceiver6lo@!G )lo@ uses %$e messa#e exc$an#e pa%%ern

Fre0ues%&responseG ($e inbound endpoin% o) %$e F@ri%er6lo@!G )lo@ uses %$e same messa#eexc$an#e pa%%ern, so %$ere is no misma%c$ concernin# %$is re#ard

e ne@ $ave some re0uiremen%s )or %$e brid#e %$a% is %o connec% %$e %@o )lo@s discussed above

22!

Page 222: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 222/354

ri/ge the Ser$ices

$en brid#in# %$e %@o )lo@s, or services, cons%ruc%ed in %$e previous sec%ion, @e @ill, )or %$e sa/eo) %$e example, use no% one, bu% %@o brid#es ($is is %o s$o@ %$a% brid#es suppor% in$eri%ance ecan de)ine an abs%rac% brid#e %$a% ex%rac%s proper%ies common %o mul%iple brid#es and %$us reducerepe%i%ion and simpli)y main%enanceIn %$is example, @e de)ine an abs%rac% paren% brid#e %$a% $as %$e re0ues%&response messa#e exc$an#e

pa%%ern and %$a% %rans)orms response messa#es #oin# %$rou#$ %$e brid#e )rom a by%e array %o anob4ec% Hs%rin# ($e c$ild brid#e %$en only need %o speci)y, apar% )rom i%s paren%, %$e inbound andou%bound endpoin%s %$a% i% is %o connec%

• Add %$e )ollo@in# )lo@ con)i#ura%ions %o %$e Fmule&brid#e&con)i# xmlG )ile, immedia%ely be)ore %$e closin# muleL %a#:

... <!-- Parent, abstract, bridge. Bridges can be declared as abstract in order to serve as a common parent to one or more bridges. The Spring bean inheritance mechanism facilitates bridge inheritance. This bridge specifies the use of the request-response message exchange pattern and the use of a byte-to-object transformer on response messages. --> <pattern:bridge name="syncResponseXformBridge" abstract="true" exchange-pattern="request-response" responseTransformer-refs="bytesToObjectXform"/>

<!-- First bridge. This bridge transfers messages from the first bridge input endpoint to the first writer service. This bridge inherits some properties from the above, abstract, bridge. This cause this bridge to use the request-response

message exchange pattern and to apply a byte-to-object transform on response messages received from the first writer service. --> <pattern:bridge name="receiverToWriterBridge" parent="syncResponseXformBridge" inboundAddress="vm://bridgeInbound" outboundAddress="vm://writerService"/>...

9o%e %$a%:• ($e paren% brid#e is named Fsync$Response )orm>rid#eG• ($e paren% brid#e is abs%rac%

($is is accomplis$ed by se%%in# value o) %$eabstract a%%ribu%e o) %$e brid#eL elemen% %o%rue

• ($e paren% brid#e uses %$e messa#e exc$an#e pa%%ern Fre0ues%&responseG• ($e paren% brid#e speci)ies %$a% %$e %rans)ormer Fby%es(o1b4ec% )ormG is %o be applied %o

response messa#es passin# %$rou#$ %$e brid#e($e value o) %$e response'ransformer-refs a%%ribu%e can con%ain a lis% o) %rans)ormers %o beapplied %o response messa#es, @$ic$ are response messa#es received )rom %$e service @$ic$address is %$e ou%bound address o) %$e brid#e

I) mul%iple names occur in %$e %rans)ormer lis%, %$ey are %o be separa%ed by spaces

222

Page 223: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 223/354

• ($e )irs%, concre%e, brid#e is named Freceiver(o ri%er>rid#eG• ($e )irs% brid#e $as %$e paren% brid#e Fsync$Response )orm>rid#eG

($is rela%ion is declared usin# %$e parent a%%ribu%e o) %$e brid#eL elemen%• ($e )irs% brid#e declare an inbound and an ou%bound address

($e inbound address is %$e address %o @$ic$ %$e receiver )lo@ sends messa#es i% receives

($e ou%bound address is %$e address o) %$e inbound endpoin% o) %$e @ri%er )lo@ declaredearlierI% is also possible %o use re)erences %o #lobal endpoin%s, usin# %$einbound&ndpoint-ref andoutbound&ndpoint-ref a%%ribu%es o) %$e brid#eL elemen%, or, as @e @ill see an example o)la%er, declare in& and ou%bound endpoin%s as c$ild elemen%s o) %$e brid#eL elemen%

+un the S<nchronous ri/ge

e are no@ ready %o %es% %$e sync$ronous brid#e• Ri#$%&clic/ %$e Fmule&brid#e&con)i# xmlG Mule con)i#ura%ion )ile and selec% Run As &L

Mule erver• Re)res$ %$e pro4ec%

A ne@ direc%ory, Freceiver!&inpu%&direc%oryG, s$ould appear in %$e roo% o) %$e pro4ec% ($is is%$e direc%ory in @$ic$ @e place )iles @$ic$ con%en%s is %o be sen% %o %$e receiver )lo@ o) %$eexample

• In Eclipse, copy %$e )ile Fperson xmlG in %$e Fexample&da%aG direc%ory and pas%e i% on%o %$eFreceiver!&inpu%&direc%oryG

• Examine %$e console($e )ollo@in# s$ould $ave been lo##ed %o %$e console, amon# o%$er %$in#s:

...ERROR 2012-03-07 16:39:07,488 [receiverFlow1.stage1.02]org.mule.api.processor.LoggerMessageProcessor: Writer 1: <?xml version="1.0"encoding="UTF-8"?><ivan:person xmlns:ivan="http://www.ivan.com/schemas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ivan.com/schemas schema2.xsd "> <firstName>Stiv</firstName> <lastName>Bator</lastName> <age>32</age></ivan:person>

WARN 2012-03-07 16:39:07,509 [receiverFlow1.stage1.02]org.mule.api.processor.LoggerMessageProcessor: Receiver 1: <?xml version="1.0"encoding="UTF-8"?><ivan:person

xmlns:ivan="http://www.ivan.com/schemas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ivan.com/schemas schema2.xsd "> <firstName>Stiv</firstName> <lastName>Bator</lastName> <age>32</age></ivan:person>

9o%e %$a%:• ince %$e en%ire messa#e )lo@ is sync$ronous, %$e messa#es are displayed in %$e order %$a%

%$ey are processed by componen%s par%icipa%in# in %$e messa#e )lo@• ($e )irs% lo# en%ry @as @ri%%en by %$e @ri%er H#reen $i#$li#$%

In addi%ion, i% @as @ri%%en usin# %$e ERR1R lo# level, @$ic$ is @$a% %$e lo##er in %$e @ri%er

223

Page 224: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 224/354

is con)i#ured %o use• ($e M- in %$e )irs% lo# en%ry is $uman&readable

($e conversion in %$e lo##er )rom by%e array %o s%rin# @or/ed as expec%ed ($is also means%$a% %$e messa#e payload a% %$is s%a#e in %$e messa#e )lo@ indeed is a by%e array

• ($e second lo# en%ry @as @ri%%en by %$e receiver Hblue $i#$li#$%

($e lo# level @as AR9, @$ic$ is @$a% %$e receiver's lo# is con)i#ured %o use• ($e M- in %$e second lo# en%ry is also $uman&readable

($e lo##er in %$e receiver @ri%es %$e payload direc%ory %o %$e lo# e can %$us conclude %$a%%$e brid#e $as success)ully %rans)ormed %$e response messa#e )rom %$e @ri%er )rom a by%earray %o a s%rin#

e conclude %$a% %$e brid#e $as )ul)illed %$e re0uiremen%s no%ed earlier and conveyed messa#es be%@een %$e %@o )lo@s=services as @ell as made %$e necessary %rans)orma%ions o) %$e messa#e)orma%

225

Page 225: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 225/354

!s#nchronous ridge

($e second brid#e example s$o@s an async$ronous brid#e %$a% passes messa#es )rom a )ile inboundendpoin% %o a @ri%er )lo@=service @i%$ou% @ai%in# )or any response .ue %o usin# async$ronouscommunica%ion, %$ere is no reason )or $avin# a receiver )lo@=service %$ere @ill be no responsemessa#e %o lo#In addi%ion %o async$ronous messa#e exc$an#e, %$e example also s$o@s %$e )ollo@in#:

• (rans)orma%ion o) messa#es received by %$e brid#e prior %o passin# %$em on• "on)i#ura%ion o) %$e inbound and ou%bound endpoin%s o) %$e brid#e as c$ild elemen%s o) %$e

brid#eL elemen%• "on)i#ura%ion o) an excep%ion s%ra%e#y on %$e brid#e• 9o%in# %$a% a brid#e can be %ransac%ional

Create the Outboun/ Ser$ice

($e async$ronous brid#e example uses one sin#le )lo@=serviceJ a @ri%er )lo@=service %$a% receivesmessa#es async$ronously and lo#s %$em %o %$e console 9o response messa#es are sen% )rom %$e@ri%er )lo@=service($is @ri%er )lo@ assumes %$a% %$e payload o) incomin# messa#es are s%rin#s

• Add %$e )ollo@in# )lo@ con)i#ura%ions %o %$e Fmule&brid#e&con)i# xmlG )ile, immedia%ely be)ore %$e closin# muleL %a#:

... <!-- Second writer flow. Asynchronously receives messages and logs their payload to the console. Assumes that message payloads are strings which require no further transformation to produce a human-readable log. --> <flow name="writerFlow2"> <inbound-endpoint address="vm://writerService2" exchange-pattern="one-way"/>

<logger level="ERROR" message="Writer 2: #[payload]"/> </flow>...

9o%e %$a%:• ($e )lo@ F@ri%er6lo@2G receives messa#es )rom a VM inbound endpoin% and lo#s %$e

messa#es• ($e inbound endpoin% o) %$e F@ri%er6lo@2G $as %$e messa#e exc$an#e pa%%ern Fone&@ayG• ($e lo# messa#e in %$e F@ri%er6lo@2G )lo@'s lo##er direc%ly lo#s %$e messa#e payload, as

re%rieved by %$e expression TUpayload($e assump%ion is made %$a% %$e messa#e payload is a s%rin#

ince %$e inbound endpoin% is %o be declared in %$e brid#e, @e are no@ ready %o con)i#ure %$e brid#e

228

Page 226: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 226/354

ri/ge the Ser$ices

As no%ed earlier, %$ere is no receiver service in %$is example Ins%ead, %$e brid#e @e are abou% %ocon)i#ure conveys da%a received by an inbound )ile endpoin% %o %$e @ri%er )lo@=service @e crea%ed in%$e previous sec%ionIn %$is second brid#e example, @e @ill use one sin#le brid#e @$ic$ $as %$e inbound and ou%boundendpoin%s con)i#ured usin# c$ild elemen%s o) %$e brid#eL elemen%, as opposed %o usin# a%%ribu%esas in %$e previous example6or %$e sa/e o) s$o@in# %$a% i% is possible, @e @ill also con)i#ure an excep%ion s%ra%e#y on %$e

brid#e as @ell as ma/in# %$e brid#e %ransac%ional• Add %$e )ollo@in# )lo@ con)i#ura%ions %o %$e Fmule&brid#e&con)i# xmlG )ile, immedia%ely

be)ore %$e closin# muleL %a#:... <!-- Second bridge. This bridge transfers messages directory from a file inbound endpoint to the second writer output in an asynchronous fashion. There is a byte-to-object transformer applied on incoming messages. Note also how the transacted attribute on the <bridge> element is set to true. This enables transaction support for the bridge, provided that the transports used by the inbound and outbound endpoints supports transactions. Both the endpoints are configured using child elements of the <bridge> element. The bridge also has a exception strategy configured, which will log exceptions.

--> <pattern:bridge name="receiverToWriterBridge2" exchange-pattern="one-way" transformer-refs="bytesToObjectXform"

transacted="true"> <file:inbound-endpoint connector-ref="fileConnector" path="./receiver2-input-directory"> </file:inbound-endpoint> <vm:outbound-endpoint path="writerService2"/>

<default-exception-strategy> <logger level="ERROR" message="An exception occurred!"/> </default-exception-strategy> </pattern:bridge>...

9o%e %$a%:• ($e brid#e is named Freceiver(o ri%er>rid#e2G• ($e brid#e is con)i#ured %o use %$e one&@ay messa#e exc$an#e pa%%ern

Bsin# %$is messa#e exc$an#e pa%%ern, %$e brid#e @ill no% respond %o incomin# messa#es nora@ai% responses )rom ou%#oin# messa#es

• ($e brid#eL elemen% con%ains a transformer-refs a%%ribu%e re)erencin# %$e #lobal by%e&array&%o&ob4ec% %rans)ormer con%ained in %$e con)i#ura%ion )ile($is %rans)ormer is applied %o messa#es received by %$e brid#e, be)ore %$ey are passed on %o%$e ou%bound endpoin%

• ($e brid#eL elemen% con%ains %$etransacted a%%ribu%e %$a% $as %$e value F%rueG

($is is ac%ually no% relevan% %o %$is example, since %$e nei%$er %$e inbound nor %$e ou%bound

227

Page 227: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 227/354

endpoin% o) %$e brid#e connec%s %o a %ransac%ional resourceI) %$ey did, se%%in# %$is a%%ribu%e %o %rue @ill cause %$e brid#e %o pus$ bac/ a messa#ereceived on %$e inbound endpoin%, )or ins%ance )rom a ?M 0ueue, i) sendin# %$e messa#e %o%$e ou%bound endpoin%, )or ins%ance inser%in# i% in%o a da%abase %able, )ails

• ($e brid#e does no% declare an inbound nor an ou%bound address•

($e brid#eL elemen% con%ains an inbound&endpoin%L and an ou%bound&endpoin%L c$ildelemen%sIns%ead o) usin# inbound and ou%bound addresses, as in %$e previous example, @e candeclare %$e endpoin%s o) %$e brid#e as c$ild elemen%s o) %$e brid#eL elemen%As men%ioned in %$e previous example, bu% no% s$o@n in %$is c$ap%er, is %$e possibili%y %o usere)erences %o #lobal endpoin%s, @i%$ %$einbound&ndpoint-ref and outbound&ndpoint-refa%%ribu%es o) %$e brid#eL elemen%

• ($e brid#eL elemen% con%ains a de)aul%&excep%ion&s%ra%e#yL c$ild elemen%I% is possible %o con)i#ure an excep%ion s%ra%e#y )or %$e brid#e i%sel), @$ic$ @ill come in%oe))ec% i) an error occurs @$en a messa#e passes %$rou#$ %$e brid#e

6or de%ails on excep%ion $andlin# s%ra%e#ies, please re)er %o %$e c$ap%er onExcep%ion<andlin# in Mule

+un the -s<nchronous ri/ge

e are no@ ready %o %es% %$e async$ronous brid#e• Ri#$%&clic/ %$e Fmule&brid#e&con)i# xmlG Mule con)i#ura%ion )ile and selec% Run As &L

Mule erver• Re)res$ %$e pro4ec%

A ne@ direc%ory, Freceiver2&inpu%&direc%oryG, s$ould appear in %$e roo% o) %$e pro4ec% ($is is

%$e direc%ory in @$ic$ @e place )iles @$ic$ con%en%s is %o be sen% %o %$e receiver )lo@ o) %$eexample• In Eclipse, copy %$e )ile Fperson xmlG in %$e Fexample&da%aG direc%ory and pas%e i% on%o %$e

Freceiver2&inpu%&direc%oryG

($e M- con%en%s o) %$e Fperson xmlG )ile s$ould $ave been @ri%%en %o %$e console by %$e second@ri%er ($ere @ill be only one sin#le occurrence o) %$e M- messa#e, since %$ere is only one sin#lelo##er in %$e messa#e )lo@

22

Page 228: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 228/354

1& & The Simple Ser$ice Pattern($e simple service pa%%ern enables s@i)% developmen% o) a service @i%$ one inbound endpoin% andone componen% ($is sec%ion @ill s$o@ %$e di))eren% possibili%ies available @$en usin# %$is pa%%ernIn addi%ion, @e @ill see $o@ %o develop services %$a% uses di))eren% @ays %o process %$e payload o)messa#es received & )or ins%ance ?A >, ?A & and ?A &R

• As a prepara%ion )or %$e simple&service examples, crea%e a Mule con)i#ura%ion )ile namedFmule&simpleservices&con)i# xmlG in %$ecom.ivan.muleconfig pac/a#e @i%$ %$e )ollo@in#con%en%s:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:mule-xml="http://www.mulesoft.org/schema/mule/xml" xmlns:test="http://www.mulesoft.org/schema/mule/test" xmlns:pattern="http://www.mulesoft.org/schema/mule/pattern"

xmlns:ivan="http://www.ivan.com/person" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/core

http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd

http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.mulesoft.org/schema/mule/xml

http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd

http://www.mulesoft.org/schema/mule/test http://www.mulesoft.org/schema/mule/test/3.2/mule-test.xsd

http://www.mulesoft.org/schema/mule/pattern http://www.mulesoft.org/schema/mule/pattern/3.2/mule-pattern.xsd">

<!-- Global transformers used by all the example simple-services. --> <append-string-transformer name="firstAppender" message="[first appender msg]"/> <append-string-transformer name="secondAppender" message="[second appender msg]"/>

</mule>

9o%e %$a%:• ($ere are a number o) namespace pre)ixes de)ined in %$e Mule con)i#ura%ion )ile

($ese are pre)ixes )or di))eren% Mule con)i#ura%ion namespaces @$ic$ @e @ill need in %$eexample, excep% )or %$e FivanG pre)ix, @$ic$ is )or a M- sc$ema namespace

• ($e Mule con)i#ura%ion )ile con%ains %@o %rans)ormers>o%$ %$e %rans)ormers append a s%rin# %o %$e messa#e %o @$ic$ %$ey are applied

• >o%$ %rans)ormers $ave a name($is in combina%ion @i%$ %$eir loca%ion in %$e con)i#ura%ion )ile ma/es %$em #lobal%rans)ormers

22;

Page 229: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 229/354

-!09RS Si&ple Service

In %$is par% o) %$e example, @e @ill develop a ?A &R @eb service usin# %$e simple service pa%%erne @ill also see $o@ %o apply %rans)ormers on incomin# messa#es be)ore %$ey are sen% %o %$e

componen% o) %$e service and on %$e response messa#e )rom %$e componen% o) %$e service

Create the Component %mplementation Class

($e componen%=service implemen%a%ion class used @i%$ %$e ?A &R service @ill also be used @i%$%$e ?A & service developed in %$e nex% sec%ion, so i% con%ains no% only ?A &R anno%a%ions, bu%also a ?A & anno%a%ion

• In %$ecom.ivan.services pac/a#e, implemen% %$e HelloService class as %$is:

package com.ivan.services;

import java.util.Date;import javax.jws.WebService;import javax.ws.rs.GET;import javax.ws.rs.Path;

import javax.ws.rs.PathParam;import javax.ws.rs.Produces;

/** * Web service endpoint implementation class that implements * a service that extends greetings. *

* @author Ivan A Krizsan */@Path("greeting")@WebServicepublic class HelloService{ /** * Greets the person with the supplied name. *

* @param inName Name of person to greet. * @return Greeting. */ @GET @Path("{name}") @Produces("text/html") public String greet(@PathParam("name") final String inName) { return "Hello " + inName + ", the time is now " + new Date(); }}

9o%e %$a%:• ($e class is anno%a%ed @i%$ %$e ?A & Y eb ervice anno%a%ion

($is @ill cause ?A & %o expose all public me%$ods in a 1A* @eb service• ($e class is anno%a%ed @i%$ %$e ?A &R Y*a%$ anno%a%ion

($e Y*a%$ anno%a%ion speci)ies %$a% %$e #ree%in#&resource mana#ed by %$is class is availablea% %$e F#ree%in#G pa%$ ($is pa%$ is rela%ive %o %$e service endpoin% BR- as @ill be speci)iedin %$e Mule con)i#ura%ion )ile la%er

• ($e greet me%$od is anno%a%ed @i%$ %$e ?A &R YCE( anno%a%ion$en issuin# a <((* CE( %o %$e #ree%in# resource, %$e greet me%$od is %$e me%$od %$a% @ill

become invo/ed• ($e greet me%$od is also anno%a%ed @i%$ %$e ?A &R Y*a%$ anno%a%ion

In %$e case o) %$e greet me%$od, %$e Y*a%$ anno%a%ion is used %o %ell ?A &R %$a% a par% o)

22+

Page 230: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 230/354

%$e BR- @ill be used as a parame%er %o %$e anno%a%ed me%$od• 6inally, %$e #ree% me%$od is also anno%a%ed @i%$ %$e ?A &R Y*roduces anno%a%ion

($e Y*roduces anno%a%ion de%ermines @$a% %ype o) resul% @ill produce In %$is case, i% @ill be <(M- %ex% ($e me%$od does no% ac%ually produce <(M- %ex%, bu% usin# %$is %ype o)resul%, @e @ill be able %o vie@ %$e s%rin# re%urned )rom %$e greet me%$od in a bro@ser

(o/if< the (ule Configuration )ile

(o comple%e %$e ?A &R simple service example, @e append a simple service con)i#ura%ion %o ourMule con)i#ura%ion )ile:

• Add %$e )ollo@in# %o %$e Fmule&simpleservices&con)i# xmlG )ile, immedia%ely be)ore %$eclosin# muleL %a#:

... <!-- Example of simple service exposing a JAX-RS web service. The transformers listed in the transformer-refs attribute are applied to the message before it is sent to the service's component. The transformers listed in the responseTransformer-refs attribute are applied to the message returned by the service's component.

Note that the appenders does not affect the parameter nor the result of the JAX-RS service. They may affect the payload.

Simple services only work with request-response inbound endpoints, so we cannot use, for instance, file inbound endpoints, since they are one-way. --> <pattern:simple-service name="MySimpleJAXRSService" address="http://localhost:8182/Services/JAXRS/" transformer-refs="firstAppender" responseTransformer-refs="secondAppender" type="jax-rs">

<component class="com.ivan.services.HelloService"/> </pattern:simple-service>...

9o%e %$a%:• ($e simple&serviceL elemen% belon#s %o %$e pa%%ern namespace• ($ere is an address a%%ribu%e in %$e simple&serviceL elemen%

($is a%%ribu%e is used %o speci)y %$e roo% BR- o) %$e ?A &R resource ($us, %$e #ree%in#resource @ill $ave %$e BR- $%%p:==local$os%:;!;2= ervices=?A R =#ree%in#=

• ($ere is a transformer-refs a%%ribu%e in %$e simple&serviceL elemen%Bsin# %$is a%%ribu%e, @e can speci)y %$e %rans)ormerHs %$a% are %o be applied %o messa#esreceived by %$e service, prior %o messa#es bein# passed %o %$e componen% o) %$e service ($evalue o) %$e a%%ribu%e con%ains %$e names o) %$e #lobal %rans)ormers, separa%ed by spaces

• ($e simple&serviceL elemen% con%ains aresponse'ransformer-refs a%%ribu%eimilar %o %$e %rans)ormer&re)s a%%ribu%e, %$is a%%ribu%e also allo@s us %o speci)y one or more

%rans)ormerHs ($ese %rans)ormers are, $o@ever, %o be applied %o messa#e produced by %$ecomponen%, %$a% is %$e response messa#e o) %$e simple service

• ($e simple&serviceL elemen% con%ains at$pe a%%ribu%e($e value o) %$e t$pe a%%ribu%e %ells Mule @$e%$er %o expose %$e simple service as a ?A &

or ?A &R @eb service I) omi%%ed, %$e simple service @ill expose a plain endpoin% usin# %$e

23

Page 231: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 231/354

%ranspor% speci)ied in %$eaddress a%%ribu%e e @ill see examples o) %$is la%er• ($e simple&serviceL $as a componen%L c$ild elemen%

($is elemen% is used %o speci)y %$e class implemen%in# %$e componen% %$a% are %o processmessa#es received by %$e service

+un the 7-5=+S Simple Ser$icee are no@ ready %o %ry %$e ?A &R simple service

• Ri#$%&clic/ %$e Fmule&simpleservices&con)i# xmlG Mule con)i#ura%ion )ile and selec% Run As&L Mule erver

• In a @eb bro@ser, en%er %$e BR-$%%p:==local$os%:;!;2= ervices=?A R =#ree%in#=?ames($e name a% %$e end o) %$e BR- may be replaced @i%$ arbi%rary name A #ree%in# s%rin#similar %o %$e )ollo@in# s$ould be displayed in %$e bro@ser:

Hello James, the time is now Wed Feb 29 07:01:14 CET 2012

9o%e %$a%:• 9ei%$er o) %$e %rans)ormers con)i#ured on %$e simple service $ave appended %$eir messa#e %o

%$e #ree%in# s%rin# @e see in %$e bro@ser($e %rans)ormers a))ec% %$e payload o) %$e messa#e, bu% @$en %$e ?A &R @eb service s%ac/used by Mule analyzes %$e re0ues%, i% uses values )rom Mule messa#e $eaders $en %$eresponse is crea%ed, i% is @ri%%en %o an ou%pu% s%ream main%ained in parallel @i%$ %$e re#ularmessa#e payload and %$e %rans)ormer does no% a))ec% %$e da%a o) %$e s%ream

23!

Page 232: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 232/354

Si&ple Services and )nheritance

Bsin# %$e in$eri%ance mec$anism available %o prin# beans, @e can declare a simple paren% service@$ic$ can be in$eri%ed )rom by o%$er simple servicesIn %$is par% o) %$e example, a simple paren% service is con)i#ured @i%$ a cus%om excep%ion s%ra%e#yA c$ild simple service is also con)i#ured, %o s$o@ %$a% %$e excep%ion s%ra%e#y applies %o c$ildren o)%$e paren% serviceIn addi%ion, @e @ill use %$e %es% componen%, @$ic$ is described in %$e(es%in# re)erence in %$esecond par% o) %$is boo/, and see %$e %rans)ormers %$a% @e de)ined @$en crea%in# %$e Mulecon)i#ura%ion )ile in ac%ion

(o/if< the (ule Configuration )ile

e con%inue %o build on %$e same Mule con)i#ura%ion )ile, Fmule&simpleservices&con)i# xmlG:• Add %$e )ollo@in# %o %$e con%en%s o) %$e Mule con)i#ura%ion )ile, immedia%ely be)ore %$e

closin# muleL %a#:

... <!-- Using the inheritance mechanism available for Spring beans, we can define an abstract simple-service which defines an exception strategy. This simple-service can then be inherited by concrete simple-service instances, which will all have the same exception strategy. --> <pattern:simple-service name="exceptionStrategyService" abstract="true"> <default-exception-strategy> <!-- The custom behaviour of the exception strategy consists of producing an extra line of log output. --> <logger level="ERROR" message="An exception occurred!"/>

</default-exception-strategy> </pattern:simple-service>

<!-- This simple-service inherits from the above simple-service, adding properties necessary for a concrete service. --> <pattern:simple-service name="MySimpleRegularService" parent="exceptionStrategyService" address="http://localhost:8182/Services/Regular/" transformer-refs="firstAppender" responseTransformer-refs="secondAppender"> <!-- This test component will transform the message to its string representation and append a string to the message. Mule expressions may be used in the string to be appended. --> <test:component appendString="( processed by test component at #[function:systime] )"> </test:component> </pattern:simple-service>...

9o%e %$a%:• ($e simple service named Fexcep%ion %ra%e#y erviceG is abs%rac%

($e abstract a%%ribu%e o) %$e simple&serviceL elemen% $as %$e value %rue($is mec$anism is implemen%ed by %$e prin# )rame@or/ and is re)erred %o as F>ean.e)ini%ion In$eri%anceG *lease consul% %$e prin# )rame@or/ documen%a%ion )or addi%ionalde%ails

232

Page 233: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 233/354

Page 234: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 234/354

Page 235: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 235/354

7-5=#S Simple Ser$ice

In %$is nex% par% o) %$e simple service example, @e @ill expose %$e #ree%in# service implemen%edearlier as a ?A & 1A* @eb service($e di))erences be%@een exposin# a ?A & and a ?A &R @eb service usin# %$e Mule simpleservice pa%%ern are:

• ($e t$pe a%%ribu%e in %$e simple&serviceL elemen% @ill $ave %$e value F4ax&@sGRecall %$a% %$et$pe a%%ribu%e $ad %$e value F4ax&rsG in our earlier example

• ($e componen% implemen%a%ion class mus% be anno%a%ed @i%$ a% leas% %$e Y eb ervice?A & anno%a%ion($e componen% implemen%a%ion class crea%ed )or %$e?A &R simple service example isalready anno%a%ed @i%$ %$e Y eb ervice anno%a%ion, so @e can use i% @i%$ou% c$an#es )or%$is example %oo

(o/if< the (ule Configuration )ile

i%$ %$e componen% implemen%a%ion class in place, @e are ready %o add ye% ano%$er simple servicecon)i#ura%ion %o our Mule con)i#ura%ion )ile

• Add %$e )ollo@in# %o %$e Fmule&simpleservices&con)i# xmlG )ile, immedia%ely be)ore %$eclosin# muleL %a#:

... <!-- This simple-service exposes a SOAP web service endpoint using JAX-WS. Transformers does not affect SOAP messages. It will, however, garble the WSDL by appending a text string to it, rendering it invalid. --> <pattern:simple-service name="MySimpleSOAPService" parent="exceptionStrategyService" address="http://localhost:8182/Services/SOAP" transformer-refs="firstAppender" responseTransformer-refs="secondAppender" type="jax-ws"> <component class="com.ivan.services.HelloService"/> </pattern:simple-service>...

9o%e %$a%:• ($e as%u%e reader may no%ice %$a% %$e commen%s in %$e above M- )ra#men% s%a%e %$a% %$e

%rans)ormers con)i#ured on %$e simple service does no% a))ec% 1A* messa#es #oin# in andou% o) %$e service, bu% cause %$e .- %o become invalid 9ever%$eless, %$ese are included in order )or us %o see %$e conse0uences )irs%&$and

• ($e t$pe a%%ribu%e in %$e simple&serviceL elemen% $as %$e value F4ax&@sG($is is accordin# %o %$e in%roduc%ory no%e o) %$is par% o) %$e simple service example

238

Page 236: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 236/354

+un the 7-5=#S Simple Ser$ice

e can no@ %ry %$e ?A & simple service• Ri#$%&clic/ %$e Fmule&simpleservices&con)i# xmlG Mule con)i#ura%ion )ile and selec% Run As

&L Mule erver• In a @eb bro@ser, en%er %$e BR-$%%p:==local$os%:;!;2= ervices= 1A* @sdl

($e resul% s$ould be an error %$a% is conveyed di))eren%ly, dependin# on %$e bro@ser you useIn 6ire)ox, i% loo/s li/e %$is:

Error messa#e %ryin# %o access %$e .- o) %$e ?A & simple service in 6ire)ox

9o%e %$a%:• ($e s%rin# a)%er %$e @sdl:de)ini%ionsL elemen% end %a# is %$e messa#e )rom %$e second

%rans)ormer con)i#ured on %$e simple service

In order %o avoid corrup%in# %$e .-, @e remove %$e response %rans)ormer on %$e simple service($e resul%in# simple&serviceL elemen% loo/s li/e %$is:... <pattern:simple-service name="MySimpleSOAPService" parent="exceptionStrategyService" address="http://localhost:8182/Services/SOAP" transformer-refs="firstAppender" type="jax-ws"> <component class="com.ivan.services.HelloService"/> </pattern:simple-service>...

ave %$e modi)ied Mule con)i#ura%ion )ile, s%op %$e Mule server and s%ar% i% a#ain ($is %ime %$e.- s$ould appear in %$e bro@ser

I) @e send a re0ues% %o %$e service usin#, )or ins%ance, soapBI, @e @ill be able %o see %$a% %$e%rans)ormer @e $ave con)i#ured on %$e simple service does no% a))ec% %$e messa#e

237

Page 237: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 237/354

Page 238: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 238/354

</ivan:person>

($e con%en%s o) %$is )ile is %o serve as %$e payload da%a @$en a re0ues% is sen% %o %$e ?A > simpleservice usin#, )or ins%ance, soapBI

Create the Component %mplementation Class

I) @e are %o use ?A > %o unmars$al M- da%a received @i%$ re0ues%s, a couple o) Mule&speci)icanno%a%ions are re0uired e @ill also use %$e #enera%ed Person class

• In %$e pac/a#e com ivan services, crea%e %$e ;A!" onsumerService class @i%$ %$e )ollo@in#implemen%a%ion:

package com.ivan.services;

import java.util.Map;import org.mule.api.annotations.param.InboundHeaders;import org.mule.api.annotations.param.Payload;import com.ivan.jaxb_generated.Person;

/** * Service that consumes XML data serialized to JAXB beans. * The JAXB generated classes need to be on the classpath and

* any root elements must be annotated with @XmlRootElement. ** @author Ivan A Krizsan

*/public class JAXBConsumerService{ public String greet(@Payload final Person inPerson, @InboundHeaders("*") Map inRequestHeaders) { String theGreeting = "Hello, ";

theGreeting += inPerson.getFirstName(); theGreeting += "!";

System.out.println("*** JAXBConsumerService: " + inRequestHeaders); System.out.println("*** JAXBConsumerService: " + inPerson.getFirstName() + " " + inPerson.getLastName());

return theGreeting; }}

9o%e %$a%:• ($e inPerson parame%er o) %$e greet me%$od is o) %$e %ype Person

Person is one o) %$e classes #enera%ed )rom %$e M- sc$ema usin# %$e ?A > sc$emacompiler

• ($e inPerson parame%er o) %$e greet me%$od is anno%a%ed @i%$ %$e Y*ayload anno%a%ion($is anno%a%ion is a Mule&speci)ic anno%a%ion %$a% %ells Mule %o %ry %o %rans)orm re0ues%

messa#e payload %o %$e anno%a%ed %ype usin# in%ernal %rans)ormers, o) @$ic$ ?A >%rans)orm is one• ($e in#e uestHeaders parame%er o) %$e greet me%$od is anno%a%ed @i%$ %$e

YInbound<eaders anno%a%ion($is anno%a%ion is also Mule&speci)ic, %ellin# Mule %o in4ec% %$e $eaders received @i%$ %$ere0ues% messa#e in%o %$e parame%er ($e %ype o) %$e parame%er mus% be Map($e value in %$e anno%a%ion may be a comma&separa%ed lis% o) names o) $eaders %o in4ec% in%o%$e parame%er map or, as in %$e above example, F G @$ic$ @ill resul% in all $eaders bein#in4ec%ed in%o %$e parame%er mapIn %$is example, %$ein#e uestHeaders map @ill receive %$e <((* $eaders, since @e @illuse <((* %o send re0ues%s %o %$e simple service

23;

Page 239: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 239/354

(o/if< the (ule Configuration )ile

As @e @ill soon see, no%$in# special is needed in %$e Mule con)i#ura%ion )ile in connec%ion %o %$e?A > simple service e 4us% con)i#ure a simple service %o use %$e componen% implemen%a%ionclass crea%ed earlier

Add %$e )ollo@in# simple service con)i#ura%ion %o %$e Fmule&simpleservices&con)i# xmlG)ile, immedia%ely be)ore %$e closin# muleL %a#:... <!-- This simple-service uses JAXB to unmarshal the XML data being the payload incoming messages. --> <pattern:simple-service name="MySimpleJAXBService" address="http://localhost:8182/Services/JAXB"> <component class="com.ivan.services.JAXBConsumerService"/> </pattern:simple-service>...

9o%e %$a%:• ($ere is no%$in# special abou% %$is simple service, excep% )or names, %$a% could lead us %o

believe %$a% i% uses ?A > %o unmars$al re0ues% payload da%a

+un the 7-5 Simple Ser$ice

e can no@ s%ar% %$e ?A > simple service• Ri#$%&clic/ %$e Fmule&simpleservices&con)i# xmlG Mule con)i#ura%ion )ile and selec% Run As

&L Mule erver

In order %o be able %o send a re0ues% %o %$e ?A > simple service, I $ave c$osen %o use soapBI Any%ool %$a% is able %o send a <((* *1 ( re0ues% @i%$ a M- payload @ill do 4us% )ine

($e )ollo@in# s%eps describe $o@ %o send a re0ues% )rom soapBI %o %$e ?A > simple service:• %ar% soapBI• In soapBI, selec% 9e@ soapBI *ro4ec% )rom %$e 6ile Menu• In %$e 9e@ soapBI *ro4ec% dialo#, en%er any name )or %$e pro4ec%• In %$e 9e@ soapBI *ro4ec% dialo#, c$ec/ %$e c$ec/box Add RE ( ervice H1pens dialo# %o

crea%e RE ( service•

"lic/ %$e 1K bu%%on• In %$e 9e@ RE ( ervice dialo# %$a% appears, en%er %$e BR-$%%p:==local$os%:;!;2= ervices

in %$e ervice Endpoin% inpu% )ield, ensure %$a% %$e "rea%e Resource H1pens dialo# %o crea%ea RE ( resource c$ec/box is c$ec/ed and clic/ %$e 1K bu%%on

• In %$e 9e@ RE ( Resource dialo# %$a% appears, en%er F?A >G in %$e Resource 9ame andResource *a%$=Endpoin% inpu% )ields and clic/ %$e 1K bu%%on

• In %$e 9e@ RE ( Me%$od dialo# %$a% appears ensure %$a% %$e <((* Me%$od *1 ( isselec%ed and clic/ %$e 1K bu%%on

• *as%e %$e con%en%s o) %$e person xml example da%a )ile in %$e lo@er le)% %ex% box in %$ere0ues% @indo@ %$a% is opened

23+

Page 240: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 240/354

Page 241: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 241/354

0'ath Si&ple Service

($e )inal simple service example s$o@s $o@ one or more *a%$ expressions can be used %o ex%rac%da%a )rom a M- payload in a re0ues% ($e example also s$o@s $o@ %o use a #lobal componen%, a

prin# bean, as %$e componen% o) a simple service

9o%e %$a% %$e use o) a #lobal namespace mana#er in %$is example re0uire %$e use o) Mule 3 2 orla%er

Create the Component %mplementation Class

($e *a%$ simple service also re0uire a cus%om componen% implemen%a%ion class since, as @i%$ %$e?A > simple service, @e @ill ma/e use o) cus%om anno%a%ions %o inser% %$e *a%$ expressions %ouse in %$e ?ava code

• In %$e pac/a#e com.ivan.services , crea%e %$e !Path onsumerService class @i%$ %$e)ollo@in# implemen%a%ion:

package com.ivan.services;

import org.mule.api.annotations.expression.XPath;

/** * Service that consumes XML data using XPath expressions to * extract data from a request. *

* @author Ivan A Krizsan */public class XPathConsumerService{ public String greet( @XPath(value="/ivan:person/firstName") final String inFirstName, @XPath(value="/ivan:person/lastName") final String inLastName) { String theGreeting = "Hello, " + inFirstName + "!";

System.out.println("*** XPathConsumerService: " + inFirstName + " " + inLastName);

return theGreeting; }}

9o%e %$a%:• >o%$ %$e parame%ers o) %$e greet me%$od are anno%a%ed @i%$ %$e Y *a%$ anno%a%ion

($is anno%a%ion is a Mule&speci)ic anno%a%ion %$a% enables *a%$ expressions %o be execu%edon an M- payload and %$e resul% %o be in4ec%ed in%o %$e anno%a%ed variable

• >o%$ %$e Y *a%$ anno%a%ions con%ain a value elemen% %$a% con%ains an *a%$ expression($e )irs% *a%$ expression re%rieves %$e con%en%s o) %$e )irs%9ameL elemen% in %$e

personL elemen% and %$e second *a%$ expression re%rieves %$e con%en%s o) %$e las%9ameLelemen%, also in %$e personL elemen%

25!

Page 242: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 242/354

(o/if< the (ule Configuration )ile

As @i%$ %$e ?A > simple service, %$ere is no%$in# in %$e Mule con)i#ura%ion )ile %$a% indica%es %$a%@e use *a%$ %o ex%rac% da%a )rom %$e M- da%a o) incomin# messa#esIn addi%ion %o %$e simple service con)i#ura%ion, %$ere is also %$e de)ini%ion o) a #lobal componen%,@$ic$ is a plain prin# bean, and a #lobal namespace mana#er($e use o) %$is /ind o) namespace mana#er re0uire Mule 3 2 or la%er

• Add %$e )ollo@in# simple service con)i#ura%ion %o %$e Fmule&simpleservices&con)i# xmlG)ile, immedia%ely be)ore %$e closin# muleL %a#:

... <!-- Global namespace manager. In this example, the namespace manager is needed to define the namespace prefix used in XPath expressions in the simple service that has method parameters annotated with the @XPath annotation. --> <mule-xml:namespace-manager includeConfigNamespaces="false"> <mule-xml:namespace

prefix="ivan" uri="http://www.ivan.com/schemas"/> </mule-xml:namespace-manager>

<!-- Global component. --> <spring:bean

name="xpathConsumer" class="com.ivan.services.XPathConsumerService"/>

<!-- XPath simple service. --> <pattern:simple-service name="MySimpleXPathService" address="http://localhost:8182/Services/XPath" component-ref="xpathConsumer"> </pattern:simple-service>...

9o%e %$a%:• ($ere is no%$in# special abou% %$is simple service, excep% )or names, %$a% could lead us %o

believe %$a% i% uses *a%$ %o ex%rac% in)orma%ion )rom %$e re0ues% payload da%a• ($ere is a mule&xml:namespace&mana#erL elemen%

As @e sa@ in c$ap%er 7, %$is elemen% is used %o declare a #lobal namespace mana#er In %$isexample, %$e namespace mana#er associa%es %$e namespace pre)ix FivanG @i%$ %$e M-namespace F $%%p:==@@@ ivan com=sc$emasG

• ($ere is a sprin#:beanL elemen%($is elemen% con%ains a prin# bean de)ini%ion ($is is %$e #lobal componen% %$a% @e @illuse as %$e componen% o) %$e *a%$ simple service

• 6inally, %$ere is a simple service de)ini%ion($e simple service de)ini%ion is similar %o earlier simple service examples, excep% )or %$e)ac% %$a% i% con%ains acomponent-ref a%%ribu%e($is a%%ribu%e is used %o re)er %o %$e prin# bean %$a% is %o serve as %$e componen% o) %$esimple service

252

Page 243: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 243/354

+un the 5Path Simple Ser$ice

($e *a%$ simple service example is s%ar%ed in %$e same manner as %$e earlier simple serviceexamples:

• Ri#$%&clic/ %$e Fmule&simpleservices&con)i# xmlG Mule con)i#ura%ion )ile and selec% Run As

&L Mule erver(o use soapBI %o send a re0ues% @i%$ an M- payload %o %$e *a%$ simple service, use %$e s%epsdescribed in %$e ?A > imple ervice example earlier , bu% replace %$e F?A >G in %$e Resource

9ame and Resource *a%$=Endpoin% @i%$ F *a%$G in %$e 9e@ RE ( Resource dialo#

253

Page 244: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 244/354

1& ' The 3ali/ator Pattern($e valida%or pa%%ern is very use)ul @$en developin# a service proxy or )acade %$a% only allo@smessa#es mee%in# cer%ain cri%eria %o pass %$rou#$An example is valida%ion o) messa#es received by a service usin# M- over <((* Hno% 1A* Avalida%or can ensure %$a% %$e messa#es %$a% are allo@ed %o pass )ur%$er in%o %$e sys%em )or processin#can be valida%ed a#ains% some M- sc$ema

A valida%or receives messa#es sync$ronously and @ill, dependin# on %$e messa#e, respond @i%$ a posi%ive or ne#a%ive ac/no@led#e messa#e %o %$e clien% Messa#es mee%in# %$e )il%erin# cri%eria o)%$e valida%or are passed on async$ronously ($is enables %$e clien% %o be in)ormed o) @$e%$er %$ere0ues% passed valida%ion and %$e underlyin# processin# lo#ic %o $andle %$e re0ues% a% some la%er%ime

($e example in %$is sec%ion @ill s$o@case %$e di))eren% op%ions o) %$e valida%or pa%%ern in anexample %$a% receives a <((* re0ues% @i%$ a number o) parame%ers, only accep%in# re0ues%s %$a%con%ain a cer%ain parame%er @i%$ a cer%ain value

As a prepara%ion )or %$e valida%or examples, crea%e a Mule con)i#ura%ion )ile named Fmule&valida%or&con)i# xmlG in %$ecom.ivan.muleconfig pac/a#e @i%$ %$e )ollo@in# con%en%s:

<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:pattern="http://www.mulesoft.org/schema/mule/pattern" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:test="http://www.mulesoft.org/schema/mule/test" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.1/mule.xsd

http://www.mulesoft.org/schema/mule/pattern http://www.mulesoft.org/schema/mule/pattern/3.2/mule-pattern.xsd

http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/3.2/mule-http.xsd

http://www.mulesoft.org/schema/mule/test http://www.mulesoft.org/schema/mule/test/3.2/mule-test.xsd">

<!-- Endpoint receiving requests that are to be validated. This particular endpoint receives HTTP requests and extracts HTTP request parameters from the URL to a map. Note that this endpoint uses the request-response message exchange pattern, in order to be able to deliver a ACK or NACK as the result of the request. The endpoint is a global endpoint. --> <http:endpoint

name="httpReceiver1" address="http://localhost:8080/validator1" exchange-pattern="request-response"> <!-- The body-to-parameter-map transformer transforms a HTTP request message to a map containing all the request parameters and their values. --> <http:body-to-parameter-map-transformer/> </http:endpoint>

<!-- Global filter that can be used by validators to determine whether a message will be accepted by the validator or not. An OGNL expression tests for the presence of the key-value pair with the key "data" and the value "123" in the map being the message payload. -->

255

Page 245: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 245/354

<expression-filter name="globalData123Filter" evaluator="ognl" expression="data=='123'"/>

<!-- This flow contains the endpoint that is to receive messages that have passed validation. -->

<flow name="goodMessageFlow"> <inbound-endpoint address="vm://goodMessageService" exchange-pattern="one-way"/> <test:component logMessageDetails="true"/> </flow>

</mule>

9o%e %$a%:• ($e Mule con)i#ura%ion con%ains a #lobal <((* endpoin%

($is is %$e )irs% o) %$e %@o <((* endpoin%s %$a% are %o receive re0ues%s ($e endpoin% addresscan be )ound in %$e address a%%ribu%e o) %$e $%%p:endpoin%L elemen% ($e exc$an#e pa%%ernis re0ues%&response, as speci)ied by %$eexchange-pattern a%%ribu%e o) %$e $%%p:endpoin%Lelemen%

• ($e #lobal <((* endpoin% con%ains a %rans)ormer($e body&%o&parame%er&map&%rans)ormer %rans)orms <((* re0ues%s %o a map %$a% con%ains%$e parame%ers )rom %$e <((* BR- and %$eir correspondin# values

• ($e Mule con)i#ura%ion con%ains a #lobal )il%er declara%ion($e expression&)il%er, named F#lobal.a%a!236il%erG, applies an 1C9- expression %o %$emessa#e payload and accep%s messa#es @$ic$ cause %$e resul% o) %$e evalua%ion %o be %rue

• ($e Mule con)i#ura%ion con%ains a )lo@ named F#oodMessa#e6lo@G($is )lo@ exposes an inbound&endpoin% %$a% @ill receive messa#es %$a% $ave passedvalida%ion Incomin# messa#es are lo##ed usin# %$e %es% componen% 6or de%ails on %$e %es%componen%, please consul% %$e re)erence sec%ion on(es%in#

>o%$ %$e valida%or examples in %$is sec%ion re0uire only Mule con)i#ura%ion and no addi%ionalresources

258

Page 246: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 246/354

First alidator Exa&ple

($e )irs% valida%or example @ill s$o@ basic valida%or con)i#ura%ion, usin# mos%ly a%%ribu%es o) %$epa%%ern:valida%orL elemen% ($e )il%er %$a% de%ermines @$e%$er %$e valida%or @ill accep% a messa#e

or no% @ill, $o@ever, be con)i#ured usin# a c$ild elemen% o) %$e pa%%ern:valida%orL elemen%, since@e @ill use an a%%ribu%e %o re)er %o a #lobal )il%er in %$e second example

(o/if< the (ule Configuration )ile

($e )irs% valida%or example consis%s o) a sin#le valida%or de)ini%ion in %$e Mule con)i#ura%ion )ile:• Add %$e )ollo@in# %o %$e Fmule&valida%or&con)i# xmlG )ile, immedia%ely be)ore %$e closin#

muleL %a#:... <!-- A validator receives synchronous requests and apply a filter to the request message payloads. If a message matches the filter, then it is forwarded in an asynchronous manner to the outbound address/endpoint of the

validator and an acknowledge message is returned to the client. If a message does not match the filter, a negative acknowledge message will be returned to the client and the message will not be forwarded. --> <pattern:validator name="receiverValidator" inboundEndpoint-ref="httpReceiver1" ackExpression="#[string:ACK good message: #[message:payload]]" nackExpression="#[string:NACK bad message: #[message:payload]]" outboundAddress="vm://goodMessageService"> <!-- Validator-local filter that determines whether a message will be accepted by the validator or not. Note that, due to a bug in Mule, a filter declared using a child element cannot be used in combination with an outbound endpoint declared as a child element. --> <expression-filter evaluator="ognl" expression="data=='123'"/> </pattern:validator>...

9o%e %$a%:• ($e pa%%ern:valida%orL elemen% con%ains aninbound&ndpoint-ref a%%ribu%e

Bsin# %$is a%%ribu%e, @e speci)y %$e inbound endpoin% o) %$e valida%or %o be %$e #lobal <((*endpoin% de)ined earlier in %$e Mule con)i#ura%ion )ile ($is is %$e endpoin% over @$ic$ %$isvalida%or receives incomin# messa#esAl%erna%ives %o %$einbound&ndpoint-ref a%%ribu%e are %$einboundAddress a%%ribu%e and an

inbound&endpoin%L c$ild elemen% ($e )ormer can be used @$en a simple endpoin% addressis su))icien%, @$ile %$e la%%er also allo@s )or more de%ailed con)i#ura%ion o) %$e valida%or'sinbound endpoin%

• ($e pa%%ern:valida%orL elemen% con%ains anac &xpression and a nac &xpression a%%ribu%eBsin# %$ese a%%ribu%es, @e speci)y %$e expressions %$a% crea%es posi%ive and ne#a%iveac/no@led#e messa#es %$a% are re%urned %o a clien% dependin# on @$e%$er a re0ues% isaccep%ed by %$e valida%or or no%

• ($e pa%%ern:valida%orL elemen% con%ains anoutboundAddress a%%ribu%eBsin# %$is a%%ribu%e, %$e endpoin% %o @$ic$ messa#es %$a% are accep%ed by %$e valida%or aresen% may be speci)ied ($e endpoin% @$ic$ address is #iven in %$is a%%ribu%e mus% use %$e

one&@ay messa#e exc$an#e pa%%ern

257

Page 247: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 247/354

• ($e pa%%ern:valida%orL elemen% con%ains an expression&)il%erL c$ild elemen%($is )il%er is @$a% de%ermines @$e%$er a messa#e received by %$e valida%or @ill be accep%ed,and )or@arded, or re4ec%ed and discardedIn %$is example @e use an 1C9- expression %$a% c$ec/s @$e%$er a proper%y o) %$e messa#e@i%$ %$e name Fda%aG $as %$e value !23

+un the )irst 3ali/ator Examplei%$ %$e above Mule con)i#ura%ion in place, @e can no@ %es% %$e )irs% valida%or example

• Ri#$%&clic/ %$e Fmule&valida%or&con)i# xmlG Mule con)i#ura%ion )ile and selec% Run As &LMule erver

• In a local @eb bro@ser, en%er %$e )ollo@in# BR-: $%%p:==local$os%:; ; =valida%or! da%aWIn %$e bro@ser, you s$ould see %$e )ollo@in# response:NACK bad message: {data=0}

9o addi%ional console ou%pu% s$ould $ave been @ri%%en• 9o@ en%er %$e )ollo@in# BR- in %$e bro@ser:$%%p:==local$os%:; ; =valida%or! da%aW!23

In %$e bro@ser, %$e )ollo@in# s$ould appear:ACK good message: {data=123}In %$e console, ou%pu% similar %o %$e )ollo@in# s$ould $ave appeared HMB-EP E I19

proper%y da%a s$or%ened :

INFO 2012-03-22 06:47:24,057 [goodMessageFlow.stage1.03]org.mule.tck.functional.FunctionalTestComponent:********************************************************************************* Message Received in service: goodMessageFlow. Content is: {data=123} *********************************************************************************INFO 2012-03-22 06:47:24,058 [goodMessageFlow.stage1.03]org.mule.tck.functional.FunctionalTestComponent: Full Message payload:{data=123}

Message properties: INVOCATION scoped properties: INBOUND scoped properties: MULE_ENCODING=UTF-8 MULE_ENDPOINT=vm://goodMessageService MULE_ORIGINATING_ENDPOINT=endpoint.vm.goodMessageService MULE_SESSION=rO0AB... OUTBOUND scoped properties: MULE_CORRELATION_GROUP_SIZE=-1 MULE_CORRELATION_SEQUENCE=-1 MULE_ENCODING=UTF-8 SESSION scoped properties:

9o%e %$a%:•

e received %$e posi%ive ac/no@led#e messa#e only @$en %$e parame%er Fda%aG, @i%$ %$evalue !23, @as appended %o %$e BR-• ($e messa#e @as only lo##ed @$en %$e parame%er Fda%aG, @i%$ %$e value !23, @as appended

%o %$e BR-($is means %$a% only messa#es con%ainin# %$is en%ry in %$e map $oldin# %$e messa#e @ere)or@arded %o %$e #oodMessa#e6lo@

25

Page 248: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 248/354

Second alidator Exa&ple

($e second valida%or example @ill s$o@ valida%or in$eri%ance, similar %o @$a% $as been s$o@n )or%$e previous con)i#ura%ion pa%%erns and %$e possibili%y %o con)i#ure an excep%ion s%ra%e#y )or avalida%or e @ill also see $o@ an inbound and ou%bound endpoin% o) a valida%or can be con)i#uredas c$ild elemen%s o) %$e pa%%ern:valida%orL elemen%($eore%ically, i% s$ould be possible %o con)i#ure bo%$ %$e inbound and ou%bound endpoin%s as @ell as%$e )il%er o) a valida%or as c$ild elemen%s o) %$e pa%%ern:valida%orL elemen% Re#re%)ully, a bu# inMule 3 2 ! preven%s %$is ($is is %$e reason )or con)i#urin# %$e )il%er as a c$ild elemen% in %$e

previous valida%or example and usin# an a%%ribu%e %o re)er %o a #lobal )il%er in %$is example

(o/if< the (ule Configuration )ile

($e second valida%or example consis%s o) %@o valida%or de)ini%ions in %$e Mule con)i#ura%ion )ileJ%$e )irs% bein# an abs%rac% valida%or and %$e second bein# a concre%e valida%or %$a% in$eri%s )rom %$e)irs% valida%or:

Add %$e )ollo@in# %o %$e Fmule&valida%or&con)i# xmlG )ile, immedia%ely be)ore %$e closin#muleL %a#:

... <!-- Validators can be abstract, specifying properties that can be inherited by multiple child validators. --> <pattern:validator name="exceptionStrategyValidator" abstract="true"> <!-- An exception strategy may be declared in the validator, handling exceptions occurring while messages are passing through the validator. --> <default-exception-strategy> <logger level="ERROR" message="An exception occurred!"/>

</default-exception-strategy> </pattern:validator>

<!-- This validator shows the following features of validators: - This validator inherits from an abstract validator and, in this particular case, inherits an exception strategy. - The inbound endpoint of a validator can be specified using a child element of the <validator> element. - The outbound endpoint of a validator can be specified using a child element of the <validator>. --> <pattern:validator name="receiverValidator2" parent="exceptionStrategyValidator" validationFilter-ref="globalData123Filter" ackExpression="#[string:ACK good message: #[message:payload]]" nackExpression="#[string:NACK bad message: #[message:payload]]"> <!-- Validator-local declaration of the endpoint on which the validator receives messages. --> <http:inbound-endpoint host="localhost" port="8080" path="validator2" exchange-pattern="request-response"> <http:body-to-parameter-map-transformer/> </http:inbound-endpoint> <!-- The outbound endpoint on which the validator forwards accepted messages can also be declared as a child element. Note that, due to a bug in Mule, an outbound endpoint declared using a child element cannot be used in combination

with a filter declared as a child element. -->

25;

Page 249: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 249/354

<outbound-endpoint address="vm://goodMessageService"/> </pattern:validator>...

9o%e %$a%:• ($e )irs% valida%or, named Fexcep%ion %ra%e#yValida%orG, is abs%rac%

As @i%$ earlier con)i#ura%ion pa%%ern examples, %$is uses %$e prin# bean in$eri%ancemec$anism, @$ic$ allo@s us %o de)ine an abs%rac% valida%or %$a% speci)ies common proper%iesin$eri%ed by one or more valida%ors in$eri%in# )rom %$e abs%rac% valida%or

• ($e abs%rac% valida%or con%ains a de)aul%&excep%ion&s%ra%e#yL elemen%An excep%ion s%ra%e#y can be con)i#ured )or a valida%or, speci)yin# $o@ excep%ions %$a%occur durin# messa#e processin# in %$e valida%or are $andled

• ($e second valida%or, named FreceiverValida%or2G, con%ains %$e parent a%%ribu%e($is a%%ribu%e is used %o speci)y any paren% valida%or )rom @$ic$ %o in$eri% proper%ies In %$isexample, %$e second valida%or in$eri%s %$e excep%ion s%ra%e#y speci)ied in %$e )irs% valida%or

($e second valida%or con%ains %$evalidation,ilter-ref a%%ribu%e($is a%%ribu%e is used %o re)er %o a #lobal )il%er de)ini%ion, @$ic$ @ill de%ermine @$e%$er amessa#e received by %$e valida%or @ill be accep%ed, and )or@arded, or re4ec%ed anddiscarded

• As seen in %$e )irs% valida%or example, %$e second valida%or's pa%%ern:valida%orL elemen%con%ain anac &xpression and a nac &xpression a%%ribu%eBsin# %$ese a%%ribu%es, @e speci)y %$e expressions %$a% crea%es posi%ive and ne#a%iveac/no@led#e messa#es %$a% are re%urned %o a clien% dependin# on @$e%$er a re0ues% isaccep%ed by %$e valida%or or no%

• ($e second valida%or's pa%%ern:valida%orL elemen% con%ains a $%%p:inbound&endpoin%L

elemen%($is s$o@s an al%erna%ive @ay %o declare %$e inbound endpoin% o) %$e valida%or, @$ic$ #ives%$e oppor%uni%y %o con)i#ure an endpoin% %$a% is speci)ic )or %$e valida%or in a more de%ailed@ay

• ($e $%%p:inbound&endpoin%L elemen% con%ains a $%%p:body&%o&parame%er&map&%rans)ormer=L elemen%($e body&%o&parame%er&map&%rans)ormer %rans)orms <((* re0ues%s %o a map %$a% con%ains%$e parame%ers )rom %$e <((* BR- and %$eir correspondin# values

• ($e second valida%or's pa%%ern:valida%orL elemen% con%ains an ou%bound&endpoin%Lelemen%($is enable us %o con)i#ure a valida%or&speci)ic ou%bound endpoin% and, )or ins%ance, applyone or more %rans)orma%ions %o %$e messa#es %$a% are sen% ou% )rom %$e valida%or

25+

Page 250: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 250/354

+un the Secon/ 3ali/ator Example

i%$ %$e above Mule con)i#ura%ion in place, @e can no@ %es% %$e second valida%or example• Ri#$%&clic/ %$e Fmule&valida%or&con)i# xmlG Mule con)i#ura%ion )ile and selec% Run As &L

Mule erver• In a local @eb bro@ser, en%er %$e )ollo@in# BR-: $%%p:==local$os%:; ; =valida%or2 da%aW2

In %$e bro@ser, you s$ould see %$e )ollo@in# response:NACK bad message: {data=2}

9o addi%ional console ou%pu% s$ould $ave been @ri%%en• 9o@ en%er %$e )ollo@in# BR- in %$e bro@ser:$%%p:==local$os%:; ; =valida%or2 da%aW!23

In %$e bro@ser, %$e )ollo@in# s$ould appear:ACK good message: {data=123}In %$e console, ou%pu% similar %o %$e )ollo@in# s$ould $ave appeared HMB-EP E I19

proper%y da%a s$or%ened :INFO 2012-03-22 16:49:09,584 [connector.http.mule.default.receiver.03]org.mule.lifecycle.AbstractLifecycleManager: Initialising:'connector.VM.mule.default.dispatcher.761170597'. Object is: VMMessageDispatcherINFO 2012-03-22 16:49:09,584 [connector.http.mule.default.receiver.03]org.mule.lifecycle.AbstractLifecycleManager: Starting:'connector.VM.mule.default.dispatcher.761170597'. Object is: VMMessageDispatcherINFO 2012-03-22 16:49:09,611 [goodMessageFlow.stage1.02]org.mule.tck.functional.FunctionalTestComponent:********************************************************************************* Message Received in service: goodMessageFlow. Content is: {data=123} *********************************************************************************INFO 2012-03-22 16:49:09,612 [goodMessageFlow.stage1.02]org.mule.tck.functional.FunctionalTestComponent: Full Message payload:{data=123}

Message properties:

INVOCATION scoped properties: INBOUND scoped properties: MULE_ENCODING=UTF-8 MULE_ENDPOINT=vm://goodMessageService MULE_ORIGINATING_ENDPOINT=endpoint.vm.goodMessageService MULE_SESSION=rO0A... OUTBOUND scoped properties: MULE_CORRELATION_GROUP_SIZE=-1 MULE_CORRELATION_SEQUENCE=-1 MULE_ENCODING=UTF-8 SESSION scoped properties:

9o%e %$a%:• ($e second valida%or be$aves in @ay iden%ical %o %$a% o) %$e )irs% valida%or• e received %$e posi%ive ac/no@led#e messa#e only @$en %$e parame%er Fda%aG, @i%$ %$e

value !23, @as appended %o %$e BR-• ($e messa#e @as only lo##ed @$en %$e parame%er Fda%aG, @i%$ %$e value !23, @as appended

%o %$e BR-($is means %$a% only messa#es con%ainin# %$is en%ry in %$e map $oldin# %$e messa#e @ere)or@arded %o %$e #oodMessa#e6lo@

28

Page 251: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 251/354

%hird alidator Exa&ple

In %$e %$ird valida%or example, @e @ill see $o@ a valida%or can be con)i#ured %o re%urn errormessa#es %o clien% i) %$e valida%or )ails %o send a valid messa#e %o %$e ou%bound endpoin% o) %$evalida%or

(o/if< the (ule Configuration )ile($e %$ird valida%or example consis%s o) one valida%or de)ini%ion in %$e Mule con)i#ura%ion )ile

• Add %$e )ollo@in# %o %$e Fmule&valida%or&con)i# xmlG )ile, immedia%ely be)ore %$e closin#muleL %a#:

... <!-- A validator can also be configured to return an error message if there was a problem sending a valid message on the outbound endpoint. If such an error message is configured, using the errorExpression attribute, the message exchange pattern between the validator and the outbound endpoint automatically becomes request-response. --> <pattern:validator name="receiverValidator3" validationFilter-ref="globalData123Filter" ackExpression="#[string:ACK good message: #[message:payload]]" nackExpression="#[string:NACK bad message: #[message:payload]]" errorExpression="#[string:An error occurred passing the message on. Messagepayload: #[message:payload]]"> <http:inbound-endpoint host="localhost" port="8080" path="validator3" exchange-pattern="request-response"> <http:body-to-parameter-map-transformer/> </http:inbound-endpoint> <!-- A bad URL is intentionally used here to show what happens when the validator fails to send a valid message to the outbound endpoint.

--> <outbound-endpoint address="http://localhost:8123/badurl"/> </pattern:validator>...

9o%e %$a%:• ($e %$ird valida%or is named FreceiverValida%or3G• ($e valida%or con%ains %$evalidation,ilter-ref a%%ribu%e

($is a%%ribu%e is used %o re)er %o a #lobal )il%er de)ini%ion, @$ic$ @ill de%ermine @$e%$er amessa#e received by %$e valida%or @ill be accep%ed, and )or@arded, or re4ec%ed anddiscarded

• ($e valida%or con%ains anac &xpression and a nac &xpression a%%ribu%eBsin# %$ese a%%ribu%es, @e speci)y %$e expressions %$a% crea%es posi%ive and ne#a%iveac/no@led#e messa#es %$a% are re%urned %o a clien% dependin# on @$e%$er a re0ues% isaccep%ed by %$e valida%or or no%

• ($e valida%or con%ains an a%%ribu%e namederror&xpressionBsin# %$is a%%ribu%e @e speci)y %$e expressions %$a% crea%e error messa#es %$a% are re%urned %oa clien% i) %$e valida%or )ails %o send a valid messa#e on %$e ou%bound endpoin%

• ($e valida%or con%ains a $%%p:inbound&endpoin%L c$ild elemen%($is elemen% speci)ies %$e inbound endpoin% on @$ic$ %$e valida%or receives messa#es

• ($e $%%p:inbound&endpoin%L elemen% con%ains a $%%p:body&%o&parame%er&map&%rans)ormer=L elemen%

28!

Page 252: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 252/354

($e body&%o&parame%er&map&%rans)ormer %rans)orms <((* re0ues%s %o a map %$a% con%ains%$e parame%ers )rom %$e <((* BR- and %$eir correspondin# values

• ($e valida%or con%ains an ou%bound&endpoin%L elemen%As in %$e previous example, %$is is %$e ou%bound endpoin% %o @$ic$ %$e valida%or )or@ardsmessa#es %$a% $ave been accep%ed by %$e valida%or's )il%er($e ou%bound endpoin% in %$is example $as delibera%ely been con)i#ured @i%$ an invalidaddress, so as %o #enera%e errors @$en %ryin# %o )or@ard messa#es

282

Page 253: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 253/354

+un the Thir/ 3ali/ator Example

i%$ %$e above Mule con)i#ura%ion in place, @e can no@ %es% %$e %$ird valida%or example• Ri#$%&clic/ %$e Fmule&valida%or&con)i# xmlG Mule con)i#ura%ion )ile and selec% Run As &L

Mule erver• In a local @eb bro@ser, en%er %$e )ollo@in# BR-: $%%p:==local$os%:; ; =valida%or3 da%aW3

In %$e bro@ser, you s$ould see %$e )ollo@in# response:NACK bad message: {data=3}

9o addi%ional console ou%pu% s$ould $ave been @ri%%en• 9o@ en%er %$e )ollo@in# BR- in %$e bro@ser:$%%p:==local$os%:; ; =valida%or3 da%aW!23

In %$e bro@ser, %$e )ollo@in# s$ould appear:An error occurred passing the message on. Message payload: {data=123}In %$e console, ou%pu% similar %o %$e )ollo@in# s$ould $ave appeared:

ERROR 2012-03-26 16:35:27,386 [connector.http.mule.default.receiver.04]org.mule.construct.Validator$ErrorAwareEventReturningMessageProcessor:org.mule.api.transport.DispatchException: Failed to route event via endpoint:

DefaultOutboundEndpoint{endpointUri=http://localhost:8123/badurl,connector=HttpConnector{ name=connector.http.mule.default lifecycle=start this=fa5e4e4 numberOfConcurrentTransactedReceivers=4 createMultipleTransactedReceivers=true connected=true supportedProtocols=[http] serviceOverrides=<none>}, name='endpoint.http.localhost.8123.badurl', mep=REQUEST_RESPONSE, properties={},transactionConfig=Transaction{factory=null, action=INDIFFERENT, timeout=0},deleteUnacceptedMessages=false, initialState=started, responseTimeout=10000,endpointEncoding=UTF-8, disableTransportTransformer=false}. Message payload is of type:HashMap

9o%e %$a%:• ($e valida%or did no% )or@ard %$e messa#e %$a% @ere no% accep%ed by %$e valida%or's )il%er

($is be$aviour is %$e same as @e $ave seen in %$e previous examples and s$ould %$us comeas no surprise

• ($e valida%or re%urned an error messa#e @$en %ryin# %o )or@ard a messa#e accep%ed by %$evalida%or's )il%er($is is %$e expec%ed be$aviour, since @e con)i#ured %$e ou%bound endpoin% o) %$e valida%or@i%$ an ille#al address e see %$a% %$e mec$anism indeed @or/s as described in %$ein%roduc%ion o) %$is sec%ion

• An error @as lo##ed @$en %$e valida%or %ried %o )or@ard a messa#e accep%ed by %$evalida%or's )il%er($is is %$e de)aul% excep%ion s%ra%e#y o) %$e valida%or $andin# %$e excep%ion %$a% occurred@$en %$e valida%or %ried %o )or@ard %$e messa#e

283

Page 254: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 254/354

1& * The #eb Ser$ice Prox< Pattern($e )inal Mule con)i#ura%ion pa%%ern is %$e @eb service proxy pa%%ern ($is pa%%ern enables us %o0uic/ly se% up a proxy )or a @eb service ($e proxy can apply one or more %rans)orma%ions,implemen% $andlin# o) errors %$a% occur @$en invo/in# %$e @eb service be$ind %$e proxy as @ell asredirec% re0ues%s )or %$e .- o) %$e @eb service be$ind %$e proxy

($e @eb service proxy example re0uires more elabora%e prepara%ions, since @e @ill use soapBI %ose% up a 1A* @eb service %$a% @e are %o crea%e a proxy )or

Exa&ple 'reparations

In preparin# )or %$e @eb service proxy example, @e @ill:• "rea%e a s/ele%on Mule con)i#ura%ion )ile• Implemen% a cus%om %rans)ormer• e% up a .- )or %$e @eb service @e are %o proxy• "rea%e a soapBI moc/ service

Create the (ule Configuration )ile

6irs%, le%'s crea%e a Mule con)i#ura%ion )ile )or %$e @eb service proxy examples• "rea%e a Mule con)i#ura%ion )ile named Fmule&@sproxy&con)i# xmlG in %$e

com.ivan.muleconfig pac/a#e @i%$ %$e )ollo@in# con%en%s:<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mule-xml="http://www.mulesoft.org/schema/mule/xml" xmlns:pattern="http://www.mulesoft.org/schema/mule/pattern" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.1/mule.xsd

http://www.mulesoft.org/schema/mule/xmlhttp://www.mulesoft.org/schema/mule/xml/3.2/mule-xml.xsd

http://www.mulesoft.org/schema/mule/pattern

http://www.mulesoft.org/schema/mule/pattern/3.2/mule-pattern.xsd">

<!-- Transformer that unpacks zipped data. --> <gzip-uncompress-transformer name="unzipTransformer"/>

<!-- Transformer that logs messages and message payloads. --> <custom-transformer

name="loggingTransformer" class="com.ivan.transformers.MessageLoggingTransformer"/> </mule>

9o%e %$a%:• ($e Mule con)i#ura%ion )ile con%ains a %rans)ormer %$a% unpac/s zipped da%a

$en @or/in# @i%$ %$is example, I no%iced %$a% soapBI compresses response da%a ($is%rans)ormer unpac/s suc$ da%a as %o ma/e i% $uman&readable

• ($e Mule con)i#ura%ion )ile con%ains a cus%om %rans)ormere @ill s$or%ly implemen% %$is %rans)ormer and see %$a% i% indeed does @$a% %$e commen%s

sayJ lo#s Mule messa#es and messa#e payloads

285

Page 255: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 255/354

%mplement a Custom Logging Transformer

($e cus%om %rans)ormer in %$is example is used %o lo# Mule messa#es and messa#e payloads %o %$econsole @i%$ou% al%erin# %$em ($e reason )or implemen%in# suc$ a %rans)ormer is %$a% %$e @ebservice proxy allo@s )or inser%ion o) one or more %rans)ormers bo%$ on ou%bound and inboundmessa#es Bsin# a lo##in# %rans)ormer, @e can very convenien%ly add lo##in# @$erever @e @an% %o

• In %$e Eclipse pro4ec%, crea%e %$e source pac/a#ecom.ivan.transformers• In %$e pac/a#e crea%ed above, implemen% a cus%om %rans)ormer in a class named

MessageLogging'ransformer as %$is:package com.ivan.transformers;

import org.mule.api.MuleMessage;import org.mule.api.transformer.TransformerException;import org.mule.transformer.AbstractMessageTransformer;

/** * Mule 3.x transformer that logs message that passes through * the transformer to the console. Messages are not altered in any way. *

* @author Ivan Krizsan */public class MessageLoggingTransformer extends AbstractMessageTransformer{ @Override public Object transformMessage(final MuleMessage inMessage, final String inOutputEncoding) throws TransformerException { try { System.out.println("***** Message: " + inMessage.toString()); System.out.println("***** Message payload: " +

inMessage.getPayloadAsString()); } catch (Exception theException) { theException.printStackTrace(); } return inMessage; }}

9o%e %$a%:• ($e MessageLogging'ransformer class ex%ends %$e AbstractMessage'ransformer

AbstractMessage'ransformer provides basic implemen%a%ion o) a %rans)ormer %$a% $asaccess %o %$e curren% messa#e *lease re)er %o %$e Mule 3 x A*I documen%a%ion )or addi%ionalde%ails

• ($e transformMessage me%$od lo#s messa#e proper%ies and %$e messa#e payload•

($e messa#e received by %$e transformMessage me%$od is re%urned by %$e me%$od($e messa#e is unal%ered

288

Page 256: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 256/354

Create the Ser$ice #SDL

($e .- crea%ed in %$is s%ep @ill serve bo%$ as a speci)ica%ion )or %$e soapBI moc/ service as@ell as a cus%om .- %$a% @e la%er @ill con)i#ure %$e @eb service proxy %o respond @i%$@$enever %$e .- o) %$e ac%ual @eb service is re0ues%ed

In %$e roo% o) %$e pro4ec%'s source $ierarc$y H%$e FsrcG )older , crea%e a )ile namedF<ello ervice @sdlG @i%$ %$e )ollo@in# con%en%s:<?xml version="1.0" encoding="UTF-8"?><wsdl:definitions targetNamespace="urn:helloWorld/sample/ivan/com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="urn:helloWorld/sample/ivan/com" xmlns:types="urn:helloWorld/types" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="HelloWorld">

<wsdl:types> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:helloWorld/types" xmlns="urn:helloWorld/types">

<xsd:element name="hello"> <xsd:complexType> <xsd:sequence> <xsd:element name="name" nillable="true" type="xsd:string"/> <xsd:element name="first_number" type="xsd:int"/> <xsd:element name="second_number" type="xsd:int"/> </xsd:sequence> </xsd:complexType> </xsd:element>

<xsd:element name="helloResponse"> <xsd:complexType> <xsd:sequence> <xsd:element name="message" nillable="true" type="xsd:string"/> <xsd:element name="sum" type="xsd:long"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </wsdl:types>

<wsdl:message name="helloRequestMsg"> <wsdl:part element="types:hello" name="helloParameters"/> </wsdl:message> <wsdl:message name="helloResponseMsg"> <wsdl:part element="types:helloResponse" name="helloResult"/> </wsdl:message>

<wsdl:portType name="HelloWorld"> <wsdl:operation name="hello"> <wsdl:input message="tns:helloRequestMsg" name="helloRequest"/> <wsdl:output message="tns:helloResponseMsg" name="helloResponse"/> </wsdl:operation>

</wsdl:portType> <wsdl:binding name="HelloWorldBinding" type="tns:HelloWorld"> <soap:binding style="document"transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="hello"> <soap:operation/> <wsdl:input name="helloRequest"> <soap:body use="literal"/> </wsdl:input> <wsdl:output name="helloResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding>

<wsdl:service name="HelloWorldService"> <wsdl:port name="port" binding="tns:HelloWorldBinding">

287

Page 257: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 257/354

<soap:address location="http://www.someserver.com"/> </wsdl:port> </wsdl:service></wsdl:definitions>

($is is an example o) a re0ues% messa#e %$a% @ill be accep%ed by a service ad$erin# %o %$e above.-:

<soapenv:Envelopexmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:typ="urn:helloWorld/types"> <soapenv:Header/> <soapenv:Body> <typ:hello> <name>Ivan</name> <first_number>2</first_number> <second_number>3</second_number> </typ:hello> </soapenv:Body></soapenv:Envelope>

and %$is is an example o) a response messa#e %$a% a service ad$erin# %o %$e above .- may use%o respond %o a re0ues%:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:typ="urn:helloWorld/types"> <soapenv:Header/> <soapenv:Body> <typ:helloResponse> <message>Hello there!</message> <sum>5</sum> </typ:helloResponse> </soapenv:Body></soapenv:Envelope>

28

Page 258: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 258/354

Create the (ock Ser$ice in soap6%

In %$is s%ep @e @ill crea%e a moc/ service in soapBI and %es% i% )rom @i%$in soapBI >e)ore proceedin#, ma/e sure you $ave do@nloaded, ins%alled and s%ar%ed soapBI

• In soapBI, #o %o %$e 6ile menu and selec% 9e@ soapBI *ro4ec%• 6ill in %$e ne@ pro4ec% dialo#

I c$ose %$e pro4ec% name F *roxy eb erviceG, @$ic$ is no% si#ni)ican%Ma/e sure %$a% %$e c$ec/boxes F"rea%e re0ues%sG and F"rea%e Moc/ erviceG are c$ec/ed6or %$e FIni%ial .-= A.-G, loca%e and selec% %$e F<ello ervice @sdlG .- @ecrea%ed earlier

"rea%in# %$e moc/ @eb service pro4ec% in soapBI

• Immedia%ely a)%er $avin# crea%ed %$e ne@ pro4ec%, a Cenera%e Moc/ ervice dialo# appearsIn %$is dialo# and %$e )ollo@in# Cenera%e Moc/ ervice dialo#, 4us% accep% %$e de)aul% valuesand clic/ 1K

• In %$e F<ello orld>indin# Moc/ erviceG @indo@ %$a% appears %o %$e ri#$%, double&clic/ %$eF$elloG opera%ion

• In %$e F$elloG @indo@ %$a% appears, double&clic/ %$e FResponse !G moc/ response• Modi)y %$e moc/ response

I c$ose %o modi)y my moc/ response li/e %$is, c$an#in# %$e con%en%s o) %$e messa#eL andsumL elemen%s:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:typ="urn:helloWorld/types"> <soapenv:Header/> <soapenv:Body> <typ:helloResponse> <message>Hello there!</message> <sum>42</sum> </typ:helloResponse> </soapenv:Body></soapenv:Envelope>

• 6ind %$e F<ello orld>indin# Moc/ erviceG @indo@ and s%ar% %$e moc/ service by clic/in#

%$e small #reen arro@ in %$e upper le)% corner o) %$e @indo@

28;

Page 259: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 259/354

($e #reen arro@ s$ould be disabled and %$ere s$ould be a messa#e sayin# %$a% %$e moc/service is runnin#

<avin# s%ar%ed %$e moc/ service in soapBI

($e moc/ service s$ould no@ be up and runnin# (o con)irm %$is, @e'll %es% i% )rom @i%$in soapBI:• In %$e pro4ec%s %ree in soapBI, )ind and double&clic/ %$e FRe0ues% !G o) %$e F$elloG opera%ion

in %$e F<ello orld>indin#G

1penin# %$e %es%&re0ues% )or %$e F$elloG opera%ion in soapBI

• Ma/e sure %$a% %$e BR- a% %$e %op o) %$e FRe0ues% !G @indo@ con%ains %$e s%rin#Fmoc/<ello orld>indin#GI) i% $asn'%, selec% %$e moc/ service's BR- )rom %$e drop&do@n lis% I% s$ould be some%$in#li/e F$%%p:==local$os%:; ;;=moc/<ello orld>indin#G, @$ere local$os% may be replaced @i%$%$e name o) your compu%er

• In %$e FRe0ues% !G @indo@ %$a% appears, clic/ %$e ri#$% %rian#le in %$e upper le)% corner o)%$e @indo@($is sends %$e %es%&re0ues% %o %$e moc/ service

• Also in %$e FRe0ues% !G @indo@, bu% on %$e ri#$%&$and side, examine %$e response o) %$ere0ues% @e 4us% issuedI% s$ould be iden%ical @i%$ %$e moc/ response @e con)i#ured earlier

• In a @eb bro@ser, issue a re0ues% )or %$e moc/ @eb service's .-($e BR- s$ould loo/ some%$in# li/e $%%p:==local$os%:; ;;=moc/<ello orld>indin# @sdlDou s$ould see a .- %$a% is almos% iden%ical %o %$e one @e crea%ed earlier

28+

Page 260: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 260/354

e $ave no@ success)ully se% up %$e moc/ @eb service and mana#ed %o send re0ues%s direc%ly %o %$emoc/ service A)%er $avin# developed %$e @eb service proxy, @e @ill redirec% re0ues%s sen% )romsoapBI %o %$e proxy, ins%ead o) sendin# %$em direc%ly %o %$e moc/ service

First Web Service 'rox# Exa&ple

i%$ %$e moc/ @eb service in place, @e are no@ ready %o develop our )irs% @eb service proxyexample I%s s%ruc%ure can be illus%ra%ed by )ollo@in# )i#ure:

6lo@ o) re0ues%s and responses in %$e )irs% @eb service proxy example

($e clien%, #reen in %$e above )i#ure, and %$e @eb service endpoin%, blue in %$e above )i#ure, are bo%$ realized usin# soapBI ($e @eb service proxy, red in %$e above )i#ure, is %o be implemen%edusin# Mule

27

Page 261: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 261/354

(o/if< the (ule Configuration )ile

($e )irs% @eb service proxy example consis%s o) a sin#le proxy de)ini%ion in %$e Mule con)i#ura%ion)ile:

• Add %$e )ollo@in# %o %$e Fmule&@sproxyx&con)i# xmlG )ile, immedia%ely be)ore %$e closin#

muleL %a#:... <!-- SOAP web service proxy that logs request and response messages that pass through the proxy. In addition, response messages are unpacked before being logged. The version of Mule 3.x used when developing this example adds the Accept-Encoding HTTP header with the value gzip, which causes the responses from soapUI to be zipped. If the response messages are garbled or if exceptions occur in the unzipTransformer, try removing this transformer from the list in the responseTransformer-refs attribute of the <pattern:web-service-proxy> element. --> <pattern:web-service-proxy name="helloWorldProxy1" transformer-refs="loggingTransformer" responseTransformer-refs="unzipTransformer loggingTransformer" inboundAddress="http://localhost:8090/helloWorld1" outboundAddress="http://localhost:8088/mockHelloWorldBinding"/>...

9o%e %$a%:• ($e @eb service proxy is con)i#ured usin# one sin#le pa%%ern:@eb&service&proxyL elemen%

in %$e Mule con)i#ura%ion )ile• ($e pa%%ern:@eb&service&proxyL elemen% con%ains atransformer-refs a%%ribu%e

($e transformer-refs a%%ribu%e is used %o con)i#ure %$e %rans)ormerHs %$a% are applied %oMule messa#es be)ore %$ey are sen% ou% )rom %$e proxy %o %$e @eb service %$a% is bein#

proxied I) more %$an one %rans)ormer is %o be applied, a space is %o be inser%ed be%@een %$enames o) %$e %rans)ormers

• ($e pa%%ern:@eb&service&proxyL elemen% con%ains aresponse'ransformer-refs a%%ribu%eimilar %o %$etransformer-refs a%%ribu%e, %$e value o) %$is a%%ribu%e speci)y one or more

%rans)ormers %$a% are %o be applied %o Mule messa#es <o@ever, %$eresponse'ransformer-refs a%%ribu%e speci)y %$e %rans)ormerHs %$a% are %o be applied %o response messa#es )rom %$e

proxied @eb service, be)ore %$ey are re%urned %o %$e clien% %$a% issued %$e correspondin#re0ues% %o %$e proxy

($e response'ransformer-refs a%%ribu%e speci)y %@o %rans)ormers($e )irs% %rans)ormer is named Funzip(rans)ormerG and %$e second is namedFlo##in#(rans)ormerG ($ese %rans)ormers are applied in %$e order in @$ic$ %$ey are lis%ed,%$a% is %$e Funzip(rans)omrerG is applied )irs% and %$en %$e Flo##in#(rans)ormerG is applied($e reason )or applyin# %$e Funzip(rans)ormerG %o response messa#es is %$a% soapBIcompresses response messa#es and %$ey mus% be decompressed be)ore bein# $uman&readable

• ($e pa%%ern:@eb&service&proxyL elemen% con%ains ainboundAddress a%%ribu%e($is a%%ribu%e speci)y %$e endpoin% address a% @$ic$ %$e @eb service proxy @ill lis%en )orre0ues%s

($e pa%%ern:@eb&service&proxyL elemen% con%ains aoutboundAddress a%%ribu%e

27!

Page 262: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 262/354

($e outboundAddress a%%ribu%e speci)ies %$e endpoin% address %o @$ic$ %$e @eb service proxy @ill )or@ard incomin# re0ues%s

+un the #eb Ser$ice Prox< Example

i%$ %$e above Mule con)i#ura%ion in place, @e can no@ %es% %$e )irs% @eb service proxy example• Ri#$%&clic/ %$e Fmule&@sproxy&con)i# xmlG Mule con)i#ura%ion )ile and selec% Run As &L

Mule erver• Ma/e sure %$a% %$e moc/ service in soapBI is s%ar%ed

6or de%ails on se%%in# up and s%ar%in# %$e moc/ service in soapBI, please re)er %o %$eExample *repara%ions earlier

• 1pen %$e FRe0ues% !G %$a% @e used %o %es% %$e moc/ service in %$eExample *repara%ions earlierIn %$e nex% )e@ s%eps @e are #oin# %o c$an#e %$e address %o @$ic$ re0ues%s are sen%, so %$a%%$ey are sen% %o %$e @eb service proxy @e 4us% crea%ed, ins%ead o) direc%ly %o %$e moc/service

• "lic/ %$e area @$ic$ con%ains %$e BR- in %$e Re0ues% ! @indo@ and selec% FUadd ne@endpoin% G

Addin# a ne@ endpoin% @$ic$ %o send a re0ues% %o in soapBI

• In %$e Add 9e@ Endpoin% dialo# %$a% appears, en%er %$e BR-$%%p:==local$os%:; + =$ello orld! and clic/ 1K

peci)yin# %$e BR- o) %$e ne@ endpoin% @$ic$ %o send re0ues%s in soapBI

272

Page 263: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 263/354

• "lic/ %$e li%%le #reen %rian#le in %$e upper&le)% corner o) %$e Re0ues% ! @indo@($is @ill send %$e re0ues% %o %$e @eb service proxy, @$ic$ in %urn @ill )or@ard %$e re0ues% %o%$e moc/ @eb serviceDou s$ould see ou%pu% similar %o %$e )ollo@in# in %$e console:

...***** Message:org.mule.DefaultMuleMessage{ id=ecafed59-8010-11e1-869b-45fdb5b2e92c payload=org.apache.commons.httpclient.ContentLengthInputStream correlationId=<not set> correlationGroup=-1 correlationSeq=-1 encoding=utf-8 exceptionPayload=<not set>

Message properties: INVOCATION scoped properties: INBOUND scoped properties: Accept-Encoding=gzip,deflate Connection=false Content-Type=text/xml; charset=utf-8 Host=localhost:8090

Keep-Alive=false MULE_ORIGINATING_ENDPOINT=endpoint.http.localhost.8090.helloWorld1 MULE_REMOTE_CLIENT_ADDRESS=/127.0.0.1:49887 User-Agent=Jakarta Commons-HttpClient/3.1 http.context.path=/helloWorld1 http.method=POST http.request=/helloWorld1 http.request.path=/helloWorld1 http.version=HTTP/1.1 OUTBOUND scoped properties: MULE_ENCODING=utf-8 SESSION scoped properties:}***** Message payload: <soapenv:Envelopexmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"xmlns:typ="urn:helloWorld/types"> <soapenv:Header/> <soapenv:Body> <typ:hello> <name>Ivan</name> <first_number>2</first_number> <second_number>3</second_number> </typ:hello> </soapenv:Body></soapenv:Envelope>INFO 2012-04-06 19:49:58,013 [connector.http.mule.default.receiver.02]org.mule.transport.service.DefaultTransportServiceDescriptor: Loading default outboundtransformer: org.mule...***** Message:org.mule.DefaultMuleMessage{ id=ed0475fd-8010-11e1-869b-45fdb5b2e92c

payload=[B correlationId=<not set> correlationGroup=-1 correlationSeq=-1 encoding=utf-8 exceptionPayload=<not set>

Message properties: INVOCATION scoped properties: INBOUND scoped properties: Connection=false Content-Encoding=gzip Content-Type=text/xml; charset=utf-8 Keep-Alive=false Server=Jetty(6.1.x) Transfer-Encoding=chunked http.method=POST http.request=http://localhost:8088/mockHelloWorldBinding

273

Page 264: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 264/354

http.status=200 http.version=HTTP/1.1 OUTBOUND scoped properties: MULE_ENCODING=utf-8 MULE_SESSION=rO0A... SESSION scoped properties:}***** Message payload: <soapenv:Envelopexmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:typ="urn:helloWorld/types"> <soapenv:Header/> <soapenv:Body> <typ:helloResponse> <message>Hello there!</message> <sum>42</sum> </typ:helloResponse> </soapenv:Body></soapenv:Envelope>

9o%e %$a%:• ($e par% o) %$e lo# $i#$li#$%ed in blue is %$e re0ues% messa#e

e can see %$a% %$e 1A* re0ues% is iden%ical %o %$a% @e $ave seen in soapBI• ($e par% o) %$e lo# $i#$li#$%ed in #reen is lo# messa#es )rom Mule ini%ializin# i%s

componen%s($is par% $as been edi%ed, %o increase readabili%y

• ($e par% o) %$e lo# $i#$li#$%ed in oran#e is %$e response messa#ee can see %$a% %$e 1A* response is iden%ical %o %$a% @e $ave con)i#ured %$e moc/ service

in soapBI %o respond @i%$• e can read %$e M- o) %$e 1A* response

I) you are up %o some experimen%a%ion, %ry removin# %$e Funzip(rans)ormerG )rom %$eresponse'ransformer-refs a%%ribu%e o) %$e pa%%ern:@eb&service&proxyL elemen% and examine

%$e response"onversely, i) you canno% read %$e response messa#e M-, %$en you s$ould %ry %o remove%$e Funzip(rans)ormerG, since i% may be %$a% your response is no% compressed

• Mule session iden%i)ier da%a $as been s$or%ened, %o avoid clu%%erin# %$e ou%pu%

275

Page 265: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 265/354

Second Web Service 'rox# Exa&ple

($e second @eb service proxy example @ill s$o@case more )ea%ures o) %$e @eb service proxy pa%%ern:

• "on)i#ura%ion o) an excep%ion s%ra%e#y in a @eb service proxy• In$eri%ance and abs%rac% @eb service proxies• In& and ou%bound @eb service proxy endpoin%s con)i#ured usin# c$ild elemen%s• - %rans)orm applied in a @eb service proxy

$ile no% speci)ic %o @eb service proxies, %$is is a #eneral example on $o@ %o apply an -%rans)orma%ion

($e s%ruc%ure o) %$e @eb service proxy is similar %o %$a% o) %$e )irs% example, @i%$ a ne@ responsemessa#e %rans)ormer added:

6lo@ o) re0ues%s and responses in %$e second @eb service proxy example

As in %$e previous example, %$e clien% and %$e @eb service endpoin%, #reen and blue respec%ively in%$e above )i#ure, are bo%$ realized usin# soapBI ($e @eb service proxy, red in %$e above )i#ure, is%o be implemen%ed usin# Mule($e clien% and @eb service endpoin% are reused )rom %$e previous example so i) you $aven'% se% upsoapBI )or %$e previous @eb service proxy example, please re)er %o %$e Example *repara%ion sec%ionearlier

e @ill also reuse %$e implemen%a%ion o) %$e lo##in# %rans)ormer %$a% also @as crea%ed as par% o) %$e prepara%ions )or %$e )irs% @eb service proxy example

278

Page 266: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 266/354

Create the Double Sum 5SL Transform

>e)ore bein# able %o develop %$e Mule con)i#ura%ion )or %$is example, @e need %o crea%e a -%rans)orm %$a% mul%iplies %$e con%en%s o) a sumL elemen% in an M- )ra#men% @i%$ %@o ($is%rans)orma%ion @ill be applied %o response messa#es )rom %$e moc/ @eb service e recall %$a% suc$a messa#e may loo/ li/e %$is:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:typ="urn:helloWorld/types"> <soapenv:Header/> <soapenv:Body> <typ:helloResponse> <message>Hello there!</message> <sum>42</sum> </typ:helloResponse> </soapenv:Body></soapenv:Envelope>

($e par% %$e - %rans)orm is %o a))ec% is $i#$li#$%ed in yello@• In %$e roo% o) %$e source&%ree in Eclipse, crea%e a )ile named Fsum.ouble(rans)orm xslG @i%$

%$e )ollo@in# con%en%:<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:typ="urn:helloWorld/types" exclude-result-prefixes="xs" version="2.0">

<!-- Template that multiplies the contents of a <sum> element with two. --> <xsl:template match="sum">

<sum><xsl:value-of select="text() * 2"/></sum> </xsl:template>

<!-- Template that copies elements and attributes, without modifications. --> <xsl:template match="node( ) | @*"> <xsl:copy> <xsl:apply-templates select="@* | node( )"/> </xsl:copy> </xsl:template></xsl:stylesheet>

$ile %eac$in# - %rans)orm developmen% is ou%side %$e scope o) %$is boo/, a )e@ @ords may be

appropria%e• ($e - %rans)orm consis%s o) %@o %empla%es• ($e )irs% %empla%e replaces a sumL elemen% and i%s con%en%s @i%$ a sumL elemen% %$a%

con%ains %$e con%en%s o) %$e ori#inal sumL elemen% mul%iplied by %@o• ($e second %empla%e copies elemen%s and a%%ribu%es @i%$ou% modi)ica%ions

277

Page 267: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 267/354

(o/if< the (ule Configuration )ile

($e second @eb service proxy example consis%s o) one ne@ %rans)ormer de)ini%ion and %@o @ebservice proxy de)ini%ions

• Add %$e )ollo@in# %o %$e Fmule&@sproxyx&con)i# xmlG )ile, immedia%ely be)ore %$e closin#

muleL %a#:... <!-- Transformer that applies the XSL transform stored in the file "sumDoubleTransform.xsl" to XML data in a message payload. --> <mule-xml:xslt-transformer name="doubleSumTransformer" xsl-file="sumDoubleTransform.xsl"/>

<!-- Abstract web service proxy that acts as a parent to one or more child web service proxies. Child web service proxies inherit the properties defined in the parent web service proxy. In this case, the exception strategy. --> <pattern:web-service-proxy name="exceptionStrategyProxy" abstract="true"> <!-- An exception strategy may be declared in a proxy, handling exceptions occurring while messages are passing through it. --> <default-exception-strategy> <logger level="ERROR" message="An exception occurred!"/> </default-exception-strategy> </pattern:web-service-proxy>

<!-- SOAP web service proxy that has the following properties: - Inherits from the above abstract web service proxy. - When there is a request for the WSDL of the proxied service,

the proxy serves up a local file containing a customized WSDL. - The proxy's inbound endpoint is declared as a child element of the <web-service-proxy> element. - The proxy's outbound endpoint is also declared as a child element of the <web-service-proxy> element. - No transformers. The logging transformers and the unzip transformer we saw in the previous example are now configured on the proxy's outbound endpoint. --> <pattern:web-service-proxy name="helloWorldProxy2" parent="exceptionStrategyProxy" wsdlFile="HelloService.wsdl"> <!-- Inbound endpoint on which the proxy receives requests. --> <inbound-endpoint address="http://localhost:8090/helloWorld2"/> <!-- Outbound endpoint to which the proxy forwards requests. The responseTimeout attribute specifies a maximum time the endpoint waits for a response. The logging transformer is "applied" to requests sent over the endpoint. The following transformers are applied to response messages from the outbound endpoint in the order listed: - A transformer that unpacks the packed (zip) payload of response messages. - A transformer that applies an XSL transform to the XML payload of response messages (doubleSumTransformer). - The logging transformer. --> <outbound-endpoint address="http://localhost:8088/mockHelloWorldBinding" responseTimeout="5000"

transformer-refs="loggingTransformer" responseTransformer-refs="unzipTransformer doubleSumTransformer

27

Page 268: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 268/354

loggingTransformer"/> </pattern:web-service-proxy>...

9o%e %$a%:• ($ere is a #lobal %rans)ormer named Fdouble um(rans)ormerG

($is is %$e -( %rans)ormer %$a% uses %$e - %rans)orm developed earlier ($e -%rans)orm o) an -( %rans)ormer can be s%ored in a )ile, as in %$is example, or en%eredinline, as a value o) an a%%ribu%e o) %$e xsl%&%rans)ormerL elemen%

• ($e @eb service proxy named Fexcep%ion %ra%e#y*roxyG is abs%rac%As @i%$ %$e o%$er Mule pa%%erns, @eb service proxies suppor% in$eri%ance Abs%rac% paren%@eb service proxies can be declared and in$eri%ed )rom by one or more c$ild @eb service

proxies• ($e abs%rac% @eb service proxy Fexcep%ion %ra%e#y*roxyG de)ines an excep%ion s%ra%e#y

uc$ an excep%ion s%ra%e#y @ill come in%o e))ec% i) an excep%ion occurs @$en a re0ues% or i%sassocia%ed response passes %$rou#$ %$e @eb service proxyAbs%rac% @eb service proxies can de)ine o%$er proper%ies @$ic$ are %o be common amon#c$ild @eb service proxies, suc$ as a common ou%bound endpoin% e%c

• ($e @eb service proxy named F$ello orld*roxy2G in$eri%s )rom %$e abs%rac% proxymen%ioned aboveIn %$is example, %$is arran#emen% causes %$e F$ello orld*roxy2G %o $ave %$e sameexcep%ion s%ra%e#y as i%s paren%, %$e Fexcep%ion %ra%e#y*roxyG

• ($e F$ello orld*roxy2G @eb service proxy $as an a%%ribu%e (sdl,ileBsin# %$is a%%ribu%e, @e can ins%ruc% %$e @eb service proxy %o re%rieve a local )ile or adocumen% a% a #iven BR- Husin# %$e(sdlLocation a%%ribu%e @$en %$e .- o) %$e @eb

service bein# proxied is re0ues%ed• 9o %rans)ormers are con)i#ured on %$e @eb service proxy• ($e inbound endpoin% o) %$e @eb service proxy is de)ined as a c$ild elemen% o) %$e @eb&

service&proxyL elemen%($e BR- o) %$e inbound endpoin% is $%%p:==local$os%:; + =$ello orld2

• ($e ou%bound endpoin% o) %$e @eb service proxy is de)ined as a c$ild elemen% o) %$e @eb&service&proxyL elemen%

• ($e ou%bound endpoin% o) %$e @eb service proxy uses %$eresponse'imeout a%%ribu%e($is a%%ribu%e decides )or $o@ lon#, in milliseconds, %$e endpoin% @ill @ai% )or a

sync$ronous response• ($e ou%bound endpoin% o) %$e @eb service proxy applies %rans)orma%ions %o bo%$ re0ues%s

and correspondin# responses, usin# %$e transformer-refs and response'ransformer-refsa%%ribu%es respec%ivelyRe0ues%s passed on %o %$e underlyin# @eb service are lo##ed, usin# %$e lo##in# %rans)ormerseen in %$e previous exampleResponses received )rom %$e underlyin# @eb service are decompressed, - %rans)ormedand lo##ed

27;

Page 269: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 269/354

(o/if< the #SDL )ile

e @an% %o be able %o dis%in#uis$ %$e .- s%ored in %$e )ile F<ello ervice @sdlG )rom any .-#enera%ed by soapBI, as %o ma/e sure %$a% %$e @eb service proxy responds re0ues%s )or %$e .-@i%$ %$e con%en%s o) %$e )ile

1pen %$e F<ello ervice @sdlG and inser% a commen% as s$o@n in %$e )ollo@in# lis%in#:<?xml version="1.0" encoding="UTF-8"?><!-- This is a local copy of the WSDL on the classpath.--><wsdl:definitions targetNamespace="urn:helloWorld/sample/ivan/com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="urn:helloWorld/sample/ivan/com" xmlns:types="urn:helloWorld/types" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="HelloWorld">...

+un the #eb Ser$ice Prox< Example

e are no@ ready %o %es% %$e second @eb service proxy example• Ri#$%&clic/ %$e Fmule&@sproxy&con)i# xmlG Mule con)i#ura%ion )ile and selec% Run As &L

Mule erver• Ma/e sure %$a% %$e moc/ service in soapBI is s%ar%ed

6or de%ails on se%%in# up and s%ar%in# %$e moc/ service in soapBI, please re)er %o %$eExample *repara%ions earlier

• 1pen %$e FRe0ues% !G %$a% @e used %o %es% %$e moc/ service in %$eExample *repara%ions earlier and modi)y %$e address %o @$ic$ re0ues%s are %o be sen% %o become$%%p:==local$os%:; + =$ello orld2

ee %$e de%ailed ins%ruc%ions on $o@ %o c$an#e %$is address in %$e previous @eb service proxy example

• "lic/ %$e li%%le #reen %rian#le in %$e upper&le)% corner o) %$e Re0ues% ! @indo@($is @ill send %$e re0ues% %o %$e second @eb service proxy, @$ic$ in %urn @ill )or@ard %$ere0ues% %o %$e moc/ @eb serviceDou s$ould see ou%pu% similar %o %$e )ollo@in# in %$e console Hexcludin#, o) course, %$ecolours :

***** Message:org.mule.DefaultMuleMessage{ id=b0bee23b-8172-11e1-869b-45fdb5b2e92c payload=org.apache.commons.httpclient.ContentLengthInputStream correlationId=<not set> correlationGroup=-1 correlationSeq=-1 encoding=utf-8 exceptionPayload=<not set>

Message properties: INVOCATION scoped properties: INBOUND scoped properties: Accept-Encoding=gzip,deflate Connection=false Content-Length=333 Content-Type=text/xml; charset=utf-8 Host=localhost:8090 Keep-Alive=false

MULE_ORIGINATING_ENDPOINT=endpoint.http.localhost.8090.helloWorld2

27+

Page 270: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 270/354

MULE_REMOTE_CLIENT_ADDRESS=/127.0.0.1:52285 User-Agent=Jakarta Commons-HttpClient/3.1 http.context.path=/helloWorld2 http.method=POST http.request=/helloWorld2 http.request.path=/helloWorld2 http.version=HTTP/1.1 OUTBOUND scoped properties: Accept-Encoding=gzip,deflate

Connection=false Content-Length=333 Content-Type=text/xml; charset=utf-8 Host=localhost:8090 Keep-Alive=false MULE_ENCODING=utf-8 MULE_ENDPOINT=http://localhost:8088/mockHelloWorldBinding MULE_ROOT_MESSAGE_ID=b0bee23b-8172-11e1-869b-45fdb5b2e92c MULE_SESSION=rO0... User-Agent=Jakarta Commons-HttpClient/3.1 http.context.path=/helloWorld2 http.method=POST http.request=/helloWorld2 http.request.path=/helloWorld2 http.version=HTTP/1.1 SESSION scoped properties:}***** Message payload: <soapenv:Envelopexmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"xmlns:typ="urn:helloWorld/types"> <soapenv:Header/> <soapenv:Body> <typ:hello> <name>Ivan</name> <first_number>2</first_number> <second_number>3</second_number> </typ:hello> </soapenv:Body></soapenv:Envelope>INFO 2012-04-08 14:02:20,522 [connector.http.mule.default.receiver.04]org.mule.transport.service.DefaultTransportServiceDescriptor: Loading default outboundtransformer: ...***** Message:org.mule.DefaultMuleMessage{ id=b1b3f09f-8172-11e1-869b-45fdb5b2e92c payload=[B correlationId=<not set> correlationGroup=-1 correlationSeq=-1 encoding=utf-8 exceptionPayload=<not set>

Message properties: INVOCATION scoped properties: INBOUND scoped properties: Connection=false Content-Encoding=gzip Content-Type=text/xml; charset=utf-8 Keep-Alive=false Server=Jetty(6.1.x) Transfer-Encoding=chunked http.method=POST http.request=http://localhost:8088/mockHelloWorldBinding http.status=200 http.version=HTTP/1.1 OUTBOUND scoped properties: MULE_ENCODING=utf-8 MULE_SESSION=rO0... SESSION scoped properties:}***** Message payload: <?xml version="1.0" encoding="utf-8"?><soapenv:Envelopexmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"xmlns:typ="urn:helloWorld/types"> <soapenv:Header/> <soapenv:Body> <typ:helloResponse> <message>Hello there!</message>

2

Page 271: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 271/354

<sum>84</sum> </typ:helloResponse> </soapenv:Body></soapenv:Envelope>

9o%e %$a%:•

($e par% o) %$e lo# $i#$li#$%ed in blue is %$e re0ues% messa#ee can see %$a% %$e 1A* re0ues% is iden%ical %o %$a% @e $ave seen in soapBI• ($e par% o) %$e lo# $i#$li#$%ed in #reen is lo# messa#es )rom Mule ini%ializin# i%s

componen%s($is par% $as been s$or%ened, %o increase readabili%y

• ($e par% o) %$e lo# $i#$li#$%ed in oran#e is %$e response messa#ee can see %$a% %$e 1A* response is iden%ical %o %$a% @e $ave con)i#ured %$e moc/ service

in soapBI %o respond @i%$• e can read %$e M- o) %$e 1A* response

As in %$e previous @eb service proxy example, %$e Funzip(rans)ormerG %rans)ormerdecompresses compressed response messa#es

• In %$e above ou%pu%, Mule session iden%i)ier da%a $as been s$or%ened %o avoid clu%%erin#• ($e con%en%s o) %$e %$e sumL elemen% in %$e response 1A* messa#e is ;5

e %$us can conclude %$a% %$e - %rans)orm $as been applied %o %$e response messa#e, asin%ended

9ex%, @e examine %$e .- o) %$e service %o @$ic$ @e send re0ues%s:• In a @eb bro@ser, issue a re0ues% %o %$e )ollo@in# BR-:$%%p:==local$os%:; + =$ello orld2

@sdl• In %$e @eb bro@ser, you s$ould see %$e .- @i%$ %$e commen% @e inser%ed in %$e

F<ello ervice @sdlG )ile earlier, s%a%in# %$a%: F($is is a local copy o) %$e .- on %$eclasspa%$ G

($is concludes %$e @eb service proxy example and %$e c$ap%er on Mule con)i#ura%ion pa%%erns

2 !

Page 272: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 272/354

Part T4o – +ecipes an/ +eference($is par% con%ains smaller, incomple%e, examples %$a% are more )ocused on solvin# a par%icular %as/or problem and some re)erence in)orma%ion Mo%iva%ions and explana%ions are more scarce in %$is

par%, compared %o par% one

1. Message Routing ($is sec%ion con%ains recipes rela%ed %o %$e rou%in# o) messa#es

1 1 Selecting Outboun/ En/point Depen/ing on the (essageAssume %$a% you @an% %o send a messa#e %o one o) several ou%#oin# endpoin%s dependin# on somec$arac%eris%ic o) %$e messa#e1ne @ay %o accomplis$ %$is is by usin# mul%iple )il%erin#&rou%erL elemen%s inside %$e ou%boundLelemen% o) a service:

...<service name="directoriesWriterService"> <inbound> <vm:inbound-endpoint path="directoriesWriter"/> </inbound> <outbound> <filtering-router> <!-- The outbound endpoint that will be used if the condition of the filter in this filtering router is met. --> <file:outbound-endpoint path="directory1/" outputPattern="#[header:originalFilename]"/> </file:outbound-endpoint> <!--

The filter that determines the messages that will send to the outbound endpoint in this filtering router. --> <regex-filter pattern="some words (.*)"/> <!-- Any transformations to be applied before messages are filtered can be insert here. --> </filtering-router> <filtering-router> ... </filtering-router> <!-- If a message does not meet the condition of any filter, it will be sent to the outbound endpoint specified in the <forwarding-catch-all-Strategy> element. -->

<forwarding-catch-all-strategy> <file:outbound-endpoint path="directory-nomatch/" outputPattern="#[header:originalFilename]"/> </file:outbound-endpoint> </forwarding-catch-all-strategy> </outbound>...

9o%e %$a%:• ($e ou%boundL elemen% o) %$e service con%ains mul%iple )il%erin#&rou%erL elemen%s• A )il%erin#&rou%erL elemen% con%ains one ou%bound endpoin%, one op%ional )il%er and any

number o) %rans)ormers

2 2

Page 273: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 273/354

• ($e ou%bound endpoin% in a )il%erin#&rou%erL is manda%ory• ($e )il%er in a )il%erin#&rou%erL elemen% de%ermines @$e%$er a messa#e @ill be sen% %o %$e

ou%bound endpoin% in %$e )il%erin#&rou%erL elemen%I) %$e messa#e mee%s %$e condi%ion o) %$e )il%er, %$e messa#e @ill be sen% %o %$e ou%boundendpoin%

($e )il%er in a )il%erin#&rou%erL is op%ional• A )il%erin#&rou%erL can con%ain zero or more %rans)ormers %$a% %rans)orm messa#es be)ore

%$ey are )il%ered• ($e ou%boundL elemen% con%ains a )or@ardin#&ca%c$&all&s%ra%e#yL elemen%

I) a messa#e does no% mee% %$e condi%ion o) any o) %$e )il%ers in %$e )il%erin#&rou%erLelemen%s, i% @ill be sen% %o %$e ou%bound endpoin% in %$e ca%c$&all s%ra%e#y elemen%

• I) no ca%c$&all s%ra%e#y elemen% is de)ined in an ou%boundL elemen% and a messa#e does no%mee% %$e condi%ion o) any o) %$e )il%ers in %$e )il%erin#&rou%erL elemen%s, %$e messa#e @ill

be i#nored and a @arnin# @ri%%en %o %$e lo#

1 " +outing a (essage Depen/ing on a Single )ilter In a service, @e can rou%e messa#es in one o) %@o direc%ions dependin# on @$e%$er or no% a messa#emee%s %$e condi%ions o) a )il%er

...<service name="someService"> <inbound> <vm:inbound-endpoint path="some.input.path"/> <!-- A selective consumer router can apply one filter to incoming messages. --> <selective-consumer-router> <!-- The filter determines whether or not the message is to be passed on to the outbound endpoint. If a message matches the filter conditions, it will be passed to the outbound endpoint. If a message does not match the filter conditions and there is a catch-all strategy defined in the <inbound> element, the message will be forwarded to the catch-all strategy. If a message does not match the filter conditions and there is no catch-all strategy defined in the <inbound> element, the message will be dropped and a message written to the log. --> <mule-xml:schema-validation-filter schemaLocations ="schemas/schema2.xsd" returnResult="false"/> </selective-consumer-router> <!--

Catch-all strategy specifying where messages that do not match the filter conditions are to be routed. --> <forwarding-catch-all-strategy> <vm:outbound-endpoint path="some.no.filtermatch.path"/> </forwarding-catch-all-strategy> </inbound> <outbound> <!-- Messages arriving to the outbound endpoint match the filter conditions. --> <pass-through-router> <vm:outbound-endpoint path="some.filtermatch.path"/> </pass-through-router> </outbound></service>...

2 3

Page 274: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 274/354

1 & Exception=Depen/ent (essage +outing($ere are %@o op%ions re#ardin# $o@ %o do rou%in# based on excep%ions, or ra%$er based %$e )ailureor success o) one o) a se% o) ou%bound endpoin%s

%he Exception9 ased Router ($e excep%ion&based rou%er is available in bo%$ Mule 2 x and Mule 3 x ($e )ollo@in# examples$o@s %$e excep%ion&based rou%er bein# used in a Mule 2 x con)i#ura%ion:

...<service name="receiverService"> ... <outbound> <!-- The exception based router attempts to send the message to the the contained endpoints in the order listed until having encountered an endpoint that does not throw an exception. --> <exception-based-router>

<!-- All endpoints but the last in the exception-based router will be forced to be synchronous. The last outbound endpoint will be asynchronous unless the synchronous attribute is explicitly set to true in the <outbound-endpoint> element in the exception- based router. The synchronous attribute on the inbound endpoint to which the message is sent will not be considered by the router. --> <vm:outbound-endpoint path="exceptionServiceInbound"/> <vm:outbound-endpoint path="helloServiceInbound" synchronous="true"/> <!-- Optionally, the exception-based router can be configured with a reply-to address, to which a message that has been processed by a service, that received the message from the exception-based router, will send the message.

--> <reply-to address="vm://nextServiceURL"/> </exception-based-router> </outbound></service>...

9o%e %$a%:• Rou%ers, as %$e excep%ion&based rou%er, are commonly used in services• ($e excep%ion&based rou%er @ill a%%emp% %o send a messa#e %o eac$ o) %$e ou%bound

endpoin%s in %$e excep%ion&based&rou%erL elemen%, in %$e order %$ey are lis%ed, un%ilencoun%erin# an invoca%ion %$a% does no% resul% in an excep%ion

• ($e excep%ion&based rou%er @ill )orce sync$ronous invoca%ion o) all %$e con%ained ou%boundendpoin%s excep% )or %$e las%($us i) @e @an% %$e invoca%ion o) %$e las% ou%bound endpoin% %o be sync$ronous, @e mus%use %$e s$nchronous a%%ribu%e HMule 2 x or %$eexchange-pattern a%%ribu%e HMule 3 x

• ($e excep%ion&based rou%er may be con)i#ured @i%$ zero or more %rans)ormers• ($e ou%bound endpoin%s o) %$e excep%ion&based rou%er may be con)i#ured @i%$ )il%ers• ($e excep%ion&based rou%er may be con)i#ured @i%$ a reply&%o address

A)%er %$e messa#e $as been success)ully processed by one o) %$e services lis%enin# %o %$eou%bound endpoin%s lis%ed in %$e excep%ion&based rou%er, %$e messa#e @ill be sen% %o %$e

2 5

Page 275: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 275/354

address con)i#ured in %$e reply&%oL elemen%'saddress a%%ribu%e• I) all %$e ou%bound endpoin%s o) %$e excep%ion&based rou%er resul% in excep%ions bein#

%$ro@n, an excep%ion @ill be %$ro@n($e excep%ion messa#e @ill indica%e %$a% %$e excep%ion&based rou%er )ailed %o rou%e %$emessa#e

Excep%ions %$ro@n by services %$a% %$e excep%ion&based rou%er a%%emp%s %o invo/e can belo##ed usin# a de)aul% or cus%om excep%ion s%ra%e#y, ei%$er on %$e individual servicecon%ainin# %$e excep%ion&based rou%er or on %$e model con%ainin# %$e service con%ainin# %$eexcep%ion&based rou%er

%he First9Successful Message 'rocessor

($e )irs%&success)ul messa#e processor is only available in Mule 3 x ($is messa#e processor isused in )lo@s, as s$o@n in %$e )ollo@in# example:...<flow name="ReceiverFlow"> ...

<!-- The first-successful message processor attempts to send the message to the the contained endpoints in the order listed until having encountered an endpoint that succeeds. The exchange-pattern attribute of the outbound endpoints in the first-successful message processor can be set to "request-response" for better reliability, but may be "one-way" as well. The failureExpression attribute defines an expression used to determine whether an invocation of an endpoint is a failure or not. In this case, an endpoint invocation is a failure if there is an exception of the type java.lang.Exception thrown. --> <first-successful failureExpression="exception-type:java.lang.Exception"> <vm:outbound-endpoint

path="exceptionServiceInbound" exchange-pattern="request-response"/> <vm:outbound-endpoint

path="helloServiceInbound" exchange-pattern="request-response"/> </first-successful></flow>...

9o%e %$a%:• ($e )irs%&success)ul messa#e processor can appear any@$ere in a )lo@ @$ere a messa#e

processor can appear($is means %$a% i% does no% necessarily $ave %o appear las% in a )lo@

• ($e failure&xpression a%%ribu%e o) %$e )irs%&success)ulL elemen% con%ains an expression %$a%

is used %o de%ermine @$e%$er %$e invoca%ion o) an endpoin% $as )ailed or no%($e de)aul% expression de%ermines )ailure as an excep%ion bein# %$ro@n or re%urned as %$eresul% o) %$e endpoin% invoca%ion($e expression in %$e above example de%ermines )ailure as an excep%ion o) %$e %ype

4ava.lang.&xception $avin# been %$ro@n or re%urned• ($e )irs%&success)ul messa#e processor @ill a%%emp% %o send a messa#e %o eac$ o) %$e

ou%bound endpoin%s in %$e )irs%&succes)ulL elemen%, in %$e order %$ey are lis%ed, un%ilencoun%erin# an invoca%ion %$a% does no% resul% in %$e failure&xpression evalua%in# %o %rue

• ($e )irs%&success)ul messa#e processor @ill no% )orce sync$ronous invoca%ion o) anyendpoin% lis%ed in %$e )irs%&success)ulL elemen%

<o@ever, usin# sync$ronous invoca%ion, %$a% is %$e messa#e exc$an#e&pa%%ern Fre0ues%&responseG @ill increase reliabili%y

2 8

Page 276: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 276/354

• I) all %$e ou%bound endpoin%s o) %$e )irs%&success)ul messa#e processor resul% in excep%ions bein# %$ro@n, an excep%ion @ill be %$ro@n($e excep%ion messa#e @ill indica%e %$a% %$e messa#e processor )ailed %o rou%e %$e messa#e

• Excep%ions %$ro@n by services %$a% %$e )irs%&success)ul messa#e processor a%%emp%s %o invo/e@ill be lo##ed by a de)aul% or cus%om excep%ion s%ra%e#y($e excep%ion s%ra%e#y mus% be con)i#ured on %$e same )lo@ as in @$ic$ %$e )irs%&success)ulmessa#e processor appears 6lo@s al@ays $ave a de)aul% excep%ion s%ra%e#y, even i) one $asno% been explici%ly con)i#ured

2 7

Page 277: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 277/354

Page 278: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 278/354

" " Combining )ilters(@o or more )il%ers can be combined %o ac% as one sin#le )il%er usin# %$e s%andard lo#ic opera%orsA9. and 1R 6il%ers can be ne#a%ed usin# %$e 91(&)il%er

%he !"*9filter ($e )ollo@in# )il%er con)i#ura%ion combines %$e payload&%ype&)il%erL and %$e re#ex&)il%erL as %oma%c$ only messa#es @i%$ payload o) %$e %ype 4ava.lang.String %$a% s%ar%s @i%$ %$e c$arac%erse0uence Fsecre% F )ollo@ed by zero or more c$arac%ers

...<and-filter> <payload-type-filter expectedType="java.lang.String"/> <regex-filter pattern="secret (.*)"/></and-filter>...

9o%e %$e and&)il%erL elemen% %$a% con%ains %@o or more )il%ers, all o) @$ic$ condi%ions mus% bema%c$ed in order )or %$e A9.&)il%er %o ma%c$

%he :R9filter

($e )ollo@in# )il%er con)i#ura%ion combines %$e messa#e&proper%y&)il%erL and %$e @ildcard&)il%erL as %o ma%c$ messa#es @$ic$ ei%$er $as %$e messa#e proper%y Fori#inal6ilenameG @i%$ a valueFma%c$in#&ms# %x%G or @$ic$ messa#e payload s%ar%s @i%$ %$e s%rin# F%$e pi#sG...<or-filter> <message-property-filter pattern="originalFilename=matching-msg.txt"/> <wildcard-filter pattern="the pigs*"/></or-filter>...

9o%e %$e or&)il%erL elemen% %$a% con%ains %@o or more )il%ers, o) @$ic$ a% leas% one )il%er condi%ionmus% be ma%c$ed in order )or %$e 1R&)il%er %o ma%c$

%he ":%9filter

($e 91(&)il%er ne#a%es %$e resul% o) one sin#le )il%er ($e )ollo@in# )il%er con)i#ura%ion accep%s allmessa#es @$ic$ payload is no% @ell&)ormed M-

...<not-filter> <custom-filter class="org.mule.module.xml.filters.IsXmlFilter"/></not-filter>...

9o%e %$e no%&)il%erL elemen% %$a% con%ains one sin#le )il%er ($e resul% o) %$e con%ained )il%er isne#a%ed

2 ;

Page 279: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 279/354

" & %mplementing Custom )iltersIn addi%ion %o %$e )il%ers supplied by Mule, i% is also possible %o implemen% cus%om )il%ers($e )ollo@in# class s$o@s @$a% a cus%om )il%er implemen%ed in ?ava may loo/ li/e:

package com.ivan.filters;

import org.mule.api.MuleMessage;import org.mule.api.routing.filter.Filter;

/** * This class implements a custom Mule filter. *

* @author Ivan Krizsan */public class MyCustomFilter implements Filter{ /* Instance variable(s): */ private String mSomeFilterProperty;

/** * Checks the supplied message to determine whether it * matches the filter conditions.

** @param inMessage Non-null Mule message to check. * @return True if message matches filter conditions, false * otherwise. */ @Override public boolean accept(final MuleMessage inMessage) { System.out.println("*** Message payload: " + inMessage.getPayload()); System.out.println(" Filter property: " + mSomeFilterProperty);

boolean theIsStringPayloadFlag = inMessage.getPayload().getClass().equals( String.class); return theIsStringPayloadFlag; }

public void setSomeFilterProperty(final String inSomeFilterProperty) { mSomeFilterProperty = inSomeFilterProperty; }}

($e )il%er can %$en be used in a Mule con)i#ura%ion )ile:

...<custom-filter class="com.ivan.filters.MyCustomFilter"> <spring:property name="someFilterProperty" value="Hello filtered world!"/></custom-filter>...

9o%e %$e proper%y bein# in4ec%ed in%o %$e )il%er

2 +

Page 280: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 280/354

3. %ransfor&ing ($is sec%ion con%ains recipes rela%ed %o %rans)orma%ion o) messa#es

& 1 Extract Part of an 5(L (essage 4ith 5Path

(o ex%rac% a par% o) %$e M- payload o) a messa#e, @e can use %$e *a%$ ex%rac%or %rans)ormer($e )ollo@in# example s$o@s $o@ %o apply %$e *a%$ ex%rac%or %rans)ormer inside a )ile:inbound&endpoin%L elemen%...<file:inbound-endpoint connector-ref="fileConnector" path="./file-input-directory"> <file:filename-wildcard-filter pattern="*.xml"/>

<!-- Must transform the XML data to DOM in order to receive a DOM node as result of the XPath transform. Otherwise we will receive the contents of the elements in the extracted XML fragment. --> <mule-xml:xml-to-dom-transformer returnClass="org.w3c.dom.Document" />

<!-- Apply an XPath expression to the message. The desired type of result can be specified as one of: STRING (default), BOOLEAN, NUMBER, NODE, NODESET --> <mule-xml:xpath-extractor-transformer expression="/ivan:PhoneBook/ivan:Person" resultType="NODE"> <!-- The XML data we are processing is in a namespace and we use a namespace prefix in the XPath expression. We must thus link the namespace prefix and the namespace URI. --> <mule-xml:namespace prefix="ivan" uri="http://www.ivan.com/schemas/addressbook"/> </mule-xml:xpath-extractor-transformer>

<!-- Must transform the message from DOM to text to make the resulting XML readable. --> <mule-xml:dom-to-xml-transformer/></file:inbound-endpoint>...

e usually combine %$e *a%$ ex%rac%or %rans)ormer @i%$ one or %@o o%$er %rans)ormers:• mule&xml:xml&%o&dom&%rans)ormerL

(o ob%ain a .1M&%ree on @$ic$ %o apply %$e %rans)orm A .1M&%ree is needed in order )or%$e resul% o) %$e *a%$ %rans)ormer %o be a M- )ra#men%

• mule&xml:xpa%$&ex%rac%or&%rans)ormerLApplies an *a%$ expression %o %$e .1M&%ree )rom %$e previous s%ep

• mule&xml:dom&%o&xml&%rans)ormerL1p%ional Bsed i) @e @an% %$e payload o) %$e messa#e %o be an M- s%rin#

9o%e %$a% i) %$e M- da%a %o be processed belon#s %o a namespace, @e mus% use a namespace pre)ixin %$e *a%$ expression and de)ine %$is namespace pre)ix usin# a mule&xml:namespaceL c$ildelemen% in %$e mule&xml:xpa%$&ex%rac%or&%rans)ormerLIn Mule 3 !, #lobally de)ined namespaces, usin# %$e mule&xml:namespace&mana#erL elemen%, are

no% used by %$e *a%$ ex%rac%or %rans)ormer ($is )ea%ure is available in Mule 3 2

2;

Page 281: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 281/354

& " Transform 5(L Data 6sing 5SLA common scenario is %$a% M- da%a needs %o be processed in some @ay >o%$ Mule 2 x and 3 xsuppor% - %rans)orma%ion usin# %$e xsl%&%rans)ormerL )rom %$e M- module($e )ollo@in# example s$o@s $o@ %o double %$e number con%ained in %$e sumL elemen% o) a

1A* messa#e

($e 1A* messa#e %$a% @e @is$ %o process loo/s li/e %$is:<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:typ="urn:helloWorld/types"> <soapenv:Header/> <soapenv:Body> <typ:helloResponse> <message>Hello there!</message> <sum>42</sum> </typ:helloResponse> </soapenv:Body></soapenv:Envelope>

($e - %rans)orma%ion %$a% @e @is$ %o apply loo/s li/e %$is:

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:typ="urn:helloWorld/types" exclude-result-prefixes="xs" version="2.0">

<xsl:template match="sum"> <sum><xsl:value-of select="text() * 2"/></sum> </xsl:template>

<xsl:template match="node( ) | @*"> <xsl:copy> <xsl:apply-templates select="@* | node( )"/> </xsl:copy>

</xsl:template></xsl:stylesheet>

- developmen% is ou%side %$e scope o) %$is boo/, bu% in brie) %$e above - %rans)orma%ioncopies all %$e elemen%s and a%%ribu%es o) an M- documen%, excep% )or %$e con%en%s o) sumLelemen%s, @$ic$ is mul%iplied by %@o

6inally, %$e de)ini%ion o) a #lobal - %rans)ormer in a Mule con)i#ura%ion )ile loo/s li/e %$is,assumin# %$e above - %rans)orma%ion $as been saved in a )ile named Fsum.ouble(rans)orm xslGand %$e namespace pre)ix Fmule&xmlG as been con)i#ured appropria%ely:

...

<!-- Transformer that applies the XSL transform stored in the file "sumDoubleTransform.xsl" to XML data in a message payload. --> <mule-xml:xslt-transformer name="doubleSumTransformer" xsl-file="sumDoubleTransform.xsl"/>...

2;!

Page 282: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 282/354

& & Pack or 6npack (essage Pa<loa/ DataMule 2 x and 3 x $as %@o %rans)ormers %$a% can compress or uncompress messa#e payload da%a(o uncompress, use %$e C[I* uncompress %rans)ormer ($e )ollo@in# example s$o@s %$edeclara%ion o) a #lobal uncompressin# %rans)ormer:...

<gzip-uncompress-transformer name="unzipTransformer"/>...

($e uncompressin# %rans)ormer accep%s %$e )ollo@in# inpu% %ypes, @$ic$ bo%$ are %o con%aincompressed da%a:

• >y%e arrays• Inpu% s%reams

(o compress da%a, use %$e <[I* compress %rans)ormer ($e )ollo@in# example s$o@s %$edeclara%ion o) a #lobal compressin# %rans)ormer:... <gzip-compress-transformer name="zipTransformer"/>...

($e compressin# %rans)ormer accep%s %$e )ollo@in# inpu% %ypes:• >y%e arrays• Inpu% s%reams• erializable ob4ec%s

2;2

Page 283: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 283/354

Page 284: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 284/354

Mule 3.x Custo& %ransfor&ers

($is example s$o@s %$e Mule 3 x version o) a %rans)ormer %$a% @ill receive a re)erence %o %$ecurren% messa#e:package com.ivan.transformers;

import org.mule.api.MuleMessage;import org.mule.api.transformer.TransformerException;import org.mule.transformer.AbstractMessageTransformer;

/** * Mule 3.x transformer that logs message that passes through * the transformer to the console. Messages are not altered in any way. *

* @author Ivan Krizsan */public class MessageLoggingTransformer extends AbstractMessageTransformer{ @Override public Object transformMessage(final MuleMessage inMessage, final String inOutputEncoding) throws TransformerException { try

{ System.out.println("***** Message: " + inMessage.toString()); System.out.println("***** Message payload: " +

inMessage.getPayloadAsString()); } catch (Exception theException) { theException.printStackTrace(); } return inMessage; }}

9o%e %$a%:• ($e above %rans)ormer does no% al%er %$e messa#e in any @ay

I) i% $ad, %$e al%ered messa#e @ould $ave been re%urned by %$etransformMessage me%$od• ($e cus%om %rans)ormer class in$eri%s )rom %$e AbstractMessage'ransformer class($is is a #ood s%ar%in#&poin% i) you are developin# a messa#e&a@are Mule 3 x %rans)ormerAll %rans)ormers are re0uired %o implemen% %$eorg.mule.api.transformer.'ransformerin%er)ace, ei%$er direc%ly or by in$eri%ance *lease re)er %o i%s A*I documen%a%ion )or a lis% o)classes %$a% implemen% %$is in%er)ace

• Ano%$er #ood s%ar%in#&poin% )or %rans)ormer&developmen% is %$e superclass %o %$e AbstractMessage'ransformer J org.mule.transformer.Abstract'ransformer ($e Abstract'ransformer class is a #eneral base&class )or all %rans)ormers

$en %$e %rans)ormer class $as been developed, @e can use i% in a Mule 3 x con)i#ura%ion )ile usin#

%$e cus%om&%rans)ormerL elemen%($e )ollo@in# example s$o@s a cus%om %rans)ormer bein# con)i#ured as a #lobal %rans)ormer @i%$%$e name Flo##in#(rans)ormerG:... <custom-transformer name="loggingTransformer" class="com.ivan.transformers.MessageLoggingTransformer"/>...

2;5

Page 285: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 285/354

,. Message 'roperties($is sec%ion con%ains recipes rela%ed %o readin# and manipula%ion o) messa#e proper%ies, also /no@nas messa#e $eaders Messa#e proper%ies are addi%ional pieces o) da%a %$a% %ravels alon# @i%$ amessa#e in a map (o re%rieve or se% a messa#e proper%y, @e need %o /no@ i%s name

' 1 +etrie$ing (essage Properties(o re%rieve a messa#e proper%y, a Mule expression can be used I) %$e ori#inal )ilename o) a )ile $as

been inser%ed as a messa#e proper%y @i%$ %$e name Fori#inal6ilenameG, as seen in %$e example inc$ap%er 5, i% can be re%rieved and used as %$e )ilename o) a )ile %o be @ri%%en li/e in %$e )ollo@in#example:

...<file:outbound-endpoint path="./file-output-directory" outputPattern="#[header:originalFilename]"/>...

Addi%ional %ex% and expressions may be inser%ed be)ore and a)%er %$e expression re%rievin# %$eori#inal )ilename messa#e proper%y

' " Setting (essage PropertiesMessa#e proper%ies can be se% usin# %$e messa#e proper%ies %rans)ormer ($e )ollo@in# exampleinser%s %$e messa#e proper%y @i%$ %$e name F*R1*ER(D9AMEG and %$e s%rin# valueF*roper%yValueG in%o %$e curren% messa#e

...<message-properties-transformer> <add-message-property key="PROPERTYNAME" value="PropertyValue"/>

</message-properties-transformer>...

9o%e %$a% Mule expressions can be used in %$e values o) %$ee$ and value a%%ribu%esAlso no%e %$a% a messa#e proper%ies %rans)ormer may con%ain mul%iple c$ild elemen%s %$a% adds,removes or renames messa#e proper%ies

' & +emo$ing (essage PropertiesMessa#e proper%ies can be removed usin# %$e messa#e proper%ies %rans)ormer ($e )ollo@in#example removes %$e messa#e proper%y @i%$ %$e name F*R1*ER(D9AMEG )rom %$e curren%messa#e...<message-properties-transformer> <delete-message-property key="PROPERTYNAME"/></message-properties-transformer>...

9o%e %$a% Mule expressions can be used in %$e value o) %$ee$ a%%ribu%eAlso no%e %$a% a messa#e proper%ies %rans)ormer may con%ain mul%iple c$ild elemen%s %$a% adds,removes or renames messa#e proper%ies

2;8

Page 286: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 286/354

' ' +enaming (essage PropertiesMessa#e proper%ies can be renamed usin# %$e messa#e proper%ies %rans)ormer ($e )ollo@in#renames %$e messa#e proper%y @i%$ %$e name F*R1*ER(D9AMEG %o F9E *R1*ER(D9AMEG

...<message-properties-transformer> <rename-message-property key="PROPERTYNAME" value=”NEWPROPERTYNAME”/>

</message-properties-transformer>...

9o%e %$a% Mule expressions can be used in %$e value o) %$ee$ a%%ribu%eAlso no%e %$a% a messa#e proper%ies %rans)ormer may con%ain mul%iple c$ild elemen%s %$a% adds,removes or renames messa#e proper%ies

' * +ea/ing an/ #riting (essage Properties to Different ScopesMessa#e proper%ies can be read and @ri%%en %o di))eren% scopes, as lis%ed belo@ Messa#e proper%yscopes are more s%ric%ly en)orced in Mule 3 x compared %o earlier versions o) Mule

• InboundMessa#e proper%ies ori#ina%in# )rom clien%s re0ues%s are )ound in %$is scope

• 1u%boundMessa#e proper%ies %o be re%urned or sen% ou% are )ound in %$is scope

• essionMessa#e proper%ies %$a% are %o be re%ained %$rou#$ou% mul%iple invoca%ions are %o be placedin %$is scope

($e messa#e proper%y scope can be speci)ied in di))eren% @ays, dependin# on %$e con%ex%In %$e )irs% example, i% is speci)ied usin# an a%%ribu%e o) a %rans)ormer:...<message-properties-transformer scope="outbound"> <add-message-property key="validated" value="true"/></message-properties-transformer>...

($e )ollo@in# example %es%s )or @$e%$er %$e proper%y 'valida%ed' in %$e ou%bound scope $as %$e value'%rue':...<when expression="OUTBOUND:validated=true" evaluator="header">...

Messa#e proper%ies can be re%rieved usin# %$e Mule expression lan#ua#e ($is example lo#s amessa#e @i%$ %$e value o) %$e 'valida%ed' messa#e proper%y )rom %$e ou%bound scope

...<logger message="Validated flag: #[header:OUTBOUND:validated]" level="ERROR"/>...

2;7

Page 287: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 287/354

/. ExpressionsExpressions allo@ )or re%rieval o) da%a )rom messa#es, messa#e $eaders, messa#e a%%ac$men%s,

beans e%c in, )or ins%ance, %$e )ollo@in# /inds o) Mule con)i#ura%ion elemen%s:• Expression %rans)ormers

Example: xpa%$&ex%rac%or&%rans)ormerL• Expressions )il%ers

Example: expression&)il%erL• Expression&based rou%ers

Example: expression&spli%%er&rou%erL

9o%e %$a% expressions are no% allo@ed every@$ereJ an inbound endpoin% needs %o /no@ )rom @$ere%o receive messa#es a% con)i#ura%ion %ime @$ile %$e evalua%ion o) %$e address %o @$ic$ an ou%boundendpoin% is %o send a messa#e can be de)erred %o run%ime

* 1 E$aluators.i))eren% /inds o) evalua%ors are used %o evalua%e di))eren% /inds o) expressions 6or ins%ance, %$ereare evalua%ors %$a% can access %$e a%%ac$men%s o) messa#es, an evalua%or %$a% allo@s )or access o)messa#e $eaders e%c>elo@ are %$e de)aul% Mule evalua%ors available in Mule 3 2 lis%ed

!ttach&ent Evaluator

($e a%%ac$men% evalua%or allo@s )or access %o any a%%ac$men%Hs %$a% a messa#e may $ave byspeci)yin# %$e name o) %$e a%%ac$men% ($e presence o) %$e a%%ac$men% is made op%ional byappendin# F G %o i%s name I) an a%%ac$men% is no% presen%, an excep%ion @ill be %$ro@n i) %$e namedoes no% end @i%$ F G

Example expression: TUa%%ac$men%:mail&a%%ac$men%Re%rieves %$e a%%ac$men% @i%$ %$e name Fmail&a%%ac$men%' )rom %$e messa#eRe%urns null i) %$e a%%ac$men% is no% presen%

Res%ric%ions: "anno% be used in expression )il%ers

!ttach&ents Evaluator

($e a%%ac$men%s evalua%or re%rieves a map con%ainin# %$e named a%%ac$men%s o) a messa#e ($e presence o) an a%%ac$men% is made op%ional by appendin# F G %o i%s name

Example expression: TUa%%ac$men%s:)oo,bar ,bazRe%rieves %$e a%%ac$men%s @i%$ %$e names F)ooG Hre0uired , FbarG Hop%ionaland FbazG Hop%ional )rom %$e messa#e

Res%ric%ions: "anno% be used in expression )il%ers

2;

Page 288: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 288/354

!ttach&ents9$ist Evaluator

($e a%%ac$men%s&lis% evalua%or re%rieves a lis% ob4ec%, implemen%in# 4ava.util.List , con%ainin# %$enamed a%%ac$men%s o) a messa#e ($e presence o) an a%%ac$men% is made op%ional by appendin# F G%o i%s name ($e F G @ildcard can be used @$en speci)yin# a%%ac$men% namesJ %$a% is F)oo G @illma%c$ F)oodG and F)oolG ($e F G used by i%sel) @ill ma%c$ any name

Example expression: TUa%%ac$men%s&lis%:)oo ,bazRe%rieves a lis% con%ainin# %$e a%%ac$men% @i%$ %$e name F)ooG Hop%ionaland all a%%ac$men% @i%$ names s%ar%in# @i%$ FbazG Hop%ional

Res%ric%ions: "anno% be used in expression )il%ers

ean Evaluator

($e bean evalua%or access proper%ies )rom a ?ava bean bein# %$e payload o) %$e curren% Mulemessa#e usin# %$e #e%%ers and se%%ers o) %$e ?ava bean

Example:

Assume %$e )ollo@in# ?ava bean class H#e%%ers and se%%ers $ave been omi%%ed %o conserve space :...public class MyBean{ private MyBean root; private MyBean of; private String all; ... /* Getters and setters for the above instance variables have been omitted. */}

($e )ollo@in# code is used %o ini%ialize and se% %$e payload o) a Mule messa#e:

... MyBean theOf = new MyBean(); theOf.setAll("evil"); MyBean theRoot = new MyBean(); theRoot.setOf(theOf); MyBean thePayload = new MyBean(); thePayload.setRoot(theRoot);

MuleMessage theMuleMessage = new DefaultMuleMessage(thePayload, theContext1);...

In %$e )ollo@in# lo##erL elemen%, %$e messa#e FevilG is re%rieved )rom %$e My>ean ins%ance @$ic$re)erence is s%ored in %$e variablethePa$load crea%ed in %$e code above

<logger level="ERROR" message="#[bean:root.of.all]"/>

($e ?ava code correspondin# %o %$e expression TUbean:roo% o) all is:

String theMsg = thePayload.getRoot().getOf().getAll();

2;;

Page 289: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 289/354

Example expression: TUbean:roo% o) allRe%rieves an ob4ec% by invo/in# get#oot<= on %$e messa#e payload, %$enre%rieves an ob4ec% by invo/in# getOf<= on %$e previous resul%, %$en re%rievessome%$in# by invo/in# getAll<= on %$e previous resul%($e resul% o) %$e en%ire expression is %$e resul% ob%ained )rom invo/in#

getAll<=

($e expression can also be @ri%%en as TUbean:roo%=o)=allRes%ric%ions: 9one

Endpoint Evaluator

($e endpoin% evalua%or allo@s )or re%rieval o) proper%ies o) #lobal endpoin%s

Assume %$e )ollo@in# #lobal endpoin% declara%ion in a Mule con)i#ura%ion )ile:

... <endpoint name="myGlobalEndpoint" address="http://ivan.com/globalEndpoint" exchange-pattern="request-response">

</endpoint>...

Example expression: TUendpoin%:myClobalEndpoin% addressRe%rieves %$e FaddressG proper%y o) %$e #lobal endpoin% @i%$ %$e nameFmyClobalEndpoin%G

Res%ric%ions: "anno% be used in expression )il%ers1nly %$e FaddressG proper%y suppor%ed, as o) Mule 3 2

Exception9%#pe Evaluator

($e excep%ion&%ype evalua%or examines %$e excep%ion payload o) a messa#e %o de%ermine @$e%$er i%is o) a speci)ic %ype

Example expression: TUexcep%ion&%ype:4avax xml soap 1A*Excep%ionEvalua%es %o %rue i) %$e excep%ion payload o) a messa#e is o) %$e %ype

4avax.xml.soap.SOAP&xceptionRes%ric%ions: "an only be used in expression )il%ers

In Mule 3 ! and la%er, %$e )irs%&success)ulL rou%er can use %$eexcep%ion&%ype evalua%or

2;+

Page 290: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 290/354

Function Evaluator

($e )unc%ion evalua%or invo/es one o) %$e )unc%ions lis%ed belo@

Function Name Description

no@ .a%e and %ime @$en )unc%ion @as invo/ed in a 4ava.s l.'imestampob4ec%

da%e .a%e @$en )unc%ion @as invo/ed in a 4ava.util.+ate ob4ec%

da%es%amp %rin# represen%a%ion o) %$e %ime @$en )unc%ion @as invo/edrepresen%ed by a 4ava.util.+ate ob4ec% )orma%%ed @i%$ aSimple+ate,ormat .e)aul% da%e )orma%: dd&MM&yyP<<&mm&ssExample: TU)unc%ion:da%es%amp:dd&MM&yy

sys%ime ($e curren% sys%em %ime as re%urned byS$stem.current'imeMillis<=

uuid %rin# represen%a%ion o) an BBI.

$os%name ($e $os% name o) local$os%

ip ($e $os% address o) local$os%

coun% Re%rieves and incremen%s a coun%er #lobal %o %$e )unc%ion evalua%oro) a Mule ins%ance ($e coun%er s%ar%s )rom zero eac$ %ime %$e Muleserver is s%ar%ed

Example expression: TU)unc%ion:sys%imeRes%ric%ions: "anno% be used in expression )il%ers

4roov# Evaluator

($e Croovy evalua%or evalua%es expression usin# %$e Croovy lan#ua#e($e )ollo@in# proper%ies are available %o %$e Croovy scrip%:

Property Description

lo# -o##er ob4ec% in$eri%in# )rom %$eSL,3;Log class

resul% Resul% o) evalua%in# %$e Croovy scrip%

mule"on%ex% 1b4ec% implemen%in# %$e Mule ontext in%er)ace

payload *ayload o) curren% Mule messa#e

src *ayload o) curren% Mule messa#e

messa#e 1nly available i) %$e expression is evalua%ed @$en processin# a Mulemessa#e, %$en an ob4ec% implemen%in# %$e MuleMessage in%er)ace

re#is%ry 1b4ec% o) %$e %ype Mule#egistr$Helper

Example expression: TU#roovy:payload subs%rin#H ,8Ex%rac%s %$e )ive )irs% c$arac%ers o) %$e payload, assumin# i% is a s%rin#

Res%ric%ions: 9one

2+

Page 291: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 291/354

+eader Evaluator

($e $eader evalua%or allo@s access %o %$e speci)ied Mule messa#e $eader

Example expression: TU$eader:1B(>1B9.:my<eaderAccesses %$e op%ionally presen% messa#e $eader @i%$ %$e name Fmy<eaderGin %$e 1B(>1B9. scope

($e presence o) a $eader is made op%ional by appendin# F G %o i%s nameI) a $eader is no% presen%, an excep%ion @ill be %$ro@n i) %$e name does no%end @i%$ F G

Res%ric%ions: 9one

+eaders Evaluator

($e $eaders evalua%or allo@s re%rieval o) mul%iple Mule messa#e $eaders, placin# %$e resul% in amap ob4ec%

Example expression: TU$eaders:1B(>1B9.:my<eader! , 1B(>1B9.:my<eader2Re%rieves %$e op%ionally presen% $eaders @i%$ %$e names Fmy<eader!G andFmy<eader2G bo%$ in %$e 1B(>1B9. scope($e presence o) a $eader is made op%ional by appendin# F G %o i%s nameI) a $eader is no% presen%, an excep%ion @ill be %$ro@n i) %$e name does no%end @i%$ F GF G can be used as a @ildcard, re%rievin# all messa#e $eaders in a cer%ainscope($e de)aul% scope is 1B(>1B9.

Res%ric%ions: "anno% be used in expressions )il%ers

+eaders9$ist Evaluator

($e $eaders evalua%or allo@s re%rieval o) mul%iple Mule messa#e $eaders, placin# %$e resul% in a lis%ob4ec%

Example expression: TU$eaders&lis%:1B(>1B9.:my<eader! , 1B(>1B9.:my<eader2Re%rieves %$e op%ionally presen% $eaders @i%$ %$e names Fmy<eader!G andFmy<eader2G bo%$ in %$e 1B(>1B9. scope($e presence o) a $eader is made op%ional by appendin# F G %o i%s nameI) a $eader is no% presen%, an excep%ion @ill be %$ro@n i) %$e name does no%end @i%$ F GF G can be used as a @ildcard, re%rievin# all messa#e $eaders in a cer%ainscope

($e de)aul% scope is 1B(>1B9.Res%ric%ions: "anno% be used in expressions )il%ers

2+!

Page 292: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 292/354

-S:" Evaluator

($e ? 19 evalua%or allo@s evalua%ion o) *a%$&li/e expressions a#ains% Mule messa#e payloadcon%ainin# ? 19 da%a, ex%rac%in# con%en%s o) nodesAssume, )or ins%ance, %$e )ollo@in# ? 19 da%a:

{"person":{"name":"Alistair Smith"}}

Also assume %$e )ollo@in# expression )ed %o %$e ? 19 evalua%or:

#[json://person/name]

($e resul% produced by %$e ? 19 evalua%or @ould be FAlis%air mi%$G H@i%$ou% 0uo%es

Example expression: TU4son:==person=nameRe%rieves %$e con%en%s o) %$e node FnameG bein# a c$ild o) %$e node FpersonG)rom %$e ? 19 messa#e payloadRe%urns null i) a node in %$e expression is no% presen%I% is also possible %es%in# @$e%$er %$e con%en%s o) a ? 19 node is e0ual %o or no% e0ual %o some value usin# expressions li/e %$is:

TU4son:==person=name W Alis%air mi%$Res%ric%ions: 9one

-S:"9"ode Evaluator

($e ? 19&9ode evalua%or allo@s evalua%ion o) pa%$&li/e expressions a#ains% Mule messa#e payload con%ainin# ? 19 da%a, ex%rac%in# %$e?ac/son node ob4ec%

Assume, )or ins%ance, %$e )ollo@in# ? 19 da%a:

{"person":{"name":"Alistair Smith"}}

Also assume %$e )ollo@in# expression )ed %o %$e ? 19 evalua%or:

#[json://person/]($e resul% produced by %$e ? 19 evalua%or @ould be an ob4ec% o) %$e %ypeOb4ect1ode con%ainin#%$e %ex% node FnameG

Example expression: TU4son:==person=Re%rieves %$e con%en%s o) %$e node FpersonG as a ?ac/son ob4ec% )rom %$e? 19 messa#e payloadRe%urns null i) a node in %$e expression is no% presen%

Res%ric%ions: Available in Mule 3 ! and la%er

-0'ath Evaluator

($e ? *a%$ evalua%or is depreca%ed and s$ould no% be used As al%erna%ives, consider %$e>eanEvalua%or discussed earlier or %$e *a%$ evalua%or %$a% @ill be in%roduced la%er

2+2

Page 293: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 293/354

Map9'a#load Evaluator

($e map&payload evalua%or re%rieves %$e value associa%ed @i%$ a cer%ain /ey )rom a Mule messa#e payload o) %$e %ype 4ava.util.Map

Example expression: TUmap&payload:/evinKey,nive/

Re%rieves %$e values associa%ed @i%$ %$e /eys F/evinKeyG and Fnive/G )rom%$e Map ob4ec% bein# %$e Mule messa#e payload and re%urns a map con%ainin#%$e %@o /ey&value pairsI) one sin#le /ey is supplied, %$e ob4ec% %ype @ill be %$a% o) %$e value ob4ec%I) mul%iple /eys are supplied, %$e resul% @ill be a map @i%$ %$e /ey&value

pairs inser%edRe0ues%in# %$e value o) a /ey no% presen% in %$e map @ill #enera%e anexcep%ion($e presence o) /eys can be made op%ional by appendin# F G %o %$e nameo) %$e /ey

Res%ric%ions: "anno% be used @i%$ expression )il%ers

Message Evaluator

($e messa#e evalua%or allo@s )or re%rieval o) proper%ies on Mule messa#es ($e )ollo@in# proper%ies are available:

Property Description

id Bni0ue id o) %$e messa#e

correla%ionId "orrela%ion id o) %$e messa#e, or null i) no% se%

correla%ionCroup ize 9umber o) messa#es in a correla%ion #roup, or

&! i) no% /no@ncorrela%ion e0uence e0uence or orderin# number )or %$e messa#e,

or &! i) orderin# is no% si#ni)ican%

reply(o Reply&%o address )or %$e messa#e, or null

payload Messa#e payload

encodin# Encodin# )or %$e messa#e, or %$e de)aul%encodin#

excep%ion Excep%ion&payload o) %$e messa#e or null

Example expression: TUmessa#e:payloadRe%rieves %$e payload o) %$e curren% Mule messa#e

Res%ric%ions: "anno% be used @i%$ expression )il%ers

2+3

Page 294: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 294/354

:4"$ Evaluator

($e 1C9- evalua%or uses 1C9- as %$e expression lan#ua#e ($e supplied 1C9- expression isapplied %o %$e payload o) %$e Mule messa#e

Example expression: TUo#nl:roo% o) all e0ualsH'evil'

Examines @$e%$er %$e FallG proper%y o) %$e Fo)G proper%y o) %$e Froo%G proper%y o) %$e Mule messa#e payload is e0ual %o FevilGI) %$e payload is cons%ruc%ed as s$o@n in %$e bean evalua%or example above,%$e example expression @ill evalua%e %o %rue

Res%ric%ions: 9one

'a#load Evaluator

($e payload evalua%or re%rieves %$e payload o) %$e curren% Mule messa#e, op%ionally %rans)ormin# i%%o %$e supplied %ype

Example expressions: TUpayload:by%eU , TUpayload:4ava lan# %rin#

Re%rieves %$e payload o) %$e curren% Mule messa#e and, in %$e )irs% example,%rans)orms i% %o a by%e array In %$e second example, %$e payload is%rans)ormed %o a s%rin#

Res%ric%ions: "anno% be used @i%$ expression )il%ers

'a#load9%#pe Evaluator

($e payload&%ype evalua%or allo@s )or )il%erin# o) Mule messa#es @i%$ a cer%ain %ype o) payload

Example expression: expression&)il%er expressionWX4ava u%il MapX evalua%orWXpayload&%ypeX=L

Accep%s only Mule messa#es @$ic$ payload is o) %$e %ype 4ava.util.Map

Res%ric%ions: "an only be used @i%$ expression )il%ers

'rocessor Evaluator

($e processor evalua%or enables invoca%ion o) a speci)ied #lobal messa#e processor @i%$ %$e resul%o) %$e nes%ed expression as applied on %$e curren% Mule messa#eA messa#e processor can be o) %$e )ollo@in# %ypes:

• "omponen%• (rans)ormer•

"us%om processor• *rocessor c$ain• 6lo@

Example expression: TUprocess:my*rocessor:$eader:I9>1B9.:Mule*roper%y2

Applies %$e processor @i%$ %$e name Fmy*rocessorG %o %$e value o) %$e Mulemessa#e proper%y in %$e inbound scope named FMule*roper%y2G

Res%ric%ions: Available in Mule 3 ! and la%er

2+5

Page 295: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 295/354

Regex Evaluator

($e re#ex evalua%or allo@s )or usin# re#ular expressions in expression )il%ers

Example expression: expression&)il%er expressionWXbUa&z X evalua%orWXre#exX=LAccep%s only Mule messa#es @$ic$ payload con%ains a% leas% one @ord %$a%s%ar%s @i%$ %$e c$arac%er FbG

Res%ric%ions: "an only be used @i%$ expression )il%ers

String Evaluator

($e s%rin# evalua%or ma/es i% possible %o combine mul%iple expression and %ex% ($e expressionscon%ained by %$e s%rin# evalua%or @ill be evalua%ed and %$e resul% inser%ed in%o %$e s%rin#

Example expression: TUs%rin#: ($e payload: TUpayload:4ava lan# %rin#"ons%ruc%s a s%rin# consis%in# o) F($e payload: F @i%$ %$e Mule messa#e

payload conca%ena%ed

Res%ric%ions: 9one %o %$e s%rin# evalua%or "on%ained expressions re%ain %$eir res%ric%ions

ariable Evaluator

($e variable evalua%or can be used %o re%rieve )lo@ variables 9o%e %$a% %$e expression enric$er @i%$%$e same name loo/s iden%ical %o %$e variable evalua%or bu% is used %o s%ore values in )lo@ variables

%$e con%ex% de%ermines @$e%$er i% is %$e variable evalua%or or %$e variable expression enric$er

Example expression: TUvariable:my6lo@VariableRe%rieves %$e con%en%s o) %$e )lo@ variable @i%$ %$e name Fmy6lo@VariableG

Res%ric%ions: Available in Mule 3 ! and la%er

($e )ollo@in# is an example o) a variable expression enric$er expression used %o s%ore %$e resul%)rom %$e )lo@ @i%$ %$e name Fmy ub6lo@G in %$e )lo@ variable @i%$ %$e name Fmy6lo@VariableG:...<enricher target="#[variable:myFlowVariable]"> <flow-ref name="mySubFlow"/></enricher>...

2+8

Page 296: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 296/354

Wildcard Evaluator

($e @ildcard evalua%or ma%c$es one or more s%rin#s %$a% con%ains @ildcards %o %$e s%rin#represen%a%ion o) %$e Mule messa#e payload or %$e %ype o) %$e payload ob4ec%

($e )ollo@in# @ildcards can be used:•

Ma%c$es any c$arac%er se0uence I) %$e en%ire @ildcard expression is F G, %$en ma%c$esany%$in#

I) %$e en%ire @ildcard expression is F G, %$en ma%c$es any%$in#• \

*receded by a classname, )or ins%ance F4ava lan# %rin#\G, ma%c$es %$e %ype o) ob4ec% bein#%$e Mule messa#e payload

Example expression: expression&)il%er expressionWXb X evalua%orWX@ildcardX=LRes%ric%ions: "an only be used @i%$ expression )il%ers

0'ath Evaluator

($e *a%$ evalua%or selec%s %$e %ex% o) one or more ma%c$in# nodes I) %$ere is only a sin#le ma%c$,%$e resul% @ill be a s%rin# I) %$e resul% con%ains more %$an a sin#le ma%c$, %$e resul% @ill be a lis% o)s%rin#s($e evalua%or opera%es on %$e )ollo@in# payload %ypes:

• %rin#%rin# payloads are conver%ed %o .1M5? documen%s

• 3" .ocumen% ob4ec%• .1M5? Elemen% ob4ec%• .1M5? .ocumen% ob4ec%

Example expression: TUxpa%$:==animal=nameRe%rieves %$e con%en%s o) %$e nameL elemen%Hs %$a% are c$ildren o) %$e

animalL elemen%, @$ic$ is a roo% elemen%

Res%ric%ions: 9one

2+7

Page 297: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 297/354

0'ath9"ode Evaluator

($e *a%$&node evalua%or selec%s one or more ma%c$in# nodes I) %$ere is only a sin#le ma%c$, %$eresul% @ill be a 3" or .1M5? .ocumen%, dependin# on %$e inpu% %ype I) %$e resul% con%ains more%$an a sin#le ma%c$, %$e resul% @ill be a lis% o) %$e a)oremen%ioned %ypes($e evalua%or opera%es on %$e )ollo@in# payload %ypes:

• %rin#%rin# payloads are conver%ed %o .1M5? documen%s

• 3" .ocumen% ob4ec%• .1M5? Elemen% ob4ec%• .1M5? .ocumen% ob4ec%

Example expression: TUxpa%$&node:==animal=nameRe%rieves %$e nameL elemen%Hs %$a% are c$ildren o) %$e

animalL elemen%, @$ic$ is a roo% elemen%

Res%ric%ions: 9one

2+

Page 298: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 298/354

. "otificationsAs men%ioned in %$e c$ap%er onMule 9o%i)ica%ions, %$e no%i)ica%ion mec$anism in Mule is similar %o%$e1bserver desi#n pa%%ern ($ere are a number o) pre&de)ined %ypes o) no%i)ica%ion even%s %$a% aresen% ou% in connec%ion %o di))eren% even%s in Mule >elo@ is a lis% o) %$ese no%i)ica%ion even% %ypes as@ell as a lis% o) %$e in%er)aces available @$en implemen%in# no%i)ica%ion lis%eners %$a% receives %$ese)ac%ory no%i)ica%ions

Apar% )rom %$e available no%i)ica%ions, Mule also suppor%s implemen%in# cus%om no%i)ica%ions & please re)er %o %$e Mule documen%a%ion )or de%ails

, 1 9otification E$ent T<pesIn Mule, bo%$ versions 2 x and 3 x, %$e )ollo@in# pre&de)ined no%i)ica%ion even% %ypes are available:

Notification Event Type Description

"19(E ( Mule con%ex% s%ar%ed, s%opped e%c

M1.E- Model li)ecycle s%a%e c$an#ed or componen%s in %$e modelre#is%ered or unre#is%ered

ERVI"E ervice s%ar%ed, s%opped, e%c

E"BRI(D Re0ues% )or au%$oriza%ion occurred

E9.*1I9(&ME ACE Messa#e received or sen% by endpoin%

"1M*19E9(&ME ACE Messa#e processed by componen%

MA9ACEME9( %a%e o) Mule ins%ance or i%s resources c$an#ed

"199E"(I19 "onnec%or connec%ed %o, released connec%ion or )ailed

connec%ion a%%emp% %o resourceRECI (RD Even% occurred on %$e Mule re#is%ry

"B (1M "us%om no%i)ica%ion

E "E*(I19 An excep%ion @as %$ro@n

(RA9 A"(I19 (ransac%ion be#un, commi%%ed or rolled bac/

R1B(I9C A rou%in# even% occurred

($e no%i)ica%ion even% %ype values lis%ed above are %$e values %$a% are %o be used in ei%$er %$e

no%i)ica%ionL elemen%s or in disable&no%i)ica%ionL elemen%s ($e sec%ion-is%enin# %o 9o%i)ica%ions belo@ s$o@ $o@ %o con)i#ure no%i)ica%ions in %$e Mule con)i#ura%ion )ile

2+;

Page 299: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 299/354

, " 9otification Listener %nterfaces 9o%i)ica%ion lis%eners are observers %$a% receive no%i)ica%ions @$en cer%ain %ypeHs o) even%s occur>y implemen%in# one o) %$e no%i)ica%ion lis%ener in%er)aces lis%ed belo@, %$e lis%ener selec%s @$a%/ind o) even%s %o receive Implemen%in#, )or ins%ance, %$eServer1otificationListener in%er)ace @illresul% in %$e lis%ener receivin# all %ypes o) even%s in %$e Mule server

$en usin# Mule 3 x, #enerics are used %o speci)y @$ic$ %ype o) no%i)ica%ions %$e lis%ener accep%s($e )ollo@in# in%er)aces are available @$en implemen%in# no%i)ica%ion lis%eners

Interface Notification EventType

Notes

erver9o%i)ica%ion-is%ener & Ances%or %o all no%i)ica%ion lis%ener in%er)acesReceives no%i)ica%ions @$en even%s occur inserver, model and componen%s

Messa#e9o%i)ica%ion-is%ener & Mule 2 x onlyAnces%or %o componen% and endpoin%messa#e no%i)ica%ion lis%enersReceives no%i)ica%ion @$en even%s are sen% orreceived by, or in, %$e Mule server

"omponen%Messa#e9o%i)ica%ion-is%ener "1M*19E9(&ME ACE Receives no%i)ica%ion @$en messa#e is processed by componen%

"onnec%ion9o%i)ica%ion-is%ener "199E"(I19 Receives no%i)ica%ion @$en connec%ores%ablis$es connec%ion, releases connec%ionor connec%ion a%%emp% %o resource )ails

"us%om9o%i)ica%ion-is%ener "B (1M "us%om no%i)ica%ion

Endpoin%Messa#e9o%i)ica%ion-is%ener E9.*1I9(&ME ACE Receives no%i)ica%ion @$en messa#e receivedor sen% by endpoin%

Excep%ion9o%i)ica%ion-is%ener E "E*(I19 Receives no%i)ica%ion @$en an excep%ion @as%$ro@n

6unc%ional(es%9o%i)ica%ion-is%ener & Mule 3 x onlyReceives no%i)ica%ions )rom ins%ances o)6unc%ional(es%"omponen% ee A*Idocumen%a%ion )or )ur%$er in)orma%ion

<ear%bea%9o%i)ica%ion-is%ener & Mule 3 x onlyReceives periodic no%i)ica%ions @$en %$eMule server is up and runnin#

Mana#emen%9o%i)ica%ion-is%ener MA9ACEME9( Receives no%i)ica%ion @$en s%a%e o) Muleins%ance or i%s resources c$an#ed

Messa#e*rocessor9o%i)ica%ion-is%ener & Mule 3 x onlyReceives no%i)ica%ions )rom messa#e

processors be)ore and a)%er processin# amessa#e

Model9o%i)ica%ion-is%ener M1.E- Receives no%i)ica%ion @$en model li)ecycles%a%e c$an#ed or componen%s in %$e modelre#is%ered or unre#is%ered

Mule"on%ex%9o%i)ica%ion-is%ener "19(E ( Receives no%i)ica%ion @$en Mule con%ex% iss%ar%ed, s%opped e%c

Re#is%ry9o%i)ica%ion-is%ener RECI (RD Receives no%i)ica%ion @$en even% occurred on%$e Mule re#is%ry

2++

Page 300: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 300/354

Remo%e.ispa%c$er9o%i)ica%ion-is%ener & Receives no%i)ica%ion @$en re0ues%s are sen%%o and received )rom remo%e Mule ins%ance

Rou%in#9o%i)ica%ion-is%ener R1B(I9C Receives no%i)ica%ion @$en a rou%in# even%occurred

ecuri%y9o%i)ica%ion-is%ener E"BRI(D Receives no%i)ica%ion @$en re0ues% )orau%$oriza%ion occurs

ervice9o%i)ica%ion-is%ener ERVI"E Receives no%i)ica%ion @$en service s%ar%ed,s%opped, e%c

ervle%"on%ex%9o%i)ica%ion-is%ener & Mule 3 x onlyReceives no%i)ica%ions @$en servle% con%ex%rela%ed %o a Mule ins%ance is ini%ialized ordes%royed His %$is implemen%ed

)%p(ranspor%9o%i)ica%ion-is%ener & Mule 3 x onlyReceives no%i)ica%ions o) 6(* opera%ions.oes no% $ave a correspondin# no%i)ica%ioneven% %ype

(ransac%ion9o%i)ica%ion-is%ener (RA9 A"(I19 (ransac%ion be#un, commi%%ed or rolled bac/

$en usin# Mule 2 x, %$e on1otification me%$od %a/es a parame%er o) %$e %ypeServer1otification($us all no%i)ica%ions @$ic$ ma%c$ %$e in%er)ace implemen%ed by %$e no%i)ica%ion lis%ener @ill bedelivered %o %$e no%i)ica%ion lis%ener Any )il%erin# on %$e no%i)ica%ion even% %ype mus% be done

pro#ramma%ically in %$e no%i)ica%ion lis%ener, usin# %$einstanceof ?ava /ey@ord

Bsin# Mule 3 x, %$e no%i)ica%ion lis%ener in%er)aces uses ?ava #enerics $en developin# ano%i)ica%ion lis%ener, @e are able %o speci)y %$e exac% %ype o) no%i)ica%ion even%s %$e lis%ener is %oreceive Mule @ill ensure %$a% only no%i)ica%ion even%s o) %$e appropria%e %ype are delivered %o %$elis%ener

6or an example s$o@in# $o@ %o implemen% a no%i)ica%ion lis%ener, please see %$e sec%ion-is%enin#%o 9o%i)ica%ions belo@

3

Page 301: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 301/354

, & 9otification E$ents 9o%i)ica%ion even% ob4ec%s are %$e %ype o) ob4ec%s delivered %oon1otification me%$ods o) no%i)ica%ionlis%eners All %ypes o) no%i)ica%ion even%s in Mule in$eri% )rom %$e class 4ava.util.&ventOb4ect and%$e %opmos% class in %$e no%i)ica%ion even% $ierarc$y in Mule isServer1otification ($e %able belo@lis%s all %$e de)aul% no%i)ica%ion even% classes in Mule*lease also re)er %o %$e above sec%ion on 9o%i)ica%ion -is%ener In%er)aces )or in)orma%ion on @$en%$e di))eren% /inds o) no%i)ica%ion even%s are sen%

Notification EventClass

Source Resource I entifier Notes

erver9o%i)ica%ion Roo% class o)no%i)ica%ion even%s

"omponen%Messa#e 9o%i)ica%ion

Mule messa#e bein# processed by %$ecomponen%=)lo@cons%ruc%

"omponen% name HMule 2 x6lo@ cons%ruc% name HMule 3 x

"onnec%ion9o%i)ica%ion "onnec%able resource "onnec%orPname or connec%orPname\G dispa%c$erHF\endpoin% BRI\G G or connec%orPname\G receiverHF\endpoin%BRI\G G or li)ecyclePob4ec%Pname or re%ryPcon%ex%Pdescrip%ion

"us%om9o%i)ica%ion Any ob4ec% Any s%rin#

Endpoin%Messa#e 9o%i)ica%ion

Mule messa#e sen% orreceived by %$eendpoin%

9ull, componen% or service nameHMule 2 x6lo@ cons%ruc% name HMule 3 x

Excep%ion9o%i)ica%ion Excep%ion %$ro@n 9ull HMule 2 x"lass name o) excep%ion roo% causeHMule 3 x

6lo@"ons%ruc%9o%i)ica%ion 6lo@ cons%ruc% name 6lo@ cons%ruc% name Mule 3 x only

Mana#emen%9o%i)ica%ion Mana#emen% ob4ec% %$a%%ri##ered no%i)ica%ion

9ull

Messa#e*rocessor 9o%i)ica%ion

MuleEven% ob4ec% 6lo@ cons%ruc% name Mule 3 x only

Model9o%i)ica%ion Model ob4ec% Model name

Mule"on%ex%9o%i)ica%ion MulePcon%ex%PdomainP id\G G\MulePcon%ex%Pclus%erPid\G G\MulePcon)i#ura%ionPid

ame as source HMule 2 xMule con%ex% con)i#ura%ion id HMule3 x

Re#is%ry9o%i)ica%ion Re#is%ry ob4ec% Mule re#is%ry id .oes no% seem %o beimplemen%ed innei%$er Mule 2 x norMule 3 x

Remo%e.ispa%c$er 9o%i)ica%ion

Mule messa#e Endpoin% Ho) remo%e Mule ins%ance %osend or receive messa#e %o=)rom

Rou%in#9o%i)ica%ion Mule messa#e 9ull or messa#e source BRI

ecuri%y9o%i)ica%ion Mule 2 x:

ecuri%yExcep%ion

Mule 2 x: ecuri%yExcep%ion s%rin#

represen%a%ion

3 !

Page 302: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 302/354

Page 303: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 303/354

• In %$e Mule con)i#ura%ion )ile, add a no%i)ica%ionsL elemen% i) no% already presen%($e no%i)ica%ionsL elemen% is an immedia%e c$ild elemen% %o %$e roo% muleL elemen%

<?xml version="1.0" encoding="UTF-8"?><mule ...> ...

<!-- Add the dynamic=”true” attribute if notification listeners are to be added at runtime, after having started the Mule context. --> <notifications> ... </notifications>

...</mule>

• In %$e no%i)ica%ionsL elemen%, add a no%i)ica%ionL elemen% as s$o@n belo@($is elemen% %ells Mule %$a% %$e applica%ion @an%s %o lis%en )or "1M*19E9(&ME ACEno%i)ica%ions

... <notifications> <notification event="COMPONENT-MESSAGE"/> ... </notifications> ...

• In %$e no%i)ica%ionsL elemen%, add a no%i)ica%ion&lis%enerL elemen%, as belo@($is elemen% re#is%er a no%i)ica%ion lis%ener @i%$ Mule ($e in%er)aceHs implemen%ed by %$elis%ener class decides %$e /ind o) no%i)ica%ion even%s %$a% @ill be sen% %o %$e lis%ener($e value o) %$e re) a%%ribu%e con%ains %$e name o) %$e prin# bean @$ic$ is %o be %$eno%i)ica%ion lis%ener

... <notifications> <notification event="COMPONENT-MESSAGE"/> <notification-listener ref="notificationListener1"/> </notifications> ...

, * Listening to 9otifications from a Specific ComponentAs per de)aul%, no%i)ica%ions )rom all sources are repor%ed %o a re#is%ered no%i)ica%ion lis%ener I% is

possible %o limi% %$e repor%ed no%i)ica%ions %o %$ose ori#ina%in# )rom a speci)ic componen% only byusin# %$e subscription a%%ribu%e in %$e no%i)ica%ion&lis%enerL elemen%($e value o) %$e a%%ribu%e con%ains %$e name o) %$e componen%, )or ins%ance a service or a )lo@, %$a%is %o be %$e only source o) no%i)ica%ions %o %$e lis%enerExample: ... <notifications> <notification event="COMPONENT-MESSAGE"/> <notification-listener ref="notificationListener1" subscription="GreetingFlow"/> </notifications> ...

($e @ildcard F G can be used in %$e subscription a%%ribu%e value, %$a% is F 6lo@ G @ill causeno%i)ica%ions )rom any componen% @$ic$ name con%ains F6lo@G %o be accep%ed

3 3

Page 304: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 304/354

, , Disabling 9otificationsI% is possible %o disable %$e delivery o) no%i)ica%ions usin# %$e disable&no%i)ica%ionL elemen% ($e)ollo@in# op%ions are available:

• .isablin# no%i)ica%ions %o lis%eners implemen%in# a cer%ain in%er)ace

Bse %$einterface-class a%%ribu%e o) %$e disable&no%i)ica%ionL elemen% and speci)y anin%er)ace ... <notifications> ... <disable-notification interface-class= "org.mule.api.context.notification.EndpointMessageNotificationListener"/> </notifications> ...

• .isablin# cer%ain no%i)ica%ion even% %ypes

Bse %$einterface a%%ribu%e o) %$e disable&no%i)ica%ionL elemen% and speci)y %$e no%i)ica%ion%ype even% %o be disabled

... <notifications> ... <disable-notification interface="ENDPOINT-MESSAGE"/> </notifications> ...

3 5

Page 305: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 305/354

, 2 +egistering a 9otification Listener Programmaticall< 9o%i)ica%ion lis%eners can also be re#is%ered pro#ramma%ically, as @ill be s$o@n in %$e )ollo@in#exampleIn order %o be able %o re#is%er no%i)ica%ion lis%eners a)%er %$e Mule con%ex% $as been s%ar%ed, %$ed$namic a%%ribu%e on %$e no%i)ica%ionsL elemen% in %$e Mule con)i#ura%ion )ile mus% be se% %o %rue

... <notifications dynamic="true"> ... </notifications> ...

($e )ollo@in# example code s$o@s $o@ %o pro#ramma%ically re#is%er a no%i)ica%ion lis%ener ($e)il%erin# on %$e componen% name is op%ional

/* * Programmatically register a notification listener on the * Mule context retrieved from the current Mule message. * Note that in order to be able to register notification * listeners when the Mule context is started, the * attribute dynamic on the <notifications> element in the * Mule configuration must be set to true. */MuleMessage theMuleMsg = inNotification.getSource();MuleContext theMuleContext = theMuleMsg.getMuleContext();

/* Create the new listener instance to be registered. */MyMsgNotificationListener3 theNewListener = new MyMsgNotificationListener3();

try{ /* * The second parameter to the registerListener method * adds filtering, causing the notification listener * only to accept notifications originating components

* with names matching the supplied name. * Note how the * wildcard may be used. */ theMuleContext.registerListener(theNewListener, "*Service*");} catch (final NotificationException theException){ theException.printStackTrace();}

9o%e %$a%:• Accordin# %o %$e Mule documen%a%ion, %$ed$namic a%%ribu%e o) %$e no%i)ica%ionsL elemen%

mus% be se% %o %rue, in order %o be able %o re#is%er no%i)ica%ion lis%eners on a Mule con%ex% %$a%$as been s%ar%ed

• ($ere are %@o versions o) %$eregisterListener me%$od on %$e Mule ontext in%er)ace>o%$ me%$ods %a/e a no%i)ica%ion lis%ener ins%ance as parame%er In addi%ion, %$e secondme%$od %a/e a s%rin# parame%er ($is s%rin# $as %$e same )unc%ion as %$e subscrip%iona%%ribu%e on %$e no%i)ica%ion&lis%enerL elemen%, allo@in# us %o speci)y %$e name o)componen%s )orm @$ic$ no%i)ica%ions @ill be accep%ed $en speci)yin# %$is name, %$e@ildcard F G can be used, as seen in %$e above example

3 8

Page 306: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 306/354

. Mule -M0 Manage&ent ($is sec%ion con%ains re)erence o) %$e Mule ?M mana#emen% op%ionsJ )irs% @e'll loo/ a% %$e op%ionsavailable @$en usin# Mule 2 x and %$en @e @ill loo/ a% %$e addi%ional op%ions made available @$enusin# Mule 3 x

(o loo/ a% %$e ?M mana#emen% op%ions on your o@n compu%er, you can use %$e example pro#ramin %$eMoni%orin# Mule c$ap%er in par% one o) %$is boo/

Mule 2.x -M0 Manage&ent

In %$is sec%ion @e @ill loo/ more closely a% %$e ?M mana#emen% op%ions available @$en runnin# aMule 2 x server ins%ance

• Expand %$e domain @i%$ %$e name s%ar%in# @i%$ FMuleG in %$e ?"onsole @indo@($e resul% s$ould loo/ li/e %$is:

Examinin# a Mule 2 x server node in ?"onsole

($e names o) %$e c$ildren s$ould #ive a #ood indica%ion o) @$a% /ind o) mana#emen% da%a can be)ound under %$e c$ild e @ill loo/ a% eac$ c$ild excep% )or %$e FMx54<%%pAdap%erG M>ean

(ule " x Ser$er >lobal Configuration

($e node or# mule "on)i#ura%ion con%ains %$e Clobal"on)i#ura%ion M>ean %$a% re)lec%s %$e #lobal

con)i#ura%ion o) %$e Mule ins%ance All %$e in)orma%ion in %$is M>ean is read&only• Expand %$e For# mule "on)i#ura%ionG node in ?"onsole and all i%s c$ildren

($e resul% s$ould loo/ li/e %$is:

3 7

Page 307: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 307/354

Mule 2 x server #lobal con)i#ura%ion moni%orin# in ?"onsole

e can see %$e )ollo@in# a%%ribu%es bein# available in %$e Clobal"on)i#ura%ion M>ean:

!ttri"ute Name #rita"le Description

Encodin# no Clobal messa#e encodin# Ex B(6&;

or/in#.irec%ory no Mule ins%ance's @or/in# direc%ory

ync$ronous no Ma/es all endpoin%s sync$ronous i) %rue

(ransac%ion(imeou% no .e)aul% %ransac%ion %imeou% in milliseconds

ync$ronousEven%(imeou% no .e)aul% %imeou% )or sync$ronous even%s inmilliseconds

3

Page 308: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 308/354

(ule " x Connectors Configuration

($e node or# mule "onnec%or allo@s us %o vie@ %$e con)i#ura%ion o) %$e curren% connec%ors in %$eMule ins%ance and %o apply cer%ain opera%ions %o a connec%or

• Expand %$e For# mule "onnec%orG node in ?"onsole

Also expand i%s )irs% c$ild M>ean, %$e " 6 connec%or, and i%s a%%ribu%es and opera%ionsnodes($e resul% s$ould loo/ li/e %$is:

Mule 2 x connec%or con)i#ura%ion moni%orin# in ?"onsole

e see %$a% %$e )ollo@in# a%%ribu%es are available )or a connec%or:

!ttri"ute Name #rita"le Description

9ame no 9ame o) %$e connec%or Fconnec%or cx) !G in %$is

case*ro%ocol no 9ame o) pro%ocol used by %$e connec%or

.isposed no "onnec%or disposed )la#

%ar%ed no "onnec%or s%ar%ed )la#

Excep%ion-is%ener no Excep%ion lis%ener bein# no%i)ied o) excep%ionsoccurrin# in %$e connec%or

3 ;

Page 309: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 309/354

($e )ollo@in# opera%ions are available on a connec%or:

$peration Name Description

dispose -i)e&cycle me%$od )reein# resources associa%ed @i%$ %$e connec%or

s%ar%"onnec%or %ar%s %$e connec%or

s%op"onnec%or %ops %$e connec%orini%ialize -i)e&cycle me%$od ini%ializin# %$e connec%or, alloca%in# any resources

ee also %$e A*I documen%a%ion o) %$eorg.mule.api.transport. onnector in%er)ace

(ule " x En/point Configuration

($e node or# mule Endpoin% allo@s us %o vie@ %$e con)i#ura%ion o) endpoin%s in %$e Mule ins%anceand %o apply cer%ain opera%ions %o endpoin%s

• Expand %$e For# mule Endpoin%G node in ?"onsoleAlso expand i%s )irs% c$ild M>ean, %$e endpoin% @i%$ %$e nameFendpoin% $%%p local$os% ; ;! services <ello erviceG, and i%s a%%ribu%es and opera%ionsnodes ($e resul% s$ould loo/ li/e %$is:

Mule 2 x server endpoin% con)i#ura%ion moni%orin# in ?"onsole

3 +

Page 310: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 310/354

e see %$a% %$e )ollo@in# a%%ribu%es are available )or an endpoin%:

!ttri"ute Name #rita"le Description

Address no Address o) endpoin%

9ame no 9ame o) endpoin%

"onnec%ed no Endpoin% connec%ed )la#Inbound no Endpoin% inbound )la#

ync$ronous no Endpoin% sync$ronous )la#

"omponen%9ame no 9ame o) service componen% con%ainin# %$eendpoin%

1u%bound no 1u%bound direc%ion endpoin% )la#

($e )ollo@in# opera%ions are available on an endpoin%:

$peration Name Description

connec% "onnec%s %$e endpoin%

disconnec% .isconnec%s %$e endpoin%

3!

Page 311: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 311/354

(ule " x (o/el Configuration

($e node or# mule Model allo@s us %o vie@ %$e con)i#ura%ion o) models in %$e Mule ins%ance and %oapply cer%ain opera%ions %o %$ese models ($e )irs% model, %$e F<elloModelHseda G corresponds %o%$e modelL elemen% in %$is c$ap%er's Mule 2 x con)i#ura%ion )ile F edaG s%ands )or Fs%a#ed even%&driven arc$i%ec%ureG and is a desi#n used in%ernally by Mule

• Expand %$e For# mule ModelG node in ?"onsoleAlso expand i%s )irs% c$ild M>ean, @i%$ %$e name F<elloModelHseda G, and %$e A%%ribu%esand 1pera%ions nodes o) %$e M>ean($e resul% s$ould loo/ li/e %$is:

Mule 2 x server model con)i#ura%ion moni%orin# in ?"onsole

A model $as %$e )ollo@in# a%%ribu%es:

!ttri"ute Name #rita"le Description

9ame no 9ame o) %$e node Ma%c$es %$e name o) %$emodelL elemen%

(ype no (ype o) model "ommonly FsedaG & s%a#ed even%&driven arc$i%ec%ure

($e )ollo@in# opera%ions are available on a model:

$peration Name Description

s%ar% %ar%s %$e model

s%op %ops %$e model

%ar%in# and s%oppin# %$e model in our example does no% seem %o yield any reac%ion

3!!

Page 312: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 312/354

(ule " x Context Configuration

9ex% up is Mule con%ex%s, )ound in %$e or# mule Mule"on%ex% node In %$is node @e can )ind aMule erverIn)o M>ean @$ic$ $olds in)orma%ion abou% %$e Mule server and %$e environmen% i% runsin

• Expand %$e For# mule Mule"on%ex%G node in ?"onsole

Also expand i%s )irs% c$ild node, %$e Mule erverIn)o M>ean, and i%s A%%ribu%es and1pera%ions nodes($e resul% s$ould loo/ li/e %$is:

Mule 2 x server in)orma%ion moni%orin# in ?"onsole

3!2

Page 313: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 313/354

($e Mule erverIn)o M>ean $as %$e )ollo@in# a%%ribu%es:

!ttri"ute Name #rita"le Description

Version no Mule version runnin# on %$e server

"on)i#>uilder"lass9ame no Mule con)i#ura%ion builder classee %$e Au%o"on)i#ura%ion>uilder class in %$e

Mule 2 x A*I documen%a%ion )or de%ails

%ar%(ime no .a%e ob4ec% $oldin# s%ar% %ime o) server

Ini%ialised no erver ini%ialized )la#

%opped no erver s%opped )la#

erverId no erver id s%rin#

>uild9umber no Mule server build number

>uild.a%e no Mule server build da%e

Vendor no Vendor %$a% crea%ed %$e Mule server

6reeMemory no Available memory in %$e Mule server ins%ance

MaxMemory no Maximum memory in %$e Mule server ins%ance

(o%alMemory no (o%al memory in %$e Mule server ins%ance

<os%name no <os%name a% @$ic$ %$e Mule server ins%ance may be accessed

<os%Ip no <os% I* address a% @$ic$ %$e Mule server ins%ancemay be accessed

1sVersion no 1 version %$e Mule server ins%ance runs in

?d/Version no Version o) %$e ?.K on @$ic$ %$e Mule serverins%ances is run

"opyri#$% no Mule copyri#$% s%rin#

-icense no Mule license s%rin#

Ins%anceId no Mule ins%ance id s%rin#ame as erverId

($e )ollo@in# opera%ions are available on a Mule erverIn)o M>ean:

$peration Name Descriptions%ar% %ar%s %$e Mule con%ex%

s%op %ops %$e Mule con%ex% and all a#en%s.oes no% release alloca%ed resources, suc$ as %$reads, so %$e ?VM in@$ic$ %$e server is runnin# @ill no% %ermina%e

dispose %ops %$e Mule con%ex% and releases associa%ed resources($e ?VM in @$ic$ %$e server is runnin# @ill %ermina%e

%oppin# %$e Mule con%ex% s%ops all services and a#en%s runnin# on %$e Mule server e @ill no% beable %o moni%or or mana#e %$e Mule server once i%s con%ex% $as been s%opped, despi%e %$e ?VM in

3!3

Page 314: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 314/354

@$ic$ %$e server @as runnin# no% bein# %ermina%ed

.isposin# %$e Mule con%ex% s$u%s do@n %$e Mule server and releases resources ($e ?VM in @$ic$%$e server @as runnin# @ill be %ermina%ed, provided %$ere are no addi%ional resources

(ule " x 9otification Configuration

Mule provides a no%i)ica%ion mec$anism %$a% enables a#en%s and componen%s in %$e Mule server %o be no%i)ied abou% c$an#es in %$e Mule server 6or )ur%$er de%ails, please re)er %o %$e sec%ion onFMule erver 9o%i)ica%ionsG in %$e Mule user documen%a%ion

• Expand %$e For# mule 9o%i)ica%ionG node in ?"onsoleAlso expand i%s )irs% and second c$ild M>eans, %$e Mule9o%i)ica%ion>roadcas%er andMule9o%i)ica%ion-is%ener, and %$eir A%%ribu%es, 1pera%ions and 9o%i)ica%ions nodes asavailable($e resul% s$ould loo/ li/e %$is:

Mule 2 x no%i)ica%ion moni%orin# in ?"onsole

($e Mule9o%i)ica%ion>roadcas%er M>ean allo@s )or addin# and removin# no%i)ica%ion lis%eners($ese )ea%ures are more usable i) you are @ri%in# your o@n pro#ram usin# ?M %o moni%or a Muleserver inside a moni%orin# pro#ram, suc$ as ?"onsole, %$ere is li%%le @e can do($e Mule9o%i)ica%ion-is%ener M>ean main%ains a lis% o) no%i)ica%ions

($e Mule9o%i)ica%ion>roadcas%er M>ean $as %$e )ollo@in# a%%ribu%es:

!ttri"ute Name #rita"le Description

9o%i)ica%ionIn)o no An array o) 4avax.management.M"ean1otificationInfo ob4ec%s$oldin# in)orma%ion abou% %$e di))eren% %ypes o)no%i)ica%ions %$a% can be emi%%ed

3!5

Page 315: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 315/354

($e )ollo@in# opera%ions are available on a Mule9o%i)ica%ion>roadcas%er M>ean:

$peration Name Description

remove9o%i)ica%ion-is%ener

Removes a lis%ener )rom %$e M>ean

remove9o%i)ica%ion-is%ener

Removes a lis%ener )rom %$e M>ean

add9o%i)ica%ion-is%ener Adds a lis%ener %o %$e M>ean

($e Mule9o%i)ica%ion-is%ener M>ean $as %$e )ollo@in# a%%ribu%es:

!ttri"ute Name #rita"le Description

9o%i)ica%ions-is% no -is% o) no%i)ica%ions

-is% ize no ize o) %$e lis% o) no%i)ica%ions

3!8

Page 316: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 316/354

(ule " x Ser$ice Configuration

($e or# mule ervice node con%ains M>eans allo@in# us %o moni%or all %$e services in our Muledeploymen%

• Expand %$e For# mule erviceG node in ?"onsole

Also expand i%s )irs% c$ild, %$e F<ello erviceG M>ean, and i%s A%%ribu%es and 1pera%ionsnodes($e resul% s$ould loo/ li/e %$is:

Mule 2 x service moni%orin# in ?"onsole

3!7

Page 317: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 317/354

A service M>ean, in %$e above pic%ure F<ello erviceG, $as %$e )ollo@in# a%%ribu%es:!ttri"ute Name #rita"le Description

9ame no 9ame o) %$e service

%a%is%ics no 1b4ec% $oldin# service s%a%is%ics

%opped no 6la# indica%in# @$e%$er service is s%opped

%oppin# no 6la# indica%in# @$e%$er service is in %$e process o)s%oppin#

*aused no 6la# indica%in# @$e%$er service is paused

Sueue ize no ize o) 0ueue $oldin# even%s %o be processed by%$e service

SueuedEven%s no 9umber o) even%s 0ueued %o be processed by %$eservice

MaxSueue ize no Maximum number o) even%s in 0ueue

Avera#eSueue ize no (o%al number o) async$ronous even%s receiveddivided by %o%al number o) 0ueued even%s

yncEven%sReceived no 9umber o) sync$ronous even%s received

AsyncEven%sReceived no 9umber o) async$ronous even%s received

(o%alEven%sReceived no (o%al number o) even%s received

yncEven%s en% no 9umber o) sync$ronous even%s sen%

AsyncEven%s en% no 9umber o) async$ronous even%s sen%

Reply(oEven%s en% no 9umber o) reply&%o even%s sen%

Reply&%o even%s are messa#es @i%$ a reply&%o BRI%o @$ic$ %$e reply o) %$e messa#e is %o be sen%

(o%alEven%s en% no (o%al number o) even%s sen%

Execu%edEven%s no 9umber o) even%s execu%ed since las% componen%s%a%is%ics rese%

Execu%ionErrors no 9umber o) errors durin# execu%ion

6a%alErrors no 9umber o) errors lo##ed as )a%al errors.epends on %$e excep%ion $andlin# s%ra%e#y

MinExecu%ion(ime no $or%es% %ime in milliseconds spen% processin# an

even%MaxExecu%ion(ime no -on#es% %ime in milliseconds spen% processin# an

even%

Avera#eExecu%ion(ime no Avera#e %ime in milliseconds spen% processin# aneven%

(o%alExecu%ion(ime no (o%al execu%ion %ime in milliseconds spen% processin# messa#es

3!

Page 318: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 318/354

($e )ollo@in# opera%ions are available on a service M>ean:

$peration Name Description

resume Resume even% processin# )or %$e service

dispose %op and dispose o) %$e service

pause *ause even% processin# )or %$e service)orce %op 6orce s%op even% processin# )or %$e service

s%op %op %$e even% processin# service

s%ar% %ar% %$e even% processin# o) %$e service

clear %a%is%ics "lears s%a%is%ics )or %$e service 9o%e %$a% no% all a%%ribu%e values are rese%

$a% is %$e di))erence be%@een s%oppin# and pausin# a service, one may as/($is depends on %$e service, bu% )or %$e " 6 @eb service in %$is example %$e <((* lis%ener isclosed @$en %$e service is s%opped ($is is no% %$e cause @$en %$e service is paused %$e <((*lis%ener remains ac%ive @$ile %$e service is paused

3!;

Page 319: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 319/354

(ule " x Statistics Configuration

($e or# mule %a%is%ics node con%ains M>eans allo@in# us %o re%rieve s%a%is%ics abou% %$e services ina Mule ins%ance e%c

• Expand %$e For# mule %a%is%icsG node in ?"onsole

Also expand bo%$ i%s c$ild M>eans as @ell as all %$e c$ild nodes o) %$e All %a%is%ics M>ean($e resul% s$ould loo/ li/e %$is:

Mule 2 x s%a%is%ics moni%orin# in ?"onsole

($e All %a%is%ics M>ean $as one sin#le a%%ribu%e, Enabled, @$ic$ may be bo%$ read and @ri%%enBsin# %$is a%%ribu%e, %$e accumula%ion o) s%a%is%ics can be enabled or disabled durin# execu%ion o)%$e Mule server

In addi%ion %o %$e a%%ribu%e, %$e All %a%is%ics M>ean also $as a number o) opera%ions rela%ed %o alls%a%is%ics o) %$e Mule server:

$peration Name Description

prin%" V ummary Cenera%es a s%a%is%ics summary repor% in " V )orma%

prin%<%ml ummary Cenera%es a s%a%is%ics summary repor% in <(M- )orma% prin% ml ummary Cenera%es a s%a%is%ics summary repor% in M- )orma%

lo# ummary Cenera%es a s%a%is%ics summary repor% and ou%pu%s i% %o %$e lo#

clear "lears s%a%is%ics and rese%s %$e s%a%is%ics accumula%ion period 9o%e %$a% no% all a%%ribu%e values are rese%

($e <ello ervice M>ean $olds s%a%is%ics o) %$e <ello ervice service, mos% par%s o) @$ic$ @e $avealready seen in %$e earlier sec%ion on service con)i#ura%ion

($is concludes %$e sec%ion on Mule 2 x ?M mana#emen%

3!+

Page 320: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 320/354

Page 321: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 321/354

(ule & x -pplication Statistics

($e ca%e#ory con%ains an Fapplica%ion %o%alsG M>ean %$a% con%ains a%%ribu%es re)lec%in# a##re#a%eds%a%is%ics o) %$e applica%ion ($e di))eren% /inds o) s%a%is%ics available in %$is M>ean is also availablein %$e Fapplica%ion %o%alsG M>ean under %$e or# mule %a%is%ics node

Expand %$e FApplica%ionG node, i%s c$ild M>ean Fapplica%ion %o%alsG and all i%s c$ild nodesin ?"onsole ($e resul% s$ould loo/ li/e %$is:

Mule 3 x applica%ion level s%a%is%ics moni%orin# in ?"onsole

($e )ollo@in# a%%ribu%es are available in %$e Fapplica%ion %o%alsG M>ean:

!ttri"ute Name #rita"le Description

9ame no 9ame o) %$e M>ean Al@ays Fapplica%ion %o%alsG

(ype no (ype o) s%a%is%ics Al@ays FApplica%ionG

%a%is%ics no 1b4ec% $oldin# applica%ion s%a%is%ics

Avera#e*rocessin#(ime no Avera#e %ime in milliseconds spen% processin# aneven%

*rocessedEven%s no 9umber o) even%s processed by %$e applica%ion

Max*rocessin#(ime no -on#es% %ime in milliseconds spen% processin# aneven%

Min*rocessin#(ime no $or%es% %ime in milliseconds spen% processin# aneven%

(o%al*rocessin#(ime no (o%al %ime in milliseconds spen% processin# even%s

Execu%ionErrors no 9umber o) errors durin# execu%ion o) %$eapplica%ion

32!

Page 322: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 322/354

6a%alErrors no 9umber o) errors lo##ed as )a%al errors in %$eapplica%ion.epends on %$e excep%ion $andlin# s%ra%e#y

AsyncEven%sReceived no 9umber o) async$ronous even%s received by %$eapplica%ion

ync$Even%sReceived no 9umber o) sync$ronous even%s received by %$eapplica%ion

(o%alEven%sReceived no (o%al number o) even%s received by %$e applica%ion

($e )ollo@in# opera%ion is available )or %$e applica%ion:$peration Name Description

clear %a%is%ics "lears %$e s%a%is%ics o) %$e applica%ion Hall s%a%is%ics a%%ribu%es lis%edabove

(ule & x Ser$er >lobal Configuration

($e M>ean "on)i#ura%ion con%ains a%%ribu%es re)lec%in# %$e #lobal con)i#ura%ion o) %$e Muleins%ance *lease also re)er %o %$e sec%ion onMule 2 x erver Clobal "on)i#ura%ion above

• Expand %$e F"on)i#ura%ionG M>ean in ?"onsole and all i%s c$ild nodes($e resul% s$ould loo/ li/e %$is:

Mule 3 x server #lobal con)i#ura%ion moni%orin# in ?"onsole

322

Page 323: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 323/354

($ere are a )e@ ne@ a%%ribu%es, some o) @$ic$ are @ri%eable, allo@in# )or c$an#es in %$econ)i#ura%ion o) a runnin# Mule 3 x server:

!ttri"ute Name #rita"le Description

$u%do@n(imeou% no (ime in milliseconds %o @ai% )or messa#es bein# processed @$en s$u%%in# do@n ins%ance

.e)aul% is 8"on%ainerMode no 6la# indica%in# @$e%$er Mule ins%ance is runnin# in

con%ainer mode H%rue or in embedded mode H)alse

6ull %ac/(races yes I) )alse, cer%ain in%ernal Mule&rela%ed en%ries areremoved )rom s%ac/ %races %o increase readabili%y

%ac/(race6il%er yes "omma&separa%ed lis% o) pac/a#es %$a% are %o beremoved )rom sani%ized s%ac/ %races

"on%ainer mode means %$e ?VM in @$ic$ %$e Mule ins%ance is runnin# @ere s%ar%ed by launc$in#%$e Mule server

(ule & x Connectors Configuration

($e connec%ors node $as c$an#ed name )rom or# mule "onnec%or %o "onnec%or and one a%%ribu%e,Excep%ion-is%ener, $as been removed6or de%ails on %$e remainin# a%%ribu%es and opera%ions, please re)er %o %$e sec%ion onMule 2 x"onnec%ors "on)i#ura%ion above

323

Page 324: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 324/354

(ule & x En/point Configuration

($e endpoin%s node $as c$an#ed name )rom or# mule Endpoin% %o Endpoin% ($e ync$ronousa%%ribu%e $as been removed and a ne@ a%%ribu%e, Messa#eExc$an#e*a%%ern, $as been added($e or#aniza%ion o) %$e nodes and M>eans belo@ %$e Endpoin% node $as also c$an#ed sli#$%ly, ascan be seen in %$e pic%ure belo@6or de%ails on %$e remainin# a%%ribu%es and opera%ions, please re)er %o %$e sec%ion onMule 2 xEndpoin% "on)i#ura%ion above

• Expand %$e FEndpoin%G node in ?"onsoleAlso expand i%s )irs% c$ild node, %$e node @i%$ %$e name Fconnec%or $%%p G, %$e M>ean @i%$%$e name Fendpoin% $%%p local$os% ; ;! services <ello erviceG, and i%s a%%ribu%es andopera%ions nodes ($e resul% s$ould loo/ li/e %$is:

Mule 3 x endpoin% con)i#ura%ion moni%orin# in ?"onsole

9o%e also %$a% %$e immedia%e c$ild o) %$e Endpoin% node is %$e <ello6lo@ node ($is re)lec%s %$e)ac% %$a% %$e Mule 3 x version o) %$e example pro#ram uses a )lo@ ins%ead o) a model and a service

325

Page 325: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 325/354

(ule & x )lo4 Configuration

En%irely ne@ in Mule 3 x, compared %o Mule 2 x, is %$e 6lo@ node @$ic$ allo@s us %o vie@ somes%a%is%ics o) )lo@s in %$e Mule ins%ance

• Expand %$e F6lo@G node in ?"onsole

Also expand i%s c$ild M>ean F<ello6lo@G and i%s a%%ribu%es and opera%ions nodes ($e resul%s$ould loo/ li/e %$is:

Mule 3 x )lo@ con)i#ura%ion moni%orin# in ?"onsole

328

Page 326: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 326/354

Page 327: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 327/354

(ule & x Context Configuration

($e or# mule Mule"on%ex% node @e sa@ @$en explorin# %$e Mule 2 x con)i#ura%ion $as beenreplaced @i%$ a M>ean named Mule"on%ex% *robably due %o %$e )ac% %$a% one Mule ins%ance al@ays$as one sin#le con%ex%

9o c$an#es in %$e a%%ribu%es or opera%ions available in %$e M>ean $ave been made 6or de%ails, please re)er %o %$e sec%ion onMule 2 x "on%ex% "on)i#ura%ion above

(ule & x Statistics Configuration

In a Mule 3 x ins%ance, %$ere are no@ %@o di))eren% nodes rela%ed %o s%a%is%icsJ %$e or# mule %a%is%inode bu% also %$e %a%is%ics node

• Expand %$e For# mule %a%is%icsG and F %a%is%icsG nodes in ?"onsoleAlso expand bo%$ %$e All %a%is%ics and <ello6lo@ M>eans and all %$eir c$ildren($e resul% s$ould loo/ li/e %$is:

Mule 3 x s%a%is%ics moni%orin# in ?"onsole

($e All %a%is%ics M>ean con%ain %$e same a%%ribu%es and opera%ions, excep% )or %$e clear andlo# ummary opera%ions, as in Mule 2 x please re)er %o %$e sec%ion onMule 2 x %a%is%ics"on)i#ura%ion above )or de%ails

($e M>ean $oldin# s%a%is%ics )or %$e <ello6lo@ is loca%ed under %$e or# mule %a%is%ics node I%duplica%es %$e in)orma%ion @e sa@ in %$e above sec%ion onMule 3 x 6lo@ "on)i#ura%ion

9e@ in Mule 3 2 is %$e Fapplica%ion %o%alsG M>ean, @$ic$ con%ain s%a%is%ic in)orma%ion abou% %$een%ire applica%ion, as discussedearlier

32

Page 328: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 328/354

(ule & x 9otification Configuration

($e op%ions available concernin# no%i)ica%ion con)i#ura%ion and moni%orin# are iden%ical @i%$ %$oseavailable in Mule 2 x *lease re)er %o %$eMule 2 x 9o%i)ica%ion "on)i#ura%ion sec%ion above

($is concludes %$e sec%ion on Mule 3 x ?M mana#emen%

32;

Page 329: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 329/354

6. 'ac;age a Mule !pplicationAn applica%ion usin# Mule can be pac/a#ed in di))eren% @ays I) Mule runs embedded, %$a% iss%ar%ed pro#ramma%ically )rom %$e applica%ion, %$en %$e applica%ion can be pac/a#ed as a re#ular?ava applica%ion or ?ava @eb applica%ion e%cI), $o@ever, %$e applica%ion is %o be deployed %o a s%andalone Mule ins%ance or a Mule ins%ancerunnin# in a @eb con%ainer or in an applica%ion server, %$en applica%ion needs %o be pac/a#ed alon#%$e ins%ruc%ions ou%lined in %$is sec%ion

8 1 Package (ule " x -pplicationsMule 2 x applica%ions canno% be pac/a#ed as one sin#le uni%, @$en deployin# %o a s%andalone Mule2 x server 1ne Mule con)i#ura%ion )ile needs %o be /ep% separa%e, @$ile %$e res% o) %$e ar%i)ac%s, suc$as compiled classes, addi%ional con)i#ura%ion )iles e%c can be pac/a#ed in a re#ular ?AR )ile

*rior %o s%ar%in# %$e s%andalone Mule 2 x server, %$e ?AR )ile @i%$ addi%ional ar%i)ac%s mus% becopied %o %$e classpa%$ o) %$e Mule 2 x server ($e recommended loca%ion is %$e Flib=userG direc%ory

in %$e Mule $ome($e Mule con)i#ura%ion )ile can be loca%ed any@$ere, since @e can #ive %$e comple%e pa%$ %o %$econ)i#ura%ion )ile as a parame%er %o Mule @$en launc$in# %$e server

8 " Package (ule & x -pplicationsIn Mule 3 x, %$e FappsG direc%ory in %$e Mule $ome $as been in%roduced as %$e pre)erred loca%ion %o@$ic$ %o deploy Mule applica%ions Mule 3 x also suppor%s %$e deploymen% o) mul%iple applica%ionsrunnin# in one and %$e same s%andalone Mule server

A Mule 3 x applica%ion is pac/a#ed in a direc%ory @i%$ %$e )ollo@in# s%ruc%ure:

($e Mule 3 x applica%ion Fsoap@sinmule3G deployed %o %$e FappsG direc%ory in %$e Mule $ome

9o%e %$a%:• ($e name o) %$e direc%ory, Fsoap@sinmule3G in %$e above example, @ill become %$e name o)

%$e Mule applica%ion• ($e applica%ion direc%ory con%ains a Mule con)i#ura%ion )ile

($e name o) %$e )ile is Fmule&con)i# xmlG, @$ic$ enables Mule %o )ind i%• ($e applica%ion direc%ory con%ains a FlibG direc%ory

*lace all %$e library ?AR )iles con%ainin# %$e classes, con)i#ura%ion )iles e%c re0uired by %$eMule applica%ion in %$is direc%ory 1nly %$e ar%i)ac%s no% already presen% in %$e Mule serverneed %o be included $ere

• ($e FappsG direc%ory con%ains a )ile named Fsoap@sinmule3&anc$or %x%G($is )ile @ill appear a)%er %$e Mule applica%ion $as been success)ully deployed %o %$e Mule

32+

Page 330: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 330/354

server I% is used %o undeploy %$e applica%ionJ dele%e %$is )ile and Mule @ill undeploy %$eapplica%ion

7. %esting Mule con%ains some )ea%ures %$a% ma/es %es%in# o) Mule in%e#ra%ions si#ni)ican%ly easier ($ere is,

)or ins%ance, a special %es% componen% and a number o) uni% %es%&case classes

: 1 Exception Component($ere is no need %o implemen% a componen% %$a% %$ro@s excep%ions, unless %$ere are par%iculardemands Ins%ead %$e componen%L elemen% in %$e %es% namespace can be used, as s$o@n in %$e)ollo@in# example:

... <!-- This test component is will always throw a NullPointerException when invoked. --> <test:component throwException="true" exceptionToThrow="java.lang.NullPointerException" exceptionText="Exception thrown by test component."/>...

9o%e %$a%:• ($e %es% componen% is available in bo%$ Mule 2 x and Mule 3 x• ($e %es% componen% is implemen%ed by %$e class ,unctional'est omponent

*lease re)er %o %$e A*I documen%a%ion o) %$is class )or de%ailed in)orma%ion on %$ecapabili%ies o) %$e %es% componen%

($e thro(&xception a%%ribu%e o) %$e %es%:componen%L only allo@s )or s%a%ic declara%ions o)ei%$er %rue or )alse• Bsin# %$eexception'o'hro( a%%ribu%e, i% is possible %o speci)y %$e /ind o) excep%ion %$a% @ill

be %$ro@n @$en %$e componen% is invo/ed($e de)aul% excep%ion %ype is ,unctional'est&xception

• ($e exception'ext a%%ribu%e allo@s us %o speci)y %$e messa#e o) %$e excep%ion %$a% @ill be%$ro@n

• (es% componen%s %$a% are con)i#ured %o %$ro@ an excep%ion @ill never lo# messa#es

I) %$e above componen% is invo/ed, @e @ill see lo# ou%pu% similar %o %$is Hsome ou%pu% omi%%ed %o

conserve space :ERROR 2012-02-27 06:56:53,111 [connector.http.mule.default.receiver.02]com.ivan.exceptionhandlers.MyCustomExceptionHandler:********************************************************************************Message : Component that caused exception is:DefaultJavaComponent{MySimpleRegularService.commponent}. Message payload is of type: StringCode : MULE_ERROR--2--------------------------------------------------------------------------------Exception stack is:1. Exception thrown by test component. (java.lang.NullPointerException) sun.reflect.NativeConstructorAccessorImpl:-2 (null)2. Component that caused exception is: DefaultJavaComponent{MySimpleRegularService.commponent}.Message payload is of type: String (org.mule.component.ComponentException) org.mule.component.DefaultComponentLifecycleAdapter:359(http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/component/ComponentException.html)--------------------------------------------------------------------------------Root Exception stack trace:

... + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true ' for everything)

33

Page 331: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 331/354

********************************************************************************

33!

Page 332: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 332/354

: " +eturn (ock Data from a Component($e %es% componen% can be con)i#ured %o re%urn s%a%ic da%a e use %$e componen%L elemen% )rom%$e %es% namespace (o re%urn s%a%ic da%a speci)ied in %$e Mule con)i#ura%ion )ile, @e use %$e re%urn&da%aL elemen% )rom %$e %es% namespace:...

<!-- This test component will return the static data specified in the <test:return-data> child element. --> <test:component> <test:return-data>some data</test:return-data> </test:component>...

9o%e %$a%:• I% is possible %o useMule expressions in %$e re%urn&da%aL elemen%

Example: %es%:re%urn&da%aLTU)unc%ion:da%es%amp =%es%:re%urn&da%aL

An al%erna%ive is %o place %$e da%a %o be re%urned by %$e componen% in a )ile and use %$e file a%%ribu%eo) %$e re%urn&da%aL elemen%:... <!-- This test component will return the contents of the file which name is specified using the file attribute of the <return-data> element. --> <test:component> <test:return-data file="message-contents-file.xml"/> </test:component>...

: & Logging (essage Detailse%%in# %$elogMessage+etails a%%ribu%e o) %$e %es%:componen%L elemen% @ill cause %$e %es%

componen% %o lo# %$e payload and proper%ies o) messa#es sen% %o %$e componen%... <!-- This test component will log payload and properties of messages sent to the component. --> <test:component logMessageDetails="true"/>...

1u%pu% )rom %$e above componen% @ill loo/ li/e %$is @$en sendin# a CE( re0ues% %o a <((*service endpoin%:INFO 2012-02-27 18:02:59,694 [connector.http.mule.default.receiver.02]org.mule.tck.functional.FunctionalTestComponent: Full Message payload:/Services/Regular/test[first appender msg]

Message properties: INVOCATION scoped properties: INBOUND scoped properties: Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Charset=ISO-8859-1,utf-8;q=0.7,*;q=0.7 Accept-Encoding=gzip, deflate Accept-Language=en-us,en;q=0.5 Connection=true Host=localhost:8182 Keep-Alive=true MULE_ORIGINATING_ENDPOINT=endpoint.http.localhost.8182.Services.Regular MULE_REMOTE_CLIENT_ADDRESS=/0:0:0:0:0:0:0:1%0:49398 User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.3)

Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729)

332

Page 333: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 333/354

http.context.path=/Services/Regular/ http.method=GET http.request=/Services/Regular/test http.request.path=/Services/Regular/test http.version=HTTP/1.1 OUTBOUND scoped properties: MULE_ENCODING=UTF-8 SESSION scoped properties:

: ' +etain a (essage .istor<($e %es% componen% used in %$e above examples can re%ain a lis% o) messa#es i% $as received ($econ%en%s o) %$is lis% can be re%rieved pro#ramma%ically or may be examined usin# a debu##er... <!-- This test component will retain a list of all messages it has received. --> <test:component enableMessageHistory="true"/>...

9o%e %$a%:• ($e messa#e $is%ory lo##in# is enabled as de)aul%• I) %$e messa#e ob4ec% is modi)ied a)%er %$e %es% componen% $as )inis$ed execu%in#, %$e

correspondin# messa#e, bein# one and %$e same ob4ec%, in %$e messa#e $is%ory lo# lis% @illalso c$an#e

Examinin# %$e messa#e $is%ory lis% in %$e Eclipse debu##er, a)%er $avin# $i% a brea/poin% se% in %$e ,unctional'est omponent class, loo/s li/e %$is:

Examinin# %$e messa#e $is%ory o) an ins%ance o) %$e ,unctional'est omponent in %$e Eclipse debu##er

333

Page 334: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 334/354

: * %ntro/uce a Dela<($e %es% componen% can also in%roduce a delay, simula%in# some %ime&consumin# processin#... <!-- This test component will introduce a two second delay in the message processing. -->

<test:component waitTime="2000">...

9o%e %$a%:• ($e delay %ime is %o be #iven in milliseconds

: , -ppen/ Text to +ecei$e/ (essages($e Mule %es% componen% can append %ex% %o %$e curren% messa#e, crea%in# a messa#e %$a% is a s%rin#ob4ec%... <!-- This test component will transform the message to its string representation and append a string to the message. Mule expressions may be used in the string to be appended. --> <test:component appendString="( processed by test component at #[function:systime] )">...

9o%e %$a%:• I) %$e incomin# messa#e is no% a s%rin# ob4ec%, i% @ill be %rans)ormed %o i%s s%rin#

represen%a%ion prior %o %ex% bein# appended %o %$e messa#e• I) %$e %es% componen% re%urns moc/ da%a, as discussed earlier, %$e moc/ da%a @ill replace any

s%rin# messa#e crea%ed by appendin# %ex% %o %$e received messa#e• Mule expressions can, as seen in %$e example above, be used in %$e %ex% %o be appended

335

Page 335: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 335/354

-ppen/ix - – Prepare for (ule De$elopment($is appendix con%ains s%ep&by&s%ep ins%ruc%ions on $o@ %o se% up %$e prin# ource (ool ui%e )ordevelopmen% @i%$ Mule versions 2 x and 3 x I% assumes %$a% you $ave already do@nloaded andins%alled a reasonably ne@ version o) prin# ource (ool ui%e I.E($e name FEclipseG @ill be used in%erc$an#eably @i%$ F prin# ource (ool ui%eG @$en re)errin# %o%$e I.E

1. *o nload and )nstall Mule($e examples in %$is boo/ uses %@o di))eren% versions o) MuleJ %$e 2 x version and %$e 3 x version

• .o@nload version 2 2 ! o) %$e Mule s%andalone dis%ribu%ion arc$ive )rom$ere• Bnpac/ %$e arc$ive %o %$e loca%ion o) your c$oice• .o@nload version 3 2 o) %$e Mule s%andalone dis%ribu%ion arc$ive )rom$ere•

Bnpac/ %$e arc$ive %o %$e loca%ion o) your c$oice*re)erably nex% %o %$e unpac/ed 2 2 ! dis%ribu%ion

In order %o be able %o use Mule in %$e s%andalone mode, some environmen% variables mus% be se%$en se%%in# environmen% variables, i% is assumed %$a% %$e necessary ?ava environmen% variables

$ave already been se%In an nix&based environmen%, ei%$er issue %$e )ollo@in# commands in a %erminal @indo@ or edi%%$e s$ell pro)ile accordin#ly "$an#e %$e pa%$ %o your Mule dis%ribu%ion accordin# %o i%s loca%ion inyour compu%er

export MULE_HOME="/Volumes/HD/Users/ivan/Applications/mule-standalone-3.2.1"PATH=$PATH:"$MULE_HOME/bin"

In a .1 &based environmen%, ei%$er issue %$e )ollo@in# commands or edi% %$e environmen%variables A#ain, c$an#e %$e pa%$ %o your Mule dis%ribu%ion accordin# %o i%s loca%ion on your local)ile sys%emset MULE_HOME="C:\mule-standalone-3.2.1"set PATH=%PATH%;%MULE_HOME%\bin

9o%e %$a% in order %o use ano%$er dis%ribu%ion @$en launc$in# a s%andalone ins%ance o) Mule, youneed %o modi)y %$e MB-EP<1ME environmen% variable %o poin% a% %$e appropria%e Muledis%ribu%ion

2. )nstall the Eclipse Mule 'lugin($e Mule plu#in )or %$e Eclipse enables crea%ion o) special Mule pro4ec%s in %$e Eclipse I.E,

provides aid @$en crea%in# Mule con)i#ura%ion )iles e%c I% is ins%alled li/e any o%$er Eclipse plu#in,usin# %$e upda%e si%e$%%p:==dis% mule)or#e or#=mule&ide=upda%es&2 ! x=

($e Mule I.E @ebpa#e, on @$ic$ %$ere is addi%ional in)orma%ion abou% %$e plu#in, can be )ound a%:$%%p:==@@@ muleso)% or#=documen%a%ion=display=MB-EI.E=<ome

338

Page 336: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 336/354

3. Configure the Mule 'lugin in Eclipse($e Eclipse Mule plu#in needs %o be %old abou% %$e Mule dis%ribu%ions available on %$e sys%em ($e)ollo@in# s%eps describe $o@ %o con)i#ure %$e plu#in %o use %$e %@o Mule dis%ribu%ions @e'vedo@nloaded earlier

1pen %$e Eclipse pre)erences• Co %o %$e Mule node• "lic/ %$e Add bu%%on• 9avi#a%e %o %$e direc%ory con%ainin# %$e Mule 3 x s%andalone dis%ribu%ion and selec% i%• "lic/ %$e Add bu%%on a#ain• 9avi#a%e %o %$e direc%ory con%ainin# %$e Mule 2 x s%andalone dis%ribu%ion and selec% i%• Ma/e sure %$e Mule 3 x dis%ribu%ion is %$e de)aul% by c$ec/in# i%s c$ec/box• "lic/ %$e Apply bu%%on in %$e lo@er ri#$% corner o) %$e *re)erences dialo#• "lic/ %$e 1K bu%%on %o exi% %$e *re)erences dialo#

($is concludes %$e prepara%ions and you s$ould no@ be ready %o develop @i%$ Mule in %$e EclipseI.E

337

Page 337: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 337/354

-ppen/ix – Create a (ule Pro!ect($is appendix describes $o@ %o crea%e a Mule pro4ec% %$a% @ill resul% in a s%andalone ?avaapplica%ion e @ill also see $o@ %o s@i%c$ be%@een Mule dis%ribu%ions in a Mule pro4ec%

1. Create the 'ro(ect i%$ %$e Mule plu#in ins%alled, %$ere @ill be a @izard )or crea%in# Mule pro4ec%s

• In %$e 6ile menu, selec% 9e@ &L *ro4ec%• elec% %$e Mule *ro4ec% @izard in %$e 9e@ *ro4ec% dialo# and clic/ %$e 9ex% bu%%on

elec%in# %$e Mule *ro4ec% @izard @$en crea%in# a ne@ pro4ec% in Eclipse

Hcon%inued on nex% pa#e

33

Page 338: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 338/354

• En%er %$e name o) %$e pro4ec%, selec% a loca%ion Hop%ional and c$oose %$e Mule dis%ribu%ion%o use in %$e pro4ec%I) you in%end %o )ollo@ %$e examples in %$is boo/ and use bo%$ Mule 2 x and Mule 3 x,con)i#ure %$e pro4ec% %o use %$e appropria%e Mule 3 x dis%ribu%ion 9o%e %$a% %$is does no%necessarily mean Mule 3 ! as s$o@n in %$e pic%ure

"rea%in# a ne@ Mule pro4ec% in EclipseJ en%erin# a pro4ec% name,selec%in# a pro4ec% loca%ion and selec%in# a Mule dis%ribu%ion %o be used by %$e pro4ec%

• "lic/ %$e 6inis$ bu%%on

33;

Page 339: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 339/354

2. S itch off the Mule 3 +ot *eplo#&ent uilder 1n occasions, %$e Mule 3 $o% deploymen% builder $as caused errors )or me @$en %ryin# %o build aMule pro4ec% (o )ix suc$ problems, 4us% s@i%c$ o)) %$e $o% deploymen% builder )or %$e pro4ec% in0ues%ion

1pen %$e *ro4ec% *re)erences in Eclipse• 9avi#a%e %o %$e >uilders node on %$e le)% side• Bnc$ec/ %$e Mule 3 <o% .eploymen% >uilder c$ec/box

@i%c$in# o)) %$e Mule 3 $o% deploymen% builder )or a Mule pro4ec% in Eclipse

33+

Page 340: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 340/354

3. Create Mule Configuration FilesMule con)i#ura%ion )iles are M- )iles used %o con)i#ure an ins%ance o) %$e Mule E > ($ey are

4us% one o) %$e @ays %o con)i#ure Mule, albei% %$e mos% common @ay ($ese M- )iles are reallyprin# bean con)i#ura%ion )iles @i%$ a se% o) special namespaces )or Mule modules and %ranspor%s

In %$is boo/ eac$ example @ill s$o@ $o@ %o use bo%$ Mule 2 x as @ell as Mule 3 x, so %@o Mulecon)i#ura%ion )iles @ill be crea%ed )or eac$ pro4ec%J one )or eac$ version I) you only @an% %o use oneversion, %$en )eel )ree %o s/ip crea%in# a second con)i#ura%ion )ile

• In %$e source )older, crea%e a Mule con)i#ura%ion )ile by, in %$e 6ile menu, selec%in# 9e@ &L1%$er and %$en loca%in# %$e Mule "on)i#ura%ion @izard

"rea%in# a Mule "on)i#ura%ion )ileJ loca%in# %$e Mule "on)i#ura%ion @izard•

"lic/ %$e 9ex% bu%%on in %$e @izards&dialo#• "lic/ %$e >ro@se bu%%on and selec% %$e source Hsrc direc%ory as %$e loca%ion @$ere %o save

%$e Mule con)i#ura%ion )ileI) you $ave a special direc%ory )or resources, li/e in a Maven pro4ec%, %$en suc$ a direc%ory isa be%%er c$oice

• "$oose a name )or %$e Mule con)i#ura%ion )ileince %$e examples in %$is boo/ @ill $ave %@o di))eren% Mule con)i#ura%ion )iles, one )or

eac$ Mule version used, i% may be a #ood idea %o append %$e Mule version number %o %$ename o) %$e con)i#ura%ion )ile($e de)aul% names used in %$is boo/ are Fmule&con)i#2 xmlG and Fmule&con)i#3 xmlG

35

Page 341: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 341/354

• "$ec/ %$e Mule Modules=(ranspor%s @$ic$ namespaces you @an% %o include in %$e Mulecon)i#ura%ion )ile you are abou% %o crea%eIndividual pro4ec%s may re0uire individual con)i#ura%ion o) @$ic$ modules and %ranspor%s %ouse uc$ re0uiremen%s @ill be included in %$e ins%ruc%ions )or eac$ individual pro4ec% &o%$er@ise use %$e de)aul% con)i#ura%ion

"on)i#urin# %$e loca%ion and name o) %$e Mule con)i#ura%ion )ile,as @ell as %$e namespaces )or %$e di))eren% modules and %ranspor%s

%o include in %$e con)i#ura%ion )ile

• "lic/ %$e 6inis$ bu%%on %o crea%e %$e ne@ Mule con)i#ura%ion )ile• "$an#e %$e Mule dis%ribu%ion used by %$e pro4ec%

($e process o) c$an#in# %$e Mule dis%ribu%ion o) a pro4ec% is described belo@I) %$e pro4ec% @as con)i#ured %o use %$e Mule 3 x dis%ribu%ion @$en you 4us% crea%ed %$econ)i#ura%ion )ile above, %$en s@i%c$ %o %$e Mule 2 x dis%ribu%ion and vice versa

• Repea% %$e above process %o crea%e a second Mule con)i#ura%ion )ile• "$an#e bac/ %$e Mule dis%ribu%ion used by %$e pro4ec%

($e process o) c$an#in# %$e Mule dis%ribu%ion o) a pro4ec% is described belo@

35!

Page 342: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 342/354

Page 343: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 343/354

/. Change the Mule *istribution of a 'ro(ect ($is sec%ion is no% relevan% @$en 4us% $avin# crea%ed a ne@ Mule pro4ec%, bu% is provided )orcomple%eness sa/e I% @ill come in $andy once @e s%ar% developin# %$e examples and @an% %odevelop )or bo%$ Mule 2 x and Mule 3 x In order %o do %$is @e need %o be able %o c$an#e %$e Muledis%ribu%ion a pro4ec% uses ($is is accomplis$ed in %$e Eclipse pro4ec% pre)erences:

• 1pen %$e *ro4ec% *re)erences in Eclipse• 9avi#a%e %o %$e ?ava >uild *a%$ node on %$e le)% side• "lic/ %$e -ibraries %ab• elec% %$e Mule -ibraries

-oca%in# %$e Mule libraries in an Eclipse pro4ec%

• "lic/ %$e Edi% bu%%on

Hcon%inue on nex% pa#e

353

Page 344: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 344/354

Page 345: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 345/354

-ppen/ix C – Enabling (a$en Depen/enc< (anagement foran Eclipse Pro!ect

($e prin# ource (ool ui%e developmen% environmen% comes @i%$ %$e Maven m2eclipse plu#in preins%alled ($is plu#in enables us %o, amon# o%$er %$in#s, enable dependency mana#emen% )or an

exis%in# pro4ec%($is s$ould be a %rivial opera%ion, @ere i% no% )or %$e )ac% %$a% %$e plu#in modi)ies %$e build pa%$ andcompiler se%%in#s o) %$e pro4ec% in %$e process>elo@ are ins%ruc%ions on $o@ %o enable dependency mana#emen% )or a pro4ec% and res%ore %$e o%$erse%%in#s:

• Ri#$%&clic/ %$e pro4ec% in %$e Eclipse *ac/a#e Explorer and selec% Maven &L Enable.ependency Mana#emen%

Enablin# Maven dependency mana#emen% )or %$e example pro4ec%

• En%er %$e da%a in %$e *1M&crea%ion dialo#As a minimum, en%er %$e #roup id & Fcom ivan muleG in my case

En%erin# da%a in %$e *1M&crea%ion dialo# @$en enablin# Maven dependency mana#emen% )or a pro4ec%

358

Page 346: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 346/354

• "lic/ %$e 6inis$ bu%%onA pom xml )ile s$ould appear in %$e pro4ec%, as can be seen in %$e *ac/a#e or *ro4ec%Explorer

• Ri#$%&clic/ on %$e pro4ec% and open %$e pro4ec%'s *roper%iesAl%erna%ively, clic/ %$e pro4ec% and $old %$e op%ion /ey @$ile $i%%in# %$e re%urn /ey

In %$e ?ava >uild *a%$ node, in %$e -ibraries %ab, c$an#e %$e ?RE ys%em -ibrary %o %$e la%es%?RE you $ave ins%alled($is is accomplis$ed by selec%in# %$e ?RE ys%em -ibrary en%ry, clic/in# %$e Edi% bu%%onand selec%in# %$e @or/space de)aul% ?RE

$en enablin# Maven dependency mana#emen%, %$e ?RE is au%oma%ically s@i%c$ed %o %$eversion ! 5 ?RE and @e need %o s@i%c$ i% bac/ %o be able %o use Mule

<avin# s@i%c$ed %$e ?RE ys%em -ibrary o) a pro4ec% @i%$ Maven dependency mana#emen%

357

Page 347: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 347/354

• %ill in %$e pro4ec%'s *roper%ies, navi#a%e %o %$e ?ava "ompile node and disable %$e use o) pro4ec% speci)ic se%%in#sA#ain, %$is is caused by enablin# %$e Maven dependency mana#emen%

<avin# disabled ?ava "ompiler se%%in#s o) a pro4ec% @i%$ Maven dependency mana#emen%

• "lic/ Apply and %$en 1K %o exi% %$e pro4ec% proper%ies dialo#

e $ave no@ comple%ed enablin# Maven dependency mana#emen% )or a pro4ec% and res%ored %$ese%%in#s modi)ied in %$e process

35

Page 348: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 348/354

-ppen/ix D – (ule Stan/alone Ser$er ($is appendix describes basic mana#emen% o) a Mule s%andalone server, suc$ as s%ar%in# ands%oppin# i% ($ere is also a sec%ion )or 1 users describin# $o@ %o )ix %$e Mule ins%alla%ion so %$a%i% can be run as a s%andalone Mule server on an 75&bi% Macin%os$ compu%er

1. Mule Standalone Server on :S 0 I) you in%end %o run Mule @i%$ Macin%os$ 1 on a 75&bi% compu%er, %$ere is a #ood c$ance %$a%you @ill run in%o problems due %o no 75&bi% version o) %$e (anu/i @rapper $avin# been included($is issue can be )ixed %$e )ollo@in# @ay:

• In a @eb bro@ser, #o %o %$e BR-$%%p:==@rapper %anu/iso)%@are com=doc=en#lis$=do@nload 4spTs%able

• .o@nload %$e la%es% .el%a *ac/ communi%y versionIn my case %$is is version 3 8 !8 and %$e direc% lin/ is:

$%%p:==@rapper %anu/iso)%@are com=do@nload=3 8 !3=@rapper&del%a&pac/&3 8 !3 %ar #z• Expand %$e do@nloaded arc$ive• 6rom %$e FlibG direc%ory in %$e roo% o) %$e unpac/ed .el%a *ac/ arc$ive, copy all )iles @$ose

names s%ar% @i%$ Flib@rapper&macosxG %o %$e Flib=boo%G direc%ory in %$e Mule direc%ory• .ele%e %$e ?AR )ile in %$e Flib=boo%G direc%ory in %$e Mule direc%ory @$ic$ name s%ar%s @i%$

F@rapperG and ends @i%$ F4arG H%$ere may or may no% be a version number in %$e )ilenameRemember %$e name o) %$e ?AR )ile, since %$e ne@ @rapper ?AR )ile mus% $ave %$e samename as %$e removed ?AR )ile o%$er@ise you @on'% be able %o s%ar% Mule )rom @i%$inEclipse

6rom %$e FlibG direc%ory in %$e roo% o) %$e unpac/ed .el%a *ac/, copy %$e F@rapper 4arG )ile%o %$e Flib=boo%G direc%ory in %$e Mule direc%oryRename %$e F@rapper 4arG )ile %o $ave %$e same name as %$e ?AR )ile removed in %$e

previous s%ep• 6rom %$e FbinG direc%ory in %$e roo% o) %$e unpac/ed .el%a *ac/, copy all %$e )iles @i%$

names s%ar% @i%$ F@rapper&macosx&universalG %o %$e Flib=boo%=execG direc%ory in %$e Muledirec%ory

I) you no@ a%%emp% %o s%ar% Mule by issuin# %$e F =muleG command in %$e FbinG direc%ory o) %$eMule direc%ory, %$ere @ill be console ou%pu% similar %o %$e )ollo@in#:

...Running Mule...--> Wrapper Started as ConsoleJava Service Wrapper Community Edition 32-bit 3.5.13 Copyright (C) 1999-2011 Tanuki Software, Ltd. All Rights Reserved. http://wrapper.tanukisoftware.com

Launching a JVM......

9o%e %$a% %$e messa#e indica%es %$e use o) %$e 32&bi% edi%ion o) %$e (anu/i @rapper <o@ever, i% is%$e 75&bi% version o) %$e ?VM %$a% is s%ar%ed, in @$ic$ Mule la%er is run

35;

Page 349: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 349/354

Page 350: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 350/354

" " Deplo< an/ 6n/eplo< a (ule -pplication$en usin# Mule 2 x, deploymen% and undeploymen% are limi%ed %o @$en s%ar%in# and s%oppin# %$e

en%ire Mule server I% is no% possible %o modi)y a runnin# Mule 2 x applica%ion @$ile i% is runnin#

i%$ Mule 3 x, i% is no% only possible %o deploy a Mule applica%ion %o a runnin# Mule server, bu% i%

is also possible %o ma/e modi)ica%ions %o a runnin# Mule applica%ion and $ave %$e modi)ica%ionsapplied on %$e )ly ($is )ea%ure is called $o% deploymen%

Mule 2.x *eplo#&ent

$en %$e Mule 2 x applica%ion $as been pac/a#ed accordin# %o %$e ins%ruc%ions in %$e sec%ion*ac/a#e Mule 2 x Applica%ions , %$e applica%ion can be deployed accordin# %o %$e )ollo@in# s%eps:

• "opy %$e ?AR )ileHs re0uired by %$e applica%ion %o %$e Flib=userG direc%ory in %$e Mule $omedirec%ory

• -aunc$ %$e Mule 2 x server @i%$ %$e pa%$ %o %$e Mule con)i#ura%ion )ile o) %$e applica%ion as

parame%er accordin# %o %$e ins%ruc%ions in %$e previous sec%ion

Mule 3.x *eplo#&ent

Mule 3 x applica%ion deploymen% $as been simpli)ied, since all %$e ar%i)ac%s o) an applica%ion can be/ep% in one and %$e same place a direc%ory In addi%ion, Mule 3 x allo@s )or $o% deploymen%,@$ic$ enables c$an#es %o a runnin# applica%ion %o come in%o e))ec% @i%$ou% res%ar%in# %$e Muleserver

i%$ %$e Mule 3 x applica%ion pac/a#ed accordin# %o %$e ins%ruc%ions in %$e sec%ion*ac/a#e Mule3 x Applica%ions, %$e applica%ion may be deployed as )ollo@s:

• "opy %$e applica%ion direc%ory %o %$e FappsG direc%ory in %$e Mule $ome direc%ory•

I) %$e Mule 3 x server is no% already s%ar%ed, %$en s%ar% i%$en s%ar%in# %$e Mule 3 x server, %$ere is no need %o supply %$e pa%$ %o %$e con)i#ura%ion)ileHs o) %$e applica%ion Mule 3 x @ill searc$ %$e FappsG direc%ory and )ind con)i#ura%ion)iles o) Mule applica%ions

" & 6n/eplo< a (ule -pplicationIn Mule 2 x, %$e only @ay %o undeploy a Mule applica%ion is %o s%op %$e server and remove %$eapplica%ion ar%i)ac%sJ ?AR )ileHs and con)i#ura%ion )ilesIn Mule 3 x, an anc$or&)ile @ill appear in %$e FappsG direc%ory nex% %o a success)ully deployedapplica%ion

Anc$or&)ile )or a success)ully deployed Mule 3 x applica%ion.ele%in# %$e anc$or&)ile o) an applica%ion undeploys %$e applica%ion in 0ues%ion

38

Page 351: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 351/354

Page 352: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 352/354

Page 353: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 353/354

• %ill in %$e ?AR -is% %ab, clic/ %$e Add ?AR=[ip bu%%on and selec% %$e *os%#re S- ?.>"?AR

<avin# selec%ed %$e da%abase driver ?AR )ile @$en crea%in# ne@ da%abase driver de)ini%ion in Eclipse

• "lic/ %$e 1K bu%%on %o )inis$ crea%ion o) %$e driver de)ini%ion

Hcon%inued on nex% pa#e

383

Page 354: ivansmuletutorial-111120030338-phpapp01

8/13/2019 ivansmuletutorial-111120030338-phpapp01

http://slidepdf.com/reader/full/ivansmuletutorial-111120030338-phpapp01 354/354

• "on%inue in %$e 9e@ ?.>" "onnec%ion *ro)ile dialo# by en%erin# user name and pass@ord($ese are pre)erably %$e adminis%ra%or username and pass@ord, as en%ered @$en ins%allin#*os%#re S- I) you @an% %o avoid en%erin# %$e pass@ord every %ime you @an% %o connec% %o%$e server, c$ec/ %$e F ave pass@ordG c$ec/box

En%erin# )inal connec%ion de%ails @$en crea%in# a ne@ da%a source in Eclipse

• (o ensure %$a% %$e da%a source con)i#ura%ion is correc%, clic/ %$e (es% "onnec%ion bu%%onI) %$e connec%ion %es% )ailed, c$an#e %$e se%%in#s and repea% %$e %es% un%il i% succeeds

• 6inis$ crea%ion o) %$e ne@ da%a source by clic/in# %$e 6inis$ bu%%on