tag:blogger.com,1999:blog-2842403726787099862024-03-13T17:36:19.771-07:00Initek Consulting - San Diego, CAproviding senior consulting services for various open source software technologies
<br><br>
specializing in AWS, IoT, Python, Java, Apache Camel, Apache ActiveMQ, high volume messaging and various web application technologiesBen ODayhttp://www.blogger.com/profile/02462610652603338525noreply@blogger.comBlogger15125tag:blogger.com,1999:blog-284240372678709986.post-5938590452059015252016-11-28T22:02:00.002-08:002016-12-06T16:57:50.744-08:00Real-Time Data Batching with Apache CamelOver the past few years, I've been working with high volume real-time message based applications. As we scale these systems out, resource usage becomes increasingly important and various trade-offs have to be considered. <br />
<br />
From a storage perspective, many of the NoSQL techs claim to handle mass volumes of parallel requests and data with ease. In the real world, however, most techs have similar system resource limitations (threading, CPU, memory, I/O, etc) that all contribute to latency/stability issues. Though many symptoms can be masked by horizontal scaling...at the end of the day, the more efficiently your applications use resources, the better things scale out...<br />
<div>
<br /></div>
One approach is to batch process data to more efficiently use resources (threads/connections) by reducing per message/request overhead. This is a widely used pattern for at-rest/bulk data processing, but not often considered for real-time/event driven processing. As it turns out, this pattern is often a best practice (or even required) for reasonable performance from external systems and storage techs when working with high volume data. <br />
<br />
I first encountered this use case when storing high volume messaging in Oracle. The issue is that transaction commits are expensive and committing a single message at a time simply doesn't scale. Instead, we switched to aggregating messages in memory and then passed these to Oracle via an array. The net result was dramatically reduced load against the database, negligible delays in processing data and a theoretical increased chance of message loss...overall, this addressed our performance issues with relatively little impact.<br />
<br />
Then, as we moved into NoSQL solutions (Cassandra, HDFS, ElasticSearch, etc), I revisited the need for batching...it was obvious that the same batching strategy still applied to these new techs as well...<br />
<br />
Luckily, we are working with Apache Camel and its implementation of EIPs (Enterprise Integration Patterns) make solving these types of issues fairly straight forward. In particular, the split and aggregator patterns are designed for just this type of message flow.<br />
<br />
For example, let's say I have 3 systems that process messages...A, B, C. System A produces messages and sends them to system B. system B does some processing and then sends them to system C for final processing.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5L-gm9eKW3NbQeOmArd-YnN5AudxrP6EYx7W2W0RHy1WdGGPmhFyWSIzlDqviGyD1nltJNS35vnHeYwzMsYLvN_3A59vlsdlATb_L25AP4h4cVRzo5nEM1zHj2UU9238CQfScLsXHrJM/s1600/Untitled+drawing+%25281%2529.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="101" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5L-gm9eKW3NbQeOmArd-YnN5AudxrP6EYx7W2W0RHy1WdGGPmhFyWSIzlDqviGyD1nltJNS35vnHeYwzMsYLvN_3A59vlsdlATb_L25AP4h4cVRzo5nEM1zHj2UU9238CQfScLsXHrJM/s320/Untitled+drawing+%25281%2529.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
in Camel, this is expressed by the following simple route (assuming ActiveMQ is used as a message bus in between systems)</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbrrCMlTjedfQzWq1nT_IBDe7xz9XAs3mJIGbrxWTHHIxNN5y0V3mbCLpkxnFbZFcq59088wuS8SOI7AZBZgAeZWLvPpPdlFE6uMx1U3vH8ZO-KB4k8RMQDP7RbXSt_Nnrwy0tczKvSy4/s1600/Screen+Shot+2016-11-28+at+9.14.26+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbrrCMlTjedfQzWq1nT_IBDe7xz9XAs3mJIGbrxWTHHIxNN5y0V3mbCLpkxnFbZFcq59088wuS8SOI7AZBZgAeZWLvPpPdlFE6uMx1U3vH8ZO-KB4k8RMQDP7RbXSt_Nnrwy0tczKvSy4/s1600/Screen+Shot+2016-11-28+at+9.14.26+PM.png" /></a></div>
<br />
now, if we find that system B requires batch sizes of 100. I can easily batch these together using a simple aggregator as follows...<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0tQPbWfel2Q1eCF13y1TisTzpnsISjOEEla7pnYoiy9GiDZp0KAOoyHfgC6a6D2Kcbv0iQG68qCsfO_70Rp4F5zobQTyXqOXtB-Y66a6UWx3aoyw-1KI3I20bVzfsHuqRVzqD74W4vks/s1600/Screen+Shot+2016-11-28+at+9.55.50+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0tQPbWfel2Q1eCF13y1TisTzpnsISjOEEla7pnYoiy9GiDZp0KAOoyHfgC6a6D2Kcbv0iQG68qCsfO_70Rp4F5zobQTyXqOXtB-Y66a6UWx3aoyw-1KI3I20bVzfsHuqRVzqD74W4vks/s1600/Screen+Shot+2016-11-28+at+9.55.50+PM.png" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; color: #262626; font-family: Georgia,serif; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: normal; letter-spacing: normal; margin: 5px 0px 15px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<span style="font-size: small;">Given the config above, we'll pass on a batch after 100 messages are aggregated (completionSize) OR after 1000ms (completionTimeout)...the latter is key to limiting the delay in processing when volume is low. also note, the above would pass on a batch size of 100 to system C...</span></div>
<div style="background-color: white; box-sizing: border-box; color: #262626; font-family: Georgia,serif; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: normal; letter-spacing: normal; margin: 5px 0px 15px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">
<span style="font-size: small;">Now, let's assume system C prefers a batch size of 10 and must get groups for the same accountId...here are the changes required</span></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHD7ezl6nd-3sX8rFDMvQpEh5rU1O3x9K7BS5dw7uXepN8WxZ5Boo62K279nNq2UsLLWdVbfhic8_cBj3BQkzZWW1OgdZ7frvRhb1s63t8phI4gcW_a6BeT6f7tXIgNur8BMN-N1a-j5E/s1600/Screen+Shot+2016-11-28+at+9.56.46+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHD7ezl6nd-3sX8rFDMvQpEh5rU1O3x9K7BS5dw7uXepN8WxZ5Boo62K279nNq2UsLLWdVbfhic8_cBj3BQkzZWW1OgdZ7frvRhb1s63t8phI4gcW_a6BeT6f7tXIgNur8BMN-N1a-j5E/s1600/Screen+Shot+2016-11-28+at+9.56.46+PM.png" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<span style="font-size: small;"><span style="background-color: white; color: #262626; display: inline; float: none; font-family: "georgia" , serif; font-style: normal; font-weight: normal; letter-spacing: normal; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">notice that in the <span style="font-family: "georgia" , serif;">s</span>ystem C route, I now call </span><b style="background-color: white; box-sizing: border-box; color: #262626; font-family: Georgia,serif; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><i style="box-sizing: border-box;">split(body())</i></b><span style="background-color: white; color: #262626; display: inline; float: none; font-family: "georgia" , serif; font-style: normal; font-weight: normal; letter-spacing: normal; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"> which will split the incoming batch from <span style="font-family: "georgia" , serif;">s</span>ystem B and allow us to re-aggregate per the requirements for this system. Also, the </span><i style="background-color: white; box-sizing: border-box; color: #262626; font-family: Georgia,serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><b style="box-sizing: border-box; font-style: normal; font-weight: bold;"><i style="box-sizing: border-box;">header("accountId")</i></b><span class="Apple-converted-space"> </span></i><span style="background-color: white; color: #262626; display: inline; float: none; font-family: "georgia" , serif; font-style: normal; font-weight: normal; letter-spacing: normal; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">in place of<span class="Apple-converted-space"> </span></span><i style="background-color: white; box-sizing: border-box; color: #262626; font-family: Georgia,serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><b style="box-sizing: border-box; font-style: normal; font-weight: bold;"><i style="box-sizing: border-box;">constant(true)</i></b></i><span style="background-color: white; color: #262626; display: inline; float: none; font-family: "georgia" , serif; font-style: normal; font-weight: normal; letter-spacing: normal; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"> tells
the aggregator to build batches/groups for similar accountId values
only and still complete after 10 are received or after 1000ms...</span></span><br />
<br />
As you can see, these are very flexible and easy to implement patterns for message flow. That said, you do have to give some consideration to memory/CPU usage and overall message reliability requirements when using these. <br />
<br />
Overall, I've used this pattern to successfully scale out high volume requests to Oracle, Cassandra, HDFS and ElasticSearch...<br />
<br />Ben ODayhttp://www.blogger.com/profile/02462610652603338525noreply@blogger.com0tag:blogger.com,1999:blog-284240372678709986.post-21483222899580708992014-06-18T12:33:00.001-07:002014-06-20T15:21:02.251-07:00ApacheCon 2014 Presentation - Apache Camel Business PatternsA few months back I had the opportunity to present at ApacheCon in Denver, CO. While the conference was small, I had the chance to meet some interesting people in the community and learn about some other Apache projects.<br />
<br />
My presentation was an introduction to how to use Camel to address common business problems (common to my experience anyways).<br />
<br />
Here is a link to my presentation slides and the audio, feedback is welcome...<br />
<br />
<a href="http://prezi.com/tf0hgra0yvn1/camel-business-patterns/">http://prezi.com/tf0hgra0yvn1/camel-business-patterns/</a><br />
<br />
<a href="http://feathercast.apache.org/podcasts/ApacheConNA2014/lawrence_b/Lawrence-B_17Ben%20Oday.mp3">http://feathercast.apache.org/podcasts/ApacheConNA2014/lawrence_b/Lawrence-B_17Ben%20Oday.mp3</a><br /><br />
<iframe allowfullscreen="" frameborder="0" height="500" mozallowfullscreen="" src="http://prezi.com/embed/tf0hgra0yvn1/?bgcolor=ffffff&lock_to_path=0&autoplay=0&autohide_ctrls=0&features=undefined&disabled_features=undefined" webkitallowfullscreen="" width="650"></iframe><br />
<br />
<br />Ben ODayhttp://www.blogger.com/profile/02462610652603338525noreply@blogger.com1tag:blogger.com,1999:blog-284240372678709986.post-48232903246824715262011-11-08T13:49:00.000-08:002012-06-11T17:41:45.704-07:00Creating A Custom Camel Component<div style="font-family: inherit;">
<span style="font-size: small;">
While Camel supports an ever growing number of components, you might have a need to create a custom component. This could be to either promote reuse across projects, customize an existing component or provide a simplified interface to an existing system. Whatever the reason, here is an overview of the options that are available within the Camel framework... </span></div>
<div style="font-family: inherit;">
<span style="font-size: small;"><br />
</span></div>
<div style="font-family: inherit;">
<span style="font-size: small;"><b>first, consider just creating a Bean or Processor</b></span></div>
<div style="font-family: inherit;">
<span style="font-size: small;"><br />
</span></div>
<div style="font-family: inherit;">
<span style="font-size: small;">Before you jump in and create a component, consider just creating a simple class to handle your custom logic. Behind the scenes, all components are just Processors with a bunch of lifecycle support around them. Beans and Processors are simple, streamlined and easy to manage.</span></div>
<div style="font-family: inherit;">
<span style="font-size: small;"> </span></div>
<div style="font-family: inherit;">
<br />
<b><span style="font-size: small;"> using a <a href="http://camel.apache.org/bean-integration.html">Bean</a>...</span></b></div>
<div style="font-family: inherit;">
<span style="font-size: small;"> </span></div>
<pre class="code-java" style="font-family: inherit;"></pre>
<pre class="code-java" style="background-color: white; font-family: inherit;">from(uri).bean(MyBean.class);</pre>
<pre class="code-java" style="background-color: white; font-family: inherit;">...<span class="code-keyword"> </span></pre>
<pre class="code-java" style="background-color: white; font-family: inherit;"><span class="code-keyword">public</span> class MyBean {
<span class="code-keyword">public</span> void doSomething(<span class="code-object">Exchange exchange</span>) {
//do something...
}</pre>
<pre class="code-java" style="background-color: white; font-family: inherit;">}</pre>
<pre class="code-java" style="background-color: white; font-family: inherit;"></pre>
<pre class="code-java" style="font-family: inherit;"><b>using a <a href="http://camel.apache.org/processor.html">Processor</a>...</b></pre>
<pre class="code-java" style="font-family: inherit;"></pre>
<pre class="code-java" style="font-family: inherit;">from(uri).process(new MyProcessor());</pre>
<pre class="code-java" style="font-family: inherit;">...<span class="code-keyword"> </span></pre>
<pre class="code-java" style="font-family: inherit;"><span class="code-keyword">public</span> class MyProcessor <span class="code-keyword">implements</span> Processor {
<span class="code-keyword">public</span> void process(Exchange exchange) <span class="code-keyword">throws</span> Exception {
<span class="code-comment"> //<span class="code-keyword">do</span> something...</span> </pre>
<pre class="code-java" style="font-family: inherit;"> }
}</pre>
<div style="font-family: inherit;">
<br /></div>
<div style="font-family: inherit;">
</div>
<div style="font-family: inherit;">
<span style="font-size: small;"><b>create a custom component</b></span></div>
<div style="font-family: inherit;">
<span style="font-size: small;"><br />
</span></div>
<div style="font-family: inherit;">
<span style="font-size: small;">If you decide to go down this route, you should start by start by using a Maven archetype to stub out a new component project for you. </span></div>
<div style="font-family: inherit;">
<span style="font-size: small;"><br />
</span></div>
<div style="font-family: inherit;">
<span style="font-size: small;">mvn archetype:generate</span></div>
<div style="font-family: inherit;">
<span style="font-size: small;"> -DarchetypeGroupId=org.apache.camel.archetypes</span></div>
<div style="font-family: inherit;">
<span style="font-size: small;"> -DarchetypeArtifactId=camel-archetype-component</span></div>
<div style="font-family: inherit;">
<span style="font-size: small;"> -DarchetypeVersion=2.9.2</span></div>
<div style="font-family: inherit;">
<span style="font-size: small;"> -DarchetypeRepository=https://repository.apache.org/content/groups/snapshots-group</span></div>
<div style="font-family: inherit;">
<span style="font-size: small;"> -DgroupId=org.apache.camel.component</span></div>
<div style="font-family: inherit;">
<span style="font-size: small;"> -DartifactId=camel-ben</span></div>
<pre class="code-java" style="font-family: inherit;"><span style="font-size: small;"> </span></pre>
<pre class="code-java" style="font-family: inherit;"><span style="font-size: small;">This will create a new Maven component project that contains an example HelloWorld component as seen here...</span></pre>
<pre class="code-java" style="font-family: inherit;"><span style="font-size: small;"></span></pre>
<pre class="code-java" style="font-family: inherit;"><span style="font-size: small;"></span> </pre>
<div class="separator" style="clear: both; font-family: inherit; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidpkCc_Wt0VJIMukpRvaCUO_k-n2yoTgZxEguEPzsvHlTpqBiAFKdQH5hy4hEx1z609rREiLkuVOjRvUpa2CGjDG9bfrxjYRrr4w_sdRSHnR0NaMFEMKeUJv1CmWX4wytxzedjTs-iOas/s1600/camel-component.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidpkCc_Wt0VJIMukpRvaCUO_k-n2yoTgZxEguEPzsvHlTpqBiAFKdQH5hy4hEx1z609rREiLkuVOjRvUpa2CGjDG9bfrxjYRrr4w_sdRSHnR0NaMFEMKeUJv1CmWX4wytxzedjTs-iOas/s1600/camel-component.png" /></a></div>
<pre class="code-java" style="font-family: inherit;"> </pre>
<pre class="code-java" style="font-family: inherit;"> </pre>
<div style="font-family: inherit;">
<br /></div>
<div style="font-family: inherit;">
<br /></div>
<div style="font-family: inherit;">
<br /></div>
<div style="font-family: inherit;">
<br /></div>
<div style="font-family: inherit;">
<br /></div>
<div style="font-family: inherit;">
The following core classes are created and have the following responsibilities:</div>
<div style="font-family: inherit;">
<br /></div>
<ul style="font-family: inherit;">
<li>HelloWorldComponent</li>
<ul>
<li>endpoint factory which implements createEndpoint() </li>
</ul>
<li>HelloWorldEndpoint</li>
<ul>
<li>producer/consumer factory which implements createConsumer(), createProducer(), createExchange()</li>
</ul>
<li>HelloWorldConsumer</li>
<ul>
<li>acts as a service to consumes request at the start of a route</li>
</ul>
<li>HelloWorldProducer</li>
<ul>
<li>acts as a service consumer to dispatch outgoing requests and receive incoming replies</li>
</ul>
<li>Exchange</li>
<ul>
<li>encapsulate the in/out message payloads and meta data about the data flowing between endpoints</li>
</ul>
<li>Message</li>
<ul>
<li>represent the message payload</li>
<li>their is an IN and OUT message for each exchange </li>
</ul>
</ul>
<div style="font-family: inherit;">
So, how do all these classes/method actually work? The best way to get your head around this is to load the project into Eclipse (or IntelliJ) and debug the unit test. This will allow you to step into the route initialization and message processing to trace the flow.</div>
<div style="font-family: inherit;">
<br /></div>
<div style="font-family: inherit;">
<b>Consumer Lifecycle</b><br />
<br />
When you define a route that uses your new component as a consumer, like this<br />
<blockquote class="tr_bq">
from("helloworld:foo").to("log:result"); </blockquote>
<br />
It does the following:<br />
<ul>
<li>creates a HelloWorldComponent instance (one per CamelContext)</li>
<li>calls HelloWorldComponent createEndpoint() with the given URI</li>
<li>creates a HelloWorldEndpoint instance (one per route reference)</li>
<li>creates a HelloWorldConsumer instance (one per route reference)</li>
<li>register the route with the CamelContext and call doStart() on the Consumer</li>
<li>consumers will then start in one of the following modes:</li>
<ul>
<li>event driven - wait for message to trigger route</li>
<li>polling consumer - manually polls a resource for events</li>
<li>scheduled polling consumer - events automatically generated by timer</li>
<li>custom threading - custom management of the event lifecyle</li>
</ul>
</ul>
</div>
<br />
<div style="font-family: inherit;">
<b>Producer Lifecycle</b><br />
<br />
When you define a route that uses your new component as a producer, like this<br />
<blockquote class="tr_bq">
from("direct:start").to("helloworld:foo"); </blockquote>
It does the following:<br />
<ul>
<li>creates a HelloWorldComponent instance (one per CamelContext)<br />
</li>
<li>calls HelloWorldComponent createEndpoint() with the given URI</li>
<li>creates a HelloWorldEndpoint instance (one per route reference)</li>
<li>creates a HelloWorldProducer instance (one per route reference)</li>
<li>register the route with the CamelContext and start the route consumer</li>
<li>the Producer's process(Exchange) method is then executed</li>
<ul>
<li>generally, this will decorate the Exchange by interfacing with some external resource (file, jms, database, etc)</li>
</ul>
</ul>
</div>
<div style="font-family: inherit;">
</div>
<pre class="code-java" style="font-family: inherit;"><pre class="code-java"><b>Other Resources</b><span style="font-size: small;"><b> </b></span></pre>
<span style="font-size: small;"> </span></pre>
<pre class="code-java" style="font-family: inherit;"><span style="font-size: small;"><a href="http://camel.apache.org/writing-components.html%20%20">http://camel.apache.org/writing-components.html</a></span></pre>
<pre class="code-java" style="font-family: inherit;"><span style="font-size: small;"><a href="http://fusesource.com/docs/mirrors/camel/developers/writing-components.html">http://fusesource.com/docs/mirrors/camel/developers/writing-components.html</a>
<a href="http://fusesource.com/docs/mirrors/camel/documentation/user-guide/creating-a-new-camel-component.html">http://fusesource.com/docs/mirrors/camel/documentation/user-guide/creating-a-new-camel-component.html</a></span></pre>
<pre class="code-java" style="font-family: inherit;"><span style="font-size: small;"> </span></pre>
<pre class="code-java" style="font-family: inherit;"><span style="font-size: small;">
</span></pre>Ben ODayhttp://www.blogger.com/profile/02462610652603338525noreply@blogger.com11tag:blogger.com,1999:blog-284240372678709986.post-81382434569599872562011-08-19T21:37:00.000-07:002013-03-26T07:55:57.433-07:00Performance Monitoring With Spring AOPIf you are using Spring to access/configure resources (DAOs/services), then you might as well add some basic performance monitoring while you are at it. This is a trivial task with Spring AOP and doesn't require any changes to existing code, just some simple configuration. <br />
<br />
First, you need to include the spring-aop, aspectj and cglib libraries. If you are using Maven, simply include the following dependencies...<br />
<br />
<dependency><br />
<groupId>org.aspectj</groupId><br />
<artifactId>aspectjweaver</artifactId><br />
<version>1.5.4</version><br />
</dependency><br />
<dependency><br />
<groupId>cglib</groupId><br />
<artifactId>cglib-nodep</artifactId><br />
<version>2.2</version><br />
</dependency><br />
<dependency><br />
<groupId>org.springframework</groupId><br />
<artifactId>spring-aop</artifactId><br />
<version>2.5.6</version><br />
</dependency><br />
<br />
<br />
Next, identify what needs monitoring and put the AOP hooks in place. Generally, this just requires adding a pointcut and advisor configuration in your existing Spring XML configuration file. This configuration will add method response time logging to all methods in the "com.mycompany.services" package. Note: these classes must be instantiated with the Spring context...otherwise, the AOP hooks will not be executed. <br />
<br />
<bean id="performanceMonitor"<br />
class="org.springframework.aop.interceptor.PerformanceMonitorInterceptor" /><br />
<br />
<aop:config><br />
<aop:pointcut id="allServiceMethods" expression="execution(* com.mycompany.services.*.*(..))"/><br />
<aop:advisor pointcut-ref="allServiceMethods" advice-ref="performanceMonitor" order="2"/><br />
</aop:config><br />
<br />
Next, you need to setup your logging (log4j, etc) to enable TRACE on the interceptor class.<br />
<br />
<logger name="org.springframework.aop.interceptor.PerformanceMonitorInterceptor" additivity="false"><br />
<level value="TRACE"/><br />
<appender-ref ref="STDOUT"/><br />
</logger><br />
<br />
That's it, now when you run your application, you will see the following logging...<br />
<br />
TRACE PerformanceMonitorInterceptor - StopWatch 'PerfTestService.processRequest': running time (millis) = 1322<br />
TRACE PerformanceMonitorInterceptor - StopWatch 'PerfTestService.processRequest': running time (millis) = 98<br />
TRACE PerformanceMonitorInterceptor - StopWatch 'PerfTestService.processRequest': running time (millis) = 1764<br />
<br />
<br />
This is a some great raw data, but unfortunately is not very useful on its own. Its for every method call and doesn't provide any other stats. This quickly clutters up the log and without some way to process/aggregate the log entries, its hard to make sense out of it. So, unless you plan of writing some log parsing or using 3rd party software (like <a href="http://splunk/">Splunk</a> or <a href="http://www.cacti.net/">Cati</a>), then you really should do some processing of the data before writing it to the log file.<br />
<br />
<br />
One easy way to do this is to just write a simple interceptor class to use instead of the Spring default one (PerformanceMonitorInterceptor). Below is an example of this that provides periodic stats (last, average and greatest response time) as well as warning whenever a method response time exceeds a configured threshold.<br />
<br />
By default, it will log stats every 10 method calls and log a warning message anytime a method response time exceeds 1000ms.<br />
<br />
public class PerfInterceptor implements MethodInterceptor {<br />
<br />
Logger logger = LoggerFactory.getLogger(PerfInterceptor.class.getName());<br />
private static ConcurrentHashMap<String, MethodStats> methodStats = new ConcurrentHashMap<String, MethodStats>();<br />
private static long statLogFrequency = 10;<br />
private static long methodWarningThreshold = 1000;<br />
<br />
public Object invoke(MethodInvocation method) throws Throwable {<br />
long start = System.currentTimeMillis();<br />
try {<br />
return method.proceed();<br />
}<br />
finally {<br />
updateStats(method.getMethod().getName(),(System.currentTimeMillis() - start));<br />
}<br />
}<br />
<br />
private void updateStats(String methodName, long elapsedTime) {<br />
MethodStats stats = methodStats.get(methodName);<br />
if(stats == null) {<br />
stats = new MethodStats(methodName);<br />
methodStats.put(methodName,stats);<br />
}<br />
stats.count++;<br />
stats.totalTime += elapsedTime;<br />
if(elapsedTime > stats.maxTime) {<br />
stats.maxTime = elapsedTime;<br />
}<br />
<br />
if(elapsedTime > methodWarningThreshold) {<br />
logger.warn("method warning: " + methodName + "(), cnt = " + stats.count + ", lastTime = " + elapsedTime + ", maxTime = " + stats.maxTime);<br />
}<br />
<br />
if(stats.count % statLogFrequency == 0) {<br />
long avgTime = stats.totalTime / stats.count;<br />
long runningAvg = (stats.totalTime-stats.lastTotalTime) / statLogFrequency;<br />
logger.debug("method: " + methodName + "(), cnt = " + stats.count + ", lastTime = " + elapsedTime + ", avgTime = " + avgTime + ", runningAvg = " + runningAvg + ", maxTime = " + stats.maxTime);<br />
<br />
//reset the last total time<br />
stats.lastTotalTime = stats.totalTime; <br />
}<br />
}<br />
<br />
class MethodStats {<br />
public String methodName;<br />
public long count;<br />
public long totalTime;<br />
public long lastTotalTime;<br />
public long maxTime;<br />
<br />
public MethodStats(String methodName) {<br />
this.methodName = methodName;<br />
}<br />
} <br />
}<br />
<br />
<br />
Now, you just need to wire this into your app by referencing this class in your Spring xml and logging config. When you run your app, you will see stats like this...<br />
<br />
WARN PerfInterceptor - method warning: processRequest(), cnt = 10, lastTime = 1072, maxTime = 1937<br />
TRACE PerfInterceptor - method: processRequest(), cnt = 10, lastTime = 1072, avgTime = 1243, runningAvg = 1243, maxTime = 1937<br />
WARN PerfInterceptor - method warning: processRequest(), cnt = 20, lastTime = 1466, maxTime = 1937<br />
TRACE PerfInterceptor - method: processRequest(), cnt = 20, lastTime = 1466, avgTime = 1067, runningAvg = 892, maxTime = 1937<br />
<br />
As you can see, these stats can provide valuable feedback about class/method performance with very little effort and without modifying any existing Java code. This information can easily be used to find bottlenecks in your application (generally database or threading related, etc)...good luck<br />
<br />
This page has been translated into <a href="http://www.webhostinghub.com/support/es/misc/monitoreo-de-rendimiento-usando-aop">Spanish</a> language by Maria Ramos from <a href="http://www.webhostinghub.com/">Webhostinghub.com</a>Ben ODayhttp://www.blogger.com/profile/02462610652603338525noreply@blogger.com4tag:blogger.com,1999:blog-284240372678709986.post-54729954456822408332011-04-08T08:34:00.000-07:002011-06-15T14:54:47.504-07:00Camel ActiveMQ Performance TestHere is a simple unit
test (extends CamelTestSupport) to get a feel for how quickly Camel routes add/remove from a JMS queue. This should give you a ballpark latency estimate (~5ms for my setup). You can also get some great AMQ performance stats via <a href="http://activemq.apache.org/jmx.html">JMX</a> to monitor an active system.<br />
<br />
However, results will vary dramatically depending on thread and AMQ
performance/QoS configurations. Refer to the <a href="http://activemq.apache.org/performance.html">AMQ performance page</a> and the <a href="http://camel.apache.org/jms.html">camel-jms page</a> for more information...<br />
<br />
<div id=":ti">
private static final Logger logger =
Logger.getLogger(AMQRouteTest.<wbr></wbr>class.getName());</div>
<div id=":ti">
@EndpointInject(uri = "mock:mock")<br />
protected MockEndpoint mock;</div>
<div id=":ti">
<br />
protected CamelContext createCamelContext() throws Exception {<br />
CamelContext camelContext = super.createCamelContext();<br />
String url ="vm://test-broker?broker.<wbr></wbr>persistent=false&broker.<wbr></wbr>useJmx=false";<br />
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url)<wbr></wbr>;<br />
camelContext.addComponent("<wbr></wbr>activemq", </div>
<div id=":ti">
JmsComponent.<wbr></wbr>jmsComponentAutoAcknowledge(<wbr></wbr>connectionFactory));<br />
return camelContext;<br />
}<br />
<br />
@Test<br />
public void test() throws Exception {<br />
int messageCnt = 10000, poolSize = 5;<br />
mock.<wbr></wbr>setMinimumExpectedMessageCount<wbr></wbr>(messageCnt);<br />
<br />
ExecutorService executor = Executors.newFixedThreadPool(<wbr></wbr>poolSize);</div>
<div id=":ti">
<br />
for (int i = 0; i < messageCnt; i++) {<br />
executor.submit(new Callable() {<br />
public Object call() throws Exception {<br />
template.sendBody("activemq:<wbr></wbr>queue:test",System.<wbr></wbr>currentTimeMillis());<br />
return null;<br />
}<br />
});<br />
}<br />
mock.assertIsSatisfied();<br />
}<br />
<br />
@Override<br />
protected RouteBuilder createRouteBuilder() throws Exception {<br />
return new RouteBuilder() {<br />
@Override<br />
public void configure() throws Exception {<br />
from("activemq:queue:test?<wbr></wbr>concurrentConsumers=10")<br />
.process(new Processor() {<br />
long totalLatency, msgCnt;<br />
public void process(Exchange exch) throws Exception {<br />
totalLatency += (System.currentTimeMillis() - exch.getIn().getBody(Long.<wbr></wbr>class));<br />
if(++msgCnt % 1000 == 0) {<br />
<a href="http://logger.info/" target="_blank">logger.info</a>("avgLatency=" + (totalLatency/msgCnt));<br />
}<br />
}<br />
})<br />
.to("mock:mock");<br />
}<br />
};<br />
<div class="im">
}<br />
<br /></div>
<div class="im">
<br /></div>
</div>Ben ODayhttp://www.blogger.com/profile/02462610652603338525noreply@blogger.com1tag:blogger.com,1999:blog-284240372678709986.post-75016382749680143992011-01-03T14:27:00.000-08:002013-06-04T10:53:34.523-07:00Apache Camel MonitoringI've seen a lot of discussion about how to monitor Camel based applications. Most people are looking for the following features: ability to view services (contexts, endpoints, routes), to view performance statistics (route throughput, etc) and to perform basic operations (start/stop routes, send messages, etc).<br />
<br />
This post will breakdown the options (that I know of) that are available today (as of Camel 2.8). If you have used other approaches or know of other ongoing development in this area, please let me know.<br />
<br />
<b><a href="http://benoday.blogspot.com/2010/08/managing-camel-routes-with-jmx-apis.html">JMX APIs</a></b> <br />
<br />
Camel uses JMX to provide a standardized way to access metadata about contexts/routes/endpoints defined in a given application. Also, you can use JMX to interact with these components (start/stop routes, etc) in some interesting ways.<br />
<br />
I recently had some very specific Camel/ActiveMQ monitoring requests from a client. After looking at the options, we ended up building a standalone Tomcat web app that used JSPs, jQuery, Ajax and JMX APIs to view route/endpoint statistics, manage Camel routes (stop, start, etc) and monitor/manipulate ActiveMQ queues. It provided some much needed visibility and management features for our Camel/ActiveMQ based message processing application...<br />
<br />
<b><a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/CamelContext.html">CamelContext</a></b> <br />
<br />
If you have a handle to the CamelContext, there are various APIs that can help describe and manage routes and endpoints. These are used by the existing Camel Web Console and can be used to build custom interface to retrieve and use this information in various ways...<br />
<br />
here are some of the notable APIs...<br />
<br />
getRouteDefinitions()<br />
getEndpoints()<br />
getEndpointsMap()<br />
getRouteStatus(routeId)<br />
startRoute(routeId)<br />
stopRoute(routeId)<br />
removeRoute(routeId)<br />
addRoutes(routeBuilder) <br />
suspendRoute(routeId)<br />
resumeRoute(routeId) <br />
<br />
With a little creativity, you can use these APIs to manage/monitor and re-wire a Camel application dynamically.<br />
<br />
<b><a href="http://camel.apache.org/web-console.html">Camel Web Console</a></b><br />
<br />
This console provides web and REST interfaces to Camel contexts/routes/endpoints and allows you to view/manage endpoints/routes, send messages to endpoints, viewing route statistics, etc.<br />
<br />
That being said, using this web console with an existing Camel application is tricky at the moment. It's currently deployed as a war file that only has access to the CamelContext defined in its embedded spring XML file. Though the entire camel-web project can be embedded and customized in your application if you desire (and know <a href="http://scalate.fusesource.org/">Scalate</a>). Given my recent client requirements, I opted to build my own basic app using JSPs/JMX as described above.<br />
<br />
There has been some recent support for deploying this console in OSGI, where it should be able to view any CamelContexts deployed in the container, etc. However, I'm yet to see this work...more on this later.<br />
<br />
<b>Using Camel APIs</b><br />
<b> </b><br />
There are also a number of Camel technologies/patterns that can be used to add monitoring to existing routes.<br />
<br />
<ul>
<li><a href="http://camel.apache.org/wire-tap.html">wire tap</a> - can add message logging (to a file or JMS queue/topic, etc) or other inline processing </li>
<li><a href="http://camel.apache.org/advicewith.html">advicewith</a> - can be used to modify existing routes to apply before/after operations or add/remove operations in a route</li>
<li><a href="http://camel.apache.org/intercept.html">intercept</a> - can be used to intercept Exchanges while they are in route, can apply to all endpoints, certain endpoints or just starting endpoints</li>
<li><a class="external-link" href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/spi/BrowsableEndpoint.html" rel="nofollow">BrowsableEndpoint</a> - is an interface which Endpoints<a href="http://camel.apache.org/endpoint.html" title="Endpoint"></a> may implement to support the browsing of the exchanges which are pending or have been sent on it.</li>
</ul>
That being said, it takes some creativity to use these effectively and caution to not adversely affect the routes you are trying to monitor.<br />
<br />
<b> Hyperic HQ</b><br />
<br />
You can use this tool to monitor Servicemix (or any process), but it more geared towards system monitoring and JVM stats. I didn't find it useful for any Camel specific monitoring. <br />
<br />
<b><a href="http://hawt.io/">HAWTIO</a> - http://hawt.io/</b><br />
This is a newer tool that has plugins for ActiveMQ, Camel, etc to expose some great information and allow your to perform JMX operations.<br />
<br />
<b>jConsole/VisualVM</b><br />
<br />
these are standard JMX based consoles. They aren't web based and can't be customized (easily anyways) to provide anything more than a tree-like view of JMX MBeans. If you know where to look though, you can do a lot with it.<br />
<br />
<b>summary</b><br />
<br />
These are just some quick notes at this point. As I learn about other ways of monitoring Camel, I'll update this list and give some more detailed comparison. Any comments are welcome...<br />
<br />
<br />Ben ODayhttp://www.blogger.com/profile/02462610652603338525noreply@blogger.com4tag:blogger.com,1999:blog-284240372678709986.post-34321910161762589282010-12-19T20:28:00.000-08:002011-01-04T10:16:39.734-08:00basic REST service in Apache CXF vs. Camel-CXFThis article demonstrates how to create/test a basic REST service in CXF vs. Camel-CXF. Given the range of configuration and deployment options, I'm focusing on building a basic OSGi bundle that can be deployed in Fuse 4.2 (ServiceMix)...basic knowledge of Maven, ServiceMix and Camel are assumed.<br />
<br />
<b>Apache CXF</b><br />
<br />
For more details, see <a href="http://cxf.apache.org/docs/jax-rs.html">http://cxf.apache.org/docs/jax-rs.html</a>.<br />
<br />
Here is an overview of the steps to get a basic example running...<br />
<br />
1. add dependencies to your pom.xml<br />
<br />
<dependency><br />
<groupId>org.apache.cxf</groupId><br />
<artifactId>cxf-rt-frontend-jaxrs</artifactId><br />
<version>2.3.0</version><br />
</dependency> <br />
<br />
2. setup the bundle-context.xml file<br />
<br />
<import resource="classpath:META-INF/cxf/cxf.xml" /><br />
<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" /><br />
<import resource="classpath:META-INF/cxf/cxf-extension-http.xml" /><br />
<import resource="classpath:META-INF/cxf/cxf-extension-http-jetty.xml" /><br />
<br />
<bean id="exampleBean" class="com.example.ExampleBean" /><br />
<br />
<jaxrs:server id="exampleService" address="http://localhost:9000/"><br />
<jaxrs:serviceBeans><br />
<ref bean="exampleBean" /><br />
</jaxrs:serviceBeans><br />
</jaxrs:server><br />
<br />
3. create a service bean class<br />
<br />
@Path("/example")<br />
public class ExampleBean {<br />
<br />
@GET<br />
@Path("/")<br />
public String ping() throws Exception {<br />
return "SUCCESS";<br />
}<br />
}<br />
<br />
4. deploy and test<br />
<br />
build the bundle using "mvn install"<br />
start servicemix <br />
deploy the bundle<br />
open a browser to "http://localhost:9000/example" (should see "SUCCESS")<br />
<br />
<b>Camel-CXF</b><br />
<br />
For details, see <a href="http://camel.apache.org/cxfrs.html">http://camel.apache.org/cxfrs.html</a>. <br />
<br />
Here is an overview of the steps to get a basic example running...<br />
<br />
1. add dependencies to your pom.xml<br />
<br />
<dependency><br />
<groupId>org.apache.camel</groupId><br />
<artifactId>camel-core</artifactId><br />
<version>${camel.version}</version><br />
</dependency><br />
<br />
<dependency><br />
<groupId>org.apache.camel</groupId><br />
<artifactId>camel-cxf</artifactId><br />
<version>${camel.version}</version><br />
</dependency> <br />
<br />
2. setup the bundle-context.xml file<br />
<br />
<camelContext trace="true" id="camelContext" xmlns="http://camel.apache.org/schema/spring"><br />
<package>com.example</package><br />
</camelContext><br />
<br />
3. create a RouteBuilder class<br />
<br />
public class ExampleRouter extends RouteBuilder {<br />
<br />
@Override<br />
public void configure() throws Exception {<br />
<br />
from("cxfrs://http://localhost:9000?resourceClasses=" + ExampleResource.class.getName())<br />
.process(new Processor() {<br />
public void process(Exchange exchange) throws Exception {<br />
//custom processing here<br />
}<br />
})<br />
.setBody(constant("SUCCESS"));<br />
}<br />
}<br />
<br />
4. create a REST Resource class<br />
<br />
@Path("/example")<br />
public class ExampleResource {<br />
<br />
@GET<br />
public void ping() {<br />
//strangely, this method is not called, only serves to configure the endpoint<br />
}<br />
}<br />
<br />
5. deploy and test<br />
<br />
build bundle using "mvn install"<br />
start servicemix<br />
deploy the bundle<br />
open a browser to "http://localhost:9000/example" (should see "SUCCESS")<br />
<br />
<b>Unit Testing</b><br />
<b> </b><br />
To perform basic unit testing for either of these approaches, use the Apache HttpClient APIs by first adding this dependency to your pom.xml...<br />
<b> </b><br />
<dependency><br /> <groupId>org.apache.httpcomponents</groupId><br />
<div id=":ei">
<artifactId>httpclient</<wbr></wbr>artifactId><br /> <version>4.0.1</version><br /> </dependency></div>
<b> </b><br />
<br />
Then, you can use these APIs to create a basic test to validate the REST services created above...<br />
<b> </b><br />
String url = "http://localhost:9000/example";<br /> HttpGet httpGet = new HttpGet(url);<br /> HttpClient httpclient = new DefaultHttpClient();<br /> HttpResponse response = httpclient.execute(httpGet);<br />
String responseMessage = EntityUtils.toString(response.getEntity());<br />
<div id=":gb">
assertEquals("SUCCESS", responseMessage);<br /> assertEquals(200, response.getStatusLine().<wbr></wbr>getStatusCode());</div>
<b> </b><br />
<br />
<b>Summary</b><br />
<br />
Overall, the approaches are very similar, but you can use various combinations of Spring XML and Java APIs to set this up. I focused on a common approach to demonstrate the basics of each approach side-by-side.<br />
<br />
That being said, if you have requirements for complex REST services (security, interceptors, filters, etc), I recommend grabbing a copy of <a href="https://www.packtpub.com/apache-cxf-web-service-development/book">Apache CXF Web Service Development</a> and following some of the more complex examples on the Apache CXF, Camel-CXFRS pages.<br />
<br />
In practice, I've generally used Camel-CXF because it gives you the flexibility of integrating with other Camel components and allows you to leverage the rich routing features of Camel. I hope to cover more complex scenarios in future posts...<br />
<br />
<br />
<br />Ben ODayhttp://www.blogger.com/profile/02462610652603338525noreply@blogger.com14tag:blogger.com,1999:blog-284240372678709986.post-3284539251438574372010-08-21T16:18:00.000-07:002011-01-01T11:06:10.360-08:00practical benefits of SubversionI recently worked with a legacy version control system and found myself missing Subversion instantly. As a consultant, I often work with clients that are using in-house processes/software to manage project artifacts. I've grown used to many of SVN's features and just assumed everyone was using something comparable these days. Here are my notes on some of the practical benefits of using Subversion that I think apply to most projects... <br />
<br />
<b>core features/benefits</b>
<br />
<ul>
<li>atomic commits</li>
<ul>
<li>one transaction to add/update/delete files</li>
<li>promotes grouping related changes together in one commit to easily identify and revert</li>
</ul>
<li>full revision history retained for files that are renamed, copied, moved or removed</li>
<li>no file locking (by design)
</li>
<ul>
<li>file locking is extremely counter-productive (especially for large teams in multiple timezones)</li>
<li>SVN's automatic merging and conflict resolution tools make locking unnecessary</li>
</ul>
<li>working copy of files are always writable and all changes are local until committed<br />
</li>
<ul>
<li>allows you to easily revert unwanted changes at commit time</li>
</ul>
<li>fast and flexible update/commits</li>
<ul>
<li>all tasks can be done from any level easily (updates, commits, merges, reverts, etc)</li>
</ul>
<li>branching and tagging are cheap operations</li>
<ul>
<li>simple to create a tag/branch and merge between them</li>
</ul>
<li>ease of access to the commit log</li>
<ul>
<li>provides high visibility to changes, file history, integrated file comparison</li>
<li>ease of reverting changes to a file or an entire commit</li>
</ul>
<li>easy to setup email triggers (on checkins to keep team informed of changes, etc)</li>
<li>integrates with everything
</li>
<ul>
<li>windows explorer integration via Tortoise </li>
<li>with leading IDEs (Eclipse, IntelliJ)</li>
<li>Agile/continuous integration tools (Atlassian, Jira, Fisheye, TeamCity, Hudson, etc.)</li>
<li>various websites to provide project/committer stats (Ohloh, etc)</li>
</ul>
<li>ease of setup and administration</li>
</ul>
<b>benefits in practice (using Windows and Tortoise)</b>
<br />
<ul>
<li>syncing up my local environment</li>
<ul>
<li>open Windows Explorer and navigate to the root directory of my project</li>
<li>right-click and choose 'TortoiseSVN->Commit'
</li>
<ul>
<li>sounds counter-intuitive, but this show me a quick view of uncommitted local changes (aka 'mock commit')</li>
<ul>
<li>reminds me what I was working on</li>
<li>lets me right-click on any files and choose 'compare with base' to get an instant diff</li>
</ul>
<li>once I'm recalibrated to the project and any in-progress changes, I cancel the commit...</li>
</ul>
<li>update my local working code<br />
</li>
<ul>
<li>switch to another branch (if necessary)<br />
</li>
<ul>
<li>if I need to work on another branch than what is currently selected, then I can switch to it easily</li>
<li>right-click on project and choose 'TortoiseSVN->Switch'</li>
<li>enter the SVN URL of the project I need to switch to</li>
<li>click 'OK' to sync up with my local files</li>
</ul>
</ul>
<ul>
<li>otherwise, right-click and choose 'SVN Update'<br />
</li>
<ul>
<li>this syncs up my local working directory with the latest changes in the repository</li>
<li>again, I can right-click on any files to compare them my local copy (to see what has changed)</li>
</ul>
<ul>
<li>any conflicts are marked (rare)</li>
<ul>
<li>I can choose to either resolve it now or later, choose to use my version, or choose to use the repository version</li>
</ul>
</ul>
</ul>
</ul>
<li>working on bug fixes/enhancements<br />
</li>
<ul>
<li>work on a single feature at a time</li>
<li>make all related changes, write any necessary unit tests and test locally (of course)</li>
<li>once complete, I commit the change...</li>
<ul>
<li>flip back to Explorer (can also be done with Eclipse SVN plugins)</li>
<li>right-click on the project and choose 'TortoiseSVN->SVN Update' (optional)
</li>
<ul>
<li>only necessary if you are working with a large development team or its been a while since I last updated from SVN</li>
<li>this will insure that there weren't any other changes that might affect my commit</li>
</ul>
<li>right-click on the base project directory and choose 'TortoiseSVN->SVN Commit'</li>
<ul>
<li>dialogue box comes up to review my changes</li>
<li>right-click on any files in question, revert as necessary</li>
<li>check/uncheck the files I want to commit</li>
<li>enter a quick comment referencing the bug/enhancement worked on and a description of the change</li>
<li>click 'OK' to commit the changes</li>
</ul>
</ul>
</ul>
<li>working on major refactoring or large feature development</li>
<ul>
<li>I don't want this to affect the trunk, so I create a new development branch</li>
<li>right-click on the project root and choose 'TortoiseSVN->Branch/Tag'</li>
<li>in the dialogue box
</li>
<ul>
<li>enter a new directory for your branch (generally /branches/<branch name>)</li>
<li> enter a comment about the purpose of the branch</li>
<li>click 'OK'...</li>
</ul>
<li>switch your local working directory to use the branch you just created (see instructions above)</li>
<li>that is it...you now have a development branch (whole process takes about 30 seconds unless you have a large project)</li>
</ul>
</ul>
<b>summary</b><br />
Overall, I think Subversion provides many productivity/quality control benefits and can even prove cost-effective to switch to mid-project. Given that most open source Java development projects use SVN, I feel pretty confident that it continues to be a solid choice for the most projects.Ben ODayhttp://www.blogger.com/profile/02462610652603338525noreply@blogger.com4tag:blogger.com,1999:blog-284240372678709986.post-48669452416934124482010-08-18T09:27:00.000-07:002011-01-01T10:58:11.232-08:00managing Camel routes with JMX APIshere is a quick example of how to programmatically access Camel MBeans to monitor and manipulate routes...<br />
<br />
<b>first, get a connection to a JMX server (assumes localhost, port 1099, no auth)</b><br />
<i>note, always cache the connection for subsequent requests (can cause memory utilization issues otherwise)</i><br />
<br />
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi");<br />
JMXConnector jmxc = JMXConnectorFactory.connect(url);<br />
MBeanServerConnection server = jmxc.getMBeanServerConnection();<br />
<br />
<b>use the following to iterate over all routes and retrieve statistics (state, exchanges, etc)...</b><br />
<b> </b><br />
ObjectName objName = new ObjectName("org.apache.camel:type=routes,*");<br />
List<ObjectName> cacheList = new LinkedList(server.queryNames(objName, null));<br />
for (Iterator<ObjectName> iter = cacheList.iterator(); iter.hasNext();)<br />
{<br />
objName = iter.next();<br />
String keyProps = objName.getCanonicalKeyPropertyListString();<br />
ObjectName objectInfoName = new ObjectName("org.apache.camel:" + keyProps);<br />
String routeId = (String) server.getAttribute(objectInfoName, "RouteId");<br />
String description = (String) server.getAttribute(objectInfoName, "Description");<br />
String state = (String) server.getAttribute(objectInfoName, "State");<br />
...<br />
}<br />
<br />
<b>use the following to execute operations against a Camel route (stop,start, etc)</b><br />
<br />
ObjectName objName = new ObjectName("org.apache.camel:type=routes,*");<br />
List<ObjectName> cacheList = new LinkedList(server.queryNames(objName, null));<br />
for (Iterator<ObjectName> iter = cacheList.iterator(); iter.hasNext();)<br />
{<br />
objName = iter.next();<br />
String keyProps = objName.getCanonicalKeyPropertyListString();<br />
if(keyProps.contains(routeID))<br />
{<br />
ObjectName objectRouteName = new ObjectName("org.apache.camel:" + keyProps);<br />
Object[] params = {};<br />
String[] sig = {};<br />
server.invoke(objectRouteName, operationName, params, sig);<br />
return;<br />
}<br />
}<br />
<br />
<b>summary</b><br />
<b></b><br />
These APIs can easily be used to build a web or command line based tool to support remote Camel management features. All of these features are available via the JMX console and Camel does provide a web console to support some management/monitoring tasks.<br />
<br />
See these pages for more information...<br />
<a href="http://camel.apache.org/camel-jmx.html">http://camel.apache.org/camel-jmx.html</a><br />
<a href="http://camel.apache.org/web-console.html%20">http://camel.apache.org/web-console.html</a><br />
<br />
<br />
<br />Ben ODayhttp://www.blogger.com/profile/02462610652603338525noreply@blogger.com4tag:blogger.com,1999:blog-284240372678709986.post-31387268067601858342010-08-17T21:32:00.000-07:002011-06-16T10:03:12.273-07:00managing ActiveMQ with JMX APIshere is a quick example of how to programmatically access ActiveMQ MBeans to monitor and manipulate message queues...<br />
<br />
<b>first, get a connection to a JMX server (assumes localhost, port 1099, no auth)</b><br />
<i>note, always cache the connection for subsequent requests (can cause memory utilization issues otherwise)</i><br />
<br />
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi");<br />
JMXConnector jmxc = JMXConnectorFactory.connect(url);<br />
MBeanServerConnection conn = jmxc.getMBeanServerConnection();<br />
<br />
<b>then, you can execute various operations such as addQueue, removeQueue, etc...</b><br />
<br />
String operationName="addQueue";<br />
String parameter="MyNewQueue";<br />
ObjectName activeMQ = new ObjectName("org.apache.activemq:BrokerName=localhost,Type=Broker");<br />
if(parameter != null) {<br />
Object[] params = {parameter};<br />
String[] sig = {"java.lang.String"};<br />
conn.invoke(activeMQ, operationName, params, sig);<br />
} else {<br />
conn.invoke(activeMQ, operationName,null,null);<br />
}
<br />
<br />
<b>also, you can get an ActiveMQ QueueViewMBean instance for a specified queue name...</b><br />
<br />
ObjectName activeMQ = new ObjectName("org.apache.activemq:BrokerName=localhost,Type=Broker");<br />
BrokerViewMBean mbean = (BrokerViewMBean) MBeanServerInvocationHandler.newProxyInstance(conn, activeMQ,BrokerViewMBean.class, true);<br />
<br />
for (ObjectName name : mbean.getQueues()) {<br />
QueueViewMBean queueMbean = (QueueViewMBean)<br />
MBeanServerInvocationHandler.newProxyInstance(mbsc, name, QueueViewMBean.class, true);<br />
<br />
if (queueMbean.getName().equals(queueName)) {<br />
queueViewBeanCache.put(cacheKey, queueMbean);<br />
return queueMbean;<br />
}<br />
}
<br />
<br />
<b>then, execute one of several APIs against the <a href="http://activemq.apache.org/maven/activemq-core/apidocs/org/apache/activemq/broker/jmx/QueueViewMBean.html">QueueViewMBean</a> instance...</b>
<br />
<br />
queue monitoring - getEnqueueCount(), getDequeueCount(), getConsumerCount(), etc...
<br />
<br />
queue manipulation - purge(), getMessage(String messageId), removeMessage(String messageId), moveMessageTo(String messageId, String destinationName), copyMessageTo(String messageId, String destinationName), etc...
<br />
<br />
<b>summary</b><br />
The APIs can easily be used to build a web or command line based tool to support remote ActiveMQ management features. That being said, all of these features are available via the JMX console itself and ActiveMQ does provide a web console to support some management/monitoring tasks.<br />
<br />
See these pages for more information...<br />
<br />
<a href="http://activemq.apache.org/jmx-support.html">http://activemq.apache.org/jmx-support.html</a><br />
<a href="http://activemq.apache.org/web-console.html%20">http://activemq.apache.org/web-console.html </a><br />
<br />
<br />Ben ODayhttp://www.blogger.com/profile/02462610652603338525noreply@blogger.com9tag:blogger.com,1999:blog-284240372678709986.post-51254083375862219672010-08-17T12:37:00.000-07:002011-04-26T16:04:23.383-07:00Camel exception handling overviewHere are some notes on adding Camel (v2.3) exception handling to a JavaDSL route. There are various approaches/options available. These notes cover the important distinctions between approaches...<br />
<br />
<b>default handling</b><br />
The default mode uses the DefaultErrorHandler strategy which simply propagates any exception back to the caller and ends the route immediately. This is rarely the desired behavior, at the very least, you should define a generic/global exception handler to log the errors and put them on a queue for further analysis (during development, testing, etc).<br />
<br />
onException(Exception)<br />
.to("log:GeneralError?level=ERROR")<br />
.to("activemq:GeneralErrorQueue");<br />
<br />
<b>try-catch-finally</b><br />
This approach mimics the Java for exception handling and is designed to be very readable and easy to implement. It inlines the try/catch/finally blocks directly in the route and is useful for route specific error handling.<br />
<br />
from("direct:start")<br />
.doTry()<br />
.process(new MyProcessor())<br />
.doCatch(Exception.class)<br />
.to("mock:error");<br />
.doFinally()<br />
.to("mock:end");<br />
<br />
<b>onException</b><br />
This approach defines the exception clause separately from the route. This makes the route and exception handling code more readable and reusable. Also, the exception handling will apply to any routes defined in its CamelContext.<br />
<br />
from("direct:start")<br />
.process(new MyProcessor())<br />
.to("mock:end");<br />
<br />
onException(Exception.class)<br />
.to("mock:error");<br />
<br />
<b>handled/continued</b><br />
These APIs provide valuable control over the flow. Adding handled(true) tells Camel to not propagate the error back to the caller (should almost always be used). The continued(true) tells Camel to resume the route where it left off (rarely used, but powerful). These can both be used to control the flow of the route in interesting ways, for example...<br />
<br />
from("direct:start")<br />
.process(new MyProcessor())<br />
.to("mock:end");<br />
<br />
//send the exception back to the client (rarely used, clients need a meaningful response)<br />
onException(ClientException.class)<br />
.handled(false) //default<br />
.log("error sent back to the client");<br />
<br />
//send a readable error message back to the client and handle the error internally<br />
onException(HandledException.class)<br />
.handled(true)<br />
.setBody(constant("error")) <br />
.to("mock:error");<br />
<br />
//ignore the exception and continue the route (can be dangerous, use wisely)<br />
onException(ContinuedException.class)<br />
.continued(true);<br />
<br />
<b>using a processor for more control</b><br />
If you need more control of the handler code, you can use an inline Processor to get a handle to the exception that was thrown and write your own handle code...<br />
<br />
onException(Exception.class)<br />
.process(new Processor() {<br />
public void process(Exchange exchange) throws Exception {<br />
Exception exception = (Exception) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);<br />
//log, email, reroute, etc.<br />
}<br />
});<br />
<br />
<b>Summary</b> <br />
Overall, the exception handling is very flexible and can meet almost any scenario you can come up with. For the sake of focusing on the basics, many advanced features haven't been covered here.<br />
<br />
For more details, see these pages on Camel's site...<br />
<br />
<a href="http://camel.apache.org/error-handling-in-camel.html">http://camel.apache.org/error-handling-in-camel.html</a><br />
<a href="http://camel.apache.org/try-catch-finally.html">http://camel.apache.org/try-catch-finally.html</a><br />
<a href="http://camel.apache.org/exception-clause.html">http://camel.apache.org/exception-clause.html</a>Ben ODayhttp://www.blogger.com/profile/02462610652603338525noreply@blogger.com0tag:blogger.com,1999:blog-284240372678709986.post-26196255786823138252010-08-15T16:18:00.000-07:002010-12-27T10:23:08.933-08:00Servicemix 4 development environment setupHere is a quick guide to setting up a development environment for working on Apache Servicemix projects... <br />
<br />
<b>Java 1.6</b><br />
<ul>
<li><a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">download</a> the JDK and run installation</li>
<li>add the /jdk/bin directory to your system path</li>
<li>create JAVA_HOME environment variable pointing to the root JDK directory</li>
</ul>
<b>Maven 2.1</b><br />
<ul>
<li><a href="http://maven.apache.org/download.html">download</a> binary and unzip to a local directory</li>
<li>edit the /bin/mvn.bat file to increase the Maven heap size (MAVEN_OPTS=-Xmx512m)</li>
<li>add the /maven/bin directory to your system path</li>
</ul>
<b>Eclipse 3.4</b><br />
<ul>
<li><a href="http://www.eclipse.org/downloads/">download</a> Eclipse and run installation</li>
<li>open Eclipse</li>
<ul>
<li>create a new workspace</li>
<li>create new project or import any existing projects<b> </b></li>
</ul>
<li>setup m2eclipse plugin</li>
<ul>
<li>go to Help->Software Updates->Available Software tab</li>
<li>then click 'Add Site' and add 'http://m2eclipse.sonatype.org/update/ and call it 'm2eclipse'</li>
<li>select the m2clipse components to install, click finish and restart Eclipse</li>
</ul>
<ul>
<li>go to Windows->Preferences->Maven->Installations</li>
</ul>
<ul><ul>
<li>set the user settings to your maven settings.xml file</li>
<li>verify that this setup the M2_REPO variable to point to your local repo under Java->Build Path->Classpath Variables</li>
</ul>
<li>go to Windows->Preferences->Maven->Archetypes</li>
<ul>
<li>click 'Add Remote Catalog'</li>
<ul>
<li>catalog file = http://repo.fusesource.com/maven2 </li>
<li>name = Fuse Catalog</li>
<li>click OK</li>
</ul>
</ul>
</ul>
</ul>
<b>Fuse ESB 4.2 (aka ServiceMix)</b><br />
<ul>
<li><a href="http://fusesource.com/downloads/">donwload</a> Fuse ESB 4.2 and run installation</li>
</ul>
<b>basic development process</b><br />
<ul>
<li>create a new project using an archetype (Maven template) that best matches your application needs</li>
<ul>
<li>from Eclipse</li>
<ul>
<li>File->New->Maven->Maven Project</li>
<li>check 'use default workspace location' and click next</li>
<li>choose the 'Fuse Catalog' and enter a desire filter</li>
<li>choose an archetype</li>
<li>click next</li>
<li>enter a group id of 'com.<company>.<project>'</li>
<li>enter an artifact of '<component>'</li>
<li>enter a package name of 'com.<company>.<project>.<component>'</li>
<li>click finish </li>
<ul>
<li>note: I have seen issues with some archetypes in Eclipse, if this happens, use the command line approach instead</li>
</ul>
</ul>
<li>from command line</li>
<ul>
<li>mvn archetype:create <br />
-DarchetypeGroupId=org.apache.servicemix.tooling<br />
-DarchetypeArtifactId=servicemix-osgi-camel-archetype<br />
-DarchetypeVersion=2010.01.0-fuse-01-00<br />
-DremoteRepositories=http://repo.fusesource.com/maven2<br /> -DgroupId=com.mycompany.project1<br />
-DartifactId=component1 <br />
-Dversion=0.0.1-SNAPSHOT</li>
</ul>
</ul>
<li> build the project</li>
<ul>
<li>in Eclipse</li>
<ul>
<li>right click on the project, go to Run As->Maven install</li>
</ul>
<li>from command line</li>
<ul>
<li>run 'mvn install'</li>
</ul>
<li>project JAR will be created under the /target directory and deployed it to your local Maven repository</li>
</ul>
<li>deploying the project to Fuse ESB 4.2</li>
<ul>
<li>run /<smx>/bin/servicemix to start the server </li>
<li>make sure any required features/bundles have been installed</li>
<ul>
<li>activemq, camel, etc</li>
</ul>
<li>from the karaf console, install your project bundle </li>
<ul>
<li>osgi:install -s mvn:com.mycompany.project1/component1 </li>
</ul>
<li>assuming that worked, you now have a working project</li>
</ul>
<li>customize project and repeat build/deploy process</li>
</ul>
<b>monitoring</b><br />
<ul>
<li>watch the servicemix log file</li>
<ul>
<li>tail -f /<smx>/data/log/karaf.log</li>
</ul>
<li> JConsole</li>
<ul>
<li><jdk>/bin/jconsole.exe</li>
<li>provides remote/local JVM monitoring</li>
<li>remote connection uri = service:jmx:rmi:///jndi/rmi://:1099/jmxrmi</li>
<li>default user/password = karaf/karaf</li>
</ul>
<li>VisualVM</li>
<ul>
<li><jdk>/bin/jvisualvm.exe</li>
<li>provides remote/local JVM monitoring (slightly better than JConsole)</li>
</ul>
</ul>
<b>other recommended software (for Windows)</b><br />
<ul>
<li><a href="http://www.cygwin.com/">cygwin</a> - adds basic Linux command support for Windows</li>
<li><a href="http://www.powercmd.com/">PowerCMD</a> - tabbed command prompts...trial/not free</li>
<li><a href="http://kdiff3.sourceforge.net/">kdiff3 </a>- excellent file/directory comparison tool</li>
<li><a href="http://www.soapui.org/">SoapUI </a>- for testing HTTP endpoints</li>
</ul>
<br />
If you know of other configurations or tools that are relevant, let me know...<br />
<br />
<ul><ul></ul>
</ul>Ben ODayhttp://www.blogger.com/profile/02462610652603338525noreply@blogger.com1tag:blogger.com,1999:blog-284240372678709986.post-45728381047654498552010-08-14T09:43:00.000-07:002010-08-29T17:50:08.763-07:00OSGi bundle communication optionsThere are various approaches to communicating between bundles...each having its own benefits/tradeoffs. Here are my (in progress) notes on evaluating the options with regards to Fuse ESB 4.2...<br />
<br />
<b>OSGi Service Registry</b><br />
This is the most common (and well documented) approach from a pure OSGi perspective. Simple XML can be used to both register APIs as services and access other bundle's services. The service registry then provides an abstraction layer and an event framework to support dynamic binding, reacting to service lifecycle changes, etc. The <a href="http://felix.apache.org/site/white-board-pattern-handler.html">whiteboard pattern</a> can also help better manage this interaction in more complex/demanding scenarios. <br />
<br />
<b>OSGi Import-Package/Export-Package</b><br />
This approach is more direct and relies on OSGi bundle manifests to tell the classloader which classes to export (make available to other bundles) or import (find classes in other bundles). This has the effect of making any imported classes look like any other class in a bundle. <br />
<br />
However, this tends to tightly couple bundles together and can cause various classloader/versioning issues when bundles get out of sync. <br />
<br />
<b>JMS</b><br />
An ActiveMQ broker is provided with Fuse ESB and is easily setup/configured. JMS endpoints can then easily be setup using Camel to wrap access to POJO Java code, etc. This can easily serve as a bridge between bundles or even VMs/machines entirely. This approach povides the standard JMS benefits (guaranteed messaging, loose coupling, high performance, failover/load balancing, etc). <br />
<br />
Some initial performance concerns usually arise when considering use of persistent XML messaging. Most performance concerns can be mitigated by varying the JMS QoS or even using serialized object messages instead of XML, etc.<br />
<br />
<b>HTTP (SOAP, REST)</b><br />
Another classic approach is to provide HTTP endpoints to interact with a bundle. This provides a loosely coupled strategy bound to either a SOAP WSDL or REST request/response format. Camel components can be used to setup these endpoints and wrap requests to POJO Java code, etc. <br />
<br />
Unfortunately, these endpoints aren't the easiest to configure/test and often impose a significant overhead in terms of marshaling request/response data around.<br />
<br />
<b>NMR</b><br />
Though its roots were in JBI/XML based messaging, it has evolved to provide a more generic/configurable approach to bundle communication. It can be configured to be to be synchronous or asynchronous. Also, the camel-nmr component makes it easy to define NMR endpoints to route data between bundles without the need to use JMS brokers or more complex HTTP definitions. <br />
<br />
See this <a href="http://trenaman.blogspot.com/2010/08/easy-useful-nmr-monsieur-nodet-vous.html">post</a> from Adrian Trenaman for more details on the new NMR... <br />
<br />
<b>VM</b><br />
VM uses asynchronous SEDA to send
messages between entities in the same virtual machine. This has the benefit of using a seperate thread for a producer and consumer. <br />
More to come on this...I'm still working out the differences between VM and NMR in OSGi.
<br />
<br />
<b>Distributed OSGi (DOSGi)</b><br />
This implements the remove services functionality using SOAP web services. Its very new and I'm still coming up to speed on it...(thanks Ashwin for pointing this out)
<br />
<br />
More to come on this...see these page for now.
<br />
<br />
<a href="http://cxf.apache.org/distributed-osgi.html" rel="nofollow" target="_blank">http://cxf.apache.org/distributed-osgi.html</a>
<br />
<a href="http://osgithoughts.blogspot.com/2010/07/cxf-distributed-osgi-12-is-out.html" rel="nofollow" target="_blank">
http://osgithoughts.blogspot.com/2010/07/cxf-distributed-osgi-12-is-out.html</a>
<br /><br />
<b>summary</b><br />
This is clearly still a work in progress. There are a lot of options and I'm still trying to get my head around the trade-offs of each approach (hence this page). As time permits, I'll try to expand on these more... <br />
<br />
If anyone has had good/bad experiences with these techniques or others, let me know...Ben ODayhttp://www.blogger.com/profile/02462610652603338525noreply@blogger.com0tag:blogger.com,1999:blog-284240372678709986.post-24144176220200453252010-08-13T21:50:00.000-07:002010-08-20T15:22:32.436-07:00Fuse ESB 4.2 quick start guideHere are some notes of getting started using Fuse ESB 4.2 (aka ServiceMix 4).
<br /><br />
For full documentation, <a href="http://fusesource.com/products/enterprise-servicemix/#documentation">click here</a>
<br /><br />
<b>install/config</b>
<ul>
<li>prerequisites</li>
<ul>
<li>required software - JDK 1.5+</li>
<li>recommended software - Maven 2.1+, Eclipse 3.4+</li>
<li>see <a href="http://benoday.blogspot.com/2010/08/servicemix-developer-machine-setup.html">this guide</a> for more details</li>
</ul>
<li>download Fuse ESB 4.2 <a href="http://fusesource.com/downloads">here </a>and follow the installation instructions</li>
</ul>
<b>basic structure</b>
<ul>
<li>/bin - executable files</li>
<li>/etc - configuration files</li>
<li>/deploy - used to deploy bundles</li>
<li>/data - runtime files</li>
</ul>
<b>basic configuration</b>
<ul>
<li>edit the "org.apache.felix.karaf.features.cfg" file to set necessary startup features (camel, activemq, etc)</li>
</ul>
<b>start server</b>
<ul>
<li>cd /[fuse]/bin</li>
<li>run 'servicemix' (starts server and karaf shell)</li>
</ul>
<b>start shell against a running server</b>
<ul>
<li>ssh -p 8101 karaf@localhost (default password:karaf)</li>
</ul>
<b>frequently used commands (for a full list <a href="http://fusesource.com/docs/esb/4.2/command_ref/ESBintro.html">click here</a>)</b>
<ul>
<li>managing features</li>
<ul>
<li>features:list </li>
<li>features:listUrl </li>
<li>features:uninstall [name]</li>
<li>features:install [name]</li>
</ul>
<li>managing bundles</li>
<ul>
<li>osgi:uninstall [id]</li>
<li>osgi:install -s file:/bundles/example-1.0.jar</li>
<li>osgi:install -s mvn:com.company/example/1.0</li>
<li>osgi:update [id] (refresh from install location)</li>
<li>osgi:refresh [id] (reinitialize the bundle)</li>
</ul>
<li>monitoring</li>
<ul>
<li>osgi:list (list all bundles)</li>
<li>osgi:list | grep test</li>
<li>osgi:headers (view bundle header info)</li>
<li>osgi:headers | grep test</li>
<li>log:display</li>
<li>log:set [level] [log package] (log:set DEBUG com.foo.bar)</li>
<li>shutdown (stop servicemix)</li>
</ul>
</ul>
<b>other tips</b>
<ul>
<li>monitor log file</li>
<ul>
<li>tail -f /[fuse]/data/log/karaf.log</li>
</ul>
<li>deploy bundles</li>
<ul>
<li>osgi:install or move bundle to the [fuse]/deploy directory</li>
</ul>
</ul>Ben ODayhttp://www.blogger.com/profile/02462610652603338525noreply@blogger.com0tag:blogger.com,1999:blog-284240372678709986.post-2791696130032012292010-08-12T22:31:00.000-07:002010-08-27T12:23:00.697-07:00Spring property-placeholder with OSGiHere are some notes on adding support for properties files to a project. I have it deployed as an OSGI bundle on Fuse ESB 4.2 (aka Apache ServiceMix), but it can be used in various containers/configurations.
Anyways, here are the steps...<br />
<br />
<span style="font-weight: bold;">first, add these required Maven dependencies to your project...</span><br />
<dependency><br />
<groupid>org.springframework.osgi</groupid><br />
<artifactid>spring-osgi-core</artifactid><br />
<version>1.2.0</version><br />
</dependency><br />
<dependency><br />
<groupid>org.springframework</groupid><br />
<artifactid>spring-context</artifactid><br />
<version>2.5.6.SEC01</version><br />
</dependency><br />
<br />
<span style="font-weight: bold;">then, add the following spring XML..</span><br />
<beans xmlns:context="http://www.springframework.org/schema/context"<br />
xmlns:osgi="http://www.springframework.org/schema/osgi"<br />
xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"<br />
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br />
xmlns="http://www.springframework.org/schema/beans"<br />
xsi:schemalocation="<br />
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd<br />
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd<br />
http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd<br />
http://www.springframework.org/schema/osgi-compendium http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd"><br />
<osgix:cm-properties id="props" persistent-id="my.props"><br />
<prop key="property.name">default value</prop><br />
</osgix:cm-properties><br />
<context:property-placeholder properties-ref="props"></context:property-placeholder><br />
<bean id="myBean" lass="com.company.MyClass"><br />
<property name="property" value="${property.name}"></property><br />
</bean><br />
</beans>
<span style="font-weight: bold;"> </span><br />
<br />
<span style="font-weight: bold;">finally, create a properties file...</span>
<br />
add a file called "my.props.cfg" to your container's classpath (/fuse/etc directory, etc) that contains key=value line separated properties...
<br />
<br />
<b>tips on using this in practice...</b><br />
<ul>
<li>the filename must match the <osgix:cm-properties> persistent-id attribute and end with ".cfg". also, avoid using dashes in the filename...</li>
<li>this will NOT work in unit tests, you'll need to override your spring XML file under /src/test/resources, remove references to osgi-compendium and hardcode the properties</li>
</ul>
<b>another option altogether...use the SystemClassLoader<br />
</b> put your properties files in the /servicemix/conf/ directory (doesn't
exist by default). This is in a bundle's classpath and can be loaded as follows...<br />
<br />
try {<br />
Properties prop = new Properties();<br />
ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();<br />
InputStream is = sysClassLoader.getResourceAsStream("my.properties");<br />
prop.load(is);<br />
} catch (Exception e) {<br />
logger.error("error getting props->" + e);<br />
}<br />
<br /> This won't leverage the osgi benefits, but is simple and will work for most cases...<br /><ul>
</ul>Ben ODayhttp://www.blogger.com/profile/02462610652603338525noreply@blogger.com1