{"id":163,"date":"2009-10-20T12:10:46","date_gmt":"2009-10-20T19:10:46","guid":{"rendered":"http:\/\/www.connorgarvey.com\/blog\/?p=163"},"modified":"2010-02-06T20:43:05","modified_gmt":"2010-02-07T03:43:05","slug":"guice-blazeds-messagebroker","status":"publish","type":"post","link":"https:\/\/connorgarvey.com\/blog\/?p=163","title":{"rendered":"Guice BlazeDS&#8217; MessageBroker"},"content":{"rendered":"<p>In <a href=\"http:\/\/www.connorgarvey.com\/blog\/?p=132\">a previous post (http:\/\/www.connorgarvey.com\/blog\/?p=132)<\/a>, I wrote about how to use Guice injection for Flex services.\u00a0 I used web.xml to configure the MessageBrokerServlet and configured each Flex service to use Guice as a factory.\u00a0 Since then, on this project, we&#8217;ve had to introduce new servlets.\u00a0 Rather than continue to use web.xml&#8217;s verbose and fully-qualified-path based configuration, we moved to using Guice&#8217;s ServletModule class.\u00a0 Here are the steps we followed.<\/p>\n<ol>\n<li>Ensure the guice-servlet.jar is included in your project and is deployed with your build.<\/li>\n<li>Add the Guice filter to web.xml.<br \/>\n<div id=\"wpshdo_1\" class=\"wp-synhighlighter-outer\"><div id=\"wpshdt_1\" class=\"wp-synhighlighter-expanded\"><table border=\"0\" width=\"100%\"><tr><td align=\"left\" width=\"80%\"><a name=\"#codesyntax_1\"><\/a><a id=\"wpshat_1\" class=\"wp-synhighlighter-title\" href=\"#codesyntax_1\"  onClick=\"javascript:wpsh_toggleBlock(1)\" title=\"Click to show\/hide code block\">Code block<\/a><\/td><td align=\"right\"><a href=\"#codesyntax_1\" onClick=\"javascript:wpsh_code(1)\" title=\"Show code only\"><img decoding=\"async\" border=\"0\" style=\"border: 0 none\" src=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/code.png\" \/><\/a>&nbsp;<a href=\"#codesyntax_1\" onClick=\"javascript:wpsh_print(1)\" title=\"Print code\"><img decoding=\"async\" border=\"0\" style=\"border: 0 none\" src=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/printer.png\" \/><\/a>&nbsp;<a href=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/About.html\" target=\"_blank\" title=\"Show plugin information\"><img decoding=\"async\" border=\"0\" style=\"border: 0 none\" src=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/info.gif\" \/><\/a>&nbsp;<\/td><\/tr><\/table><\/div><div id=\"wpshdi_1\" class=\"wp-synhighlighter-inner\" style=\"display: block;\"><pre class=\"xml\" style=\"font-family:monospace;\"><span class=\"sc3\"><span class=\"re1\">&lt;filter<span class=\"re2\">&gt;<\/span><\/span><\/span>\n  <span class=\"sc3\"><span class=\"re1\">&lt;filter-name<span class=\"re2\">&gt;<\/span><\/span><\/span>guiceFilter<span class=\"sc3\"><span class=\"re1\">&lt;\/filter-name<span class=\"re2\">&gt;<\/span><\/span><\/span>\n  <span class=\"sc3\"><span class=\"re1\">&lt;filter-class<span class=\"re2\">&gt;<\/span><\/span><\/span>com.google.inject.servlet.GuiceFilter<span class=\"sc3\"><span class=\"re1\">&lt;\/filter-class<span class=\"re2\">&gt;<\/span><\/span><\/span>\n<span class=\"sc3\"><span class=\"re1\">&lt;\/filter<span class=\"re2\">&gt;<\/span><\/span><\/span>\n<span class=\"sc3\"><span class=\"re1\">&lt;filter-mapping<span class=\"re2\">&gt;<\/span><\/span><\/span>\n  <span class=\"sc3\"><span class=\"re1\">&lt;filter-name<span class=\"re2\">&gt;<\/span><\/span><\/span>guiceFilter<span class=\"sc3\"><span class=\"re1\">&lt;\/filter-name<span class=\"re2\">&gt;<\/span><\/span><\/span>\n  <span class=\"sc3\"><span class=\"re1\">&lt;url-pattern<span class=\"re2\">&gt;<\/span><\/span><\/span>\/*<span class=\"sc3\"><span class=\"re1\">&lt;\/url-pattern<span class=\"re2\">&gt;<\/span><\/span><\/span>\n<span class=\"sc3\"><span class=\"re1\">&lt;\/filter-mapping<span class=\"re2\">&gt;<\/span><\/span><\/span><\/pre><\/div><\/div><\/li>\n<li>Create a servlet module, a class extending com.google.inject.servlet.ServletModule.<\/li>\n<li>Override the configureServlets() method of ServletModule and add the message broker servlet configuration.<br \/>\n<div id=\"wpshdo_2\" class=\"wp-synhighlighter-outer\"><div id=\"wpshdt_2\" class=\"wp-synhighlighter-expanded\"><table border=\"0\" width=\"100%\"><tr><td align=\"left\" width=\"80%\"><a name=\"#codesyntax_2\"><\/a><a id=\"wpshat_2\" class=\"wp-synhighlighter-title\" href=\"#codesyntax_2\"  onClick=\"javascript:wpsh_toggleBlock(2)\" title=\"Click to show\/hide code block\">Code block<\/a><\/td><td align=\"right\"><a href=\"#codesyntax_2\" onClick=\"javascript:wpsh_code(2)\" title=\"Show code only\"><img decoding=\"async\" border=\"0\" style=\"border: 0 none\" src=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/code.png\" \/><\/a>&nbsp;<a href=\"#codesyntax_2\" onClick=\"javascript:wpsh_print(2)\" title=\"Print code\"><img decoding=\"async\" border=\"0\" style=\"border: 0 none\" src=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/printer.png\" \/><\/a>&nbsp;<a href=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/About.html\" target=\"_blank\" title=\"Show plugin information\"><img decoding=\"async\" border=\"0\" style=\"border: 0 none\" src=\"https:\/\/connorgarvey.com\/blog\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/info.gif\" \/><\/a>&nbsp;<\/td><\/tr><\/table><\/div><div id=\"wpshdi_2\" class=\"wp-synhighlighter-inner\" style=\"display: block;\"><pre class=\"java5\" style=\"font-family:monospace;\"><span class=\"kw2\">this<\/span>.<span class=\"me1\">bind<\/span><span class=\"br0\">&#40;<\/span>MessageBrokerServlet.<span class=\"kw2\">class<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">in<\/span><span class=\"br0\">&#40;<\/span>Scopes.<span class=\"me1\">SINGLETON<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span>\n<span class=\"kw2\">final<\/span> Map<span class=\"sy0\">&lt;<\/span>String, String<span class=\"sy0\">&gt;<\/span> params = <span class=\"kw2\">new<\/span> TreeMap<span class=\"sy0\">&lt;<\/span>String, String<span class=\"sy0\">&gt;<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span>\nparams.<span class=\"me1\">put<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;services.configuration.file&quot;<\/span>, <span class=\"kw2\">this<\/span>.<span class=\"me1\">context<\/span>\n    .<span class=\"me1\">getRealPath<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;WEB-INF\/config\/flex\/services-config.xml&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span>\n<span class=\"kw2\">this<\/span>.<span class=\"me1\">serve<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;\/messagebroker\/*&quot;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">with<\/span><span class=\"br0\">&#40;<\/span>MessageBrokerServlet.<span class=\"kw2\">class<\/span>, params<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/pre><\/div><\/div><\/p>\n<ol>\n<li>Normally, servlets configured in Guice are tagged with @Singleton.\u00a0 Since the MessageBrokerServlet is third party, it&#8217;s marked as a singleton here, in the module.<\/li>\n<\/ol>\n<\/li>\n<li>Add the new module to the Guice servlet context listener, which should already be configured in web.xml.<\/li>\n<li>Remove the servlet and servlet-mapping from web.xml.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>In a previous post (http:\/\/www.connorgarvey.com\/blog\/?p=132), I wrote about how to use Guice injection for Flex services.\u00a0 I used web.xml to configure the MessageBrokerServlet and configured each Flex service to use Guice as a factory.\u00a0 Since then, on this project, we&#8217;ve had to introduce new servlets.\u00a0 Rather than continue to use web.xml&#8217;s verbose and fully-qualified-path based [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[26],"tags":[30,31,62,24,4],"class_list":["post-163","post","type-post","status-publish","format-standard","hentry","category-guice","tag-blazeds","tag-google","tag-guice","tag-injection","tag-java"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1ql2n-2D","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=163"}],"version-history":[{"count":4,"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/163\/revisions"}],"predecessor-version":[{"id":168,"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/163\/revisions\/168"}],"wp:attachment":[{"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/connorgarvey.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}