Labels

algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)

Approximate Search or Spell Check using Trie in Java

the sample code in java which uses  TRIE ds can be found <<Click here>>

the main program which has to be run is MainRunner.Java

modify the searchString variable in the main program.

books.txt : this file has the list of books which are indexed for the approximate search.

commonwords.txt this file has the words which will be ignored.

Features:

can detect 4 types of errors

the exact word say is programming
1) Transposition : prorgamming
2) Insertion: programNming
3) Deletion: programing {m is deleted}
4) Substitution: projramming

would all list the results of books which have programming text.







Service Mix Drools

Adding A Service mix drools  

mvn archetype:create  -DarchetypeGroupId=org.apache.servicemix.tooling  -DarchetypeArtifactId=servicemix-drools-service-unit  -DarchetypeVersion=2010.01  -DgroupId=com.bytes.tutorial  -DartifactId=drools-su -Dversion=1.01

groupId, Value: com.bytes.tutorial
packageName, Value: com.bytes.tutorial
package, Value: com.bytes.tutorial
version, Value: 1.01
artifactId, Value: com.bytes.drools




here is the example 

i have xml files in a folder C:\SOURCE and the content of them is some thing like this below, based on the content

the drools will be configured in such a way that their destination is decided by the drools.
File1
<?xml version="1.0" encoding="UTF-8"?>
<test id="1">
this file matches rule 1
</test>

File2
<?xml version="1.0" encoding="UTF-8"?>
<test id="2">
this file matches rule 2
</test>

File3
<?xml version="1.0" encoding="UTF-8"?>
<test id="3">
this file matches rule 3
</test>

Then the D-rools are configured like this.
the above rules check the id attribute present in the xml based on that 
they send the messages to one of file_sender1,file_sender2,file_sender3.
package org.apache.servicemix.drools

import org.apache.servicemix.drools.model.Exchange;

global org.apache.servicemix.drools.model.JbiHelper jbi;

rule "Route to target1"
when
me : Exchange( status == Exchange.ACTIVE, $in : in != null )
eval( $in.xpath("/test/@id = 1") )
then
jbi.route( "service:http://servicemix.apache.org/bytes/file_sender1" );
end

rule "Route to target2"
when
me : Exchange( status == Exchange.ACTIVE, $in : in != null )
eval( $in.xpath("/test/@id = 2") )
then
jbi.route( "service:http://servicemix.apache.org/bytes/file_sender2" );
end

rule "Route to target3"
when
me : Exchange( status == Exchange.ACTIVE, $in : in != null )
eval( $in.xpath("/test/@id = 3") )
then
jbi.route( "service:http://servicemix.apache.org/bytes/file_sender3" );
end


File Poller 
<file:poller service="bytes:file_poller" endpoint="file-poller"
targetService="bytes:serviceNameDR"
targetEndpoint="drools" 
file="file:C:/SOURCE">

File Sender
<file:sender service="bytes:file_sender1"
endpoint="file-sender"
directory="file:C:/DESTINATION1" />
<file:sender service="bytes:file_sender2"
endpoint="file-sender"
directory="file:C:/DESTINATION2" />
<file:sender service="bytes:file_sender3"
endpoint="file-sender"
directory="file:C:/DESTINATION3" />

the sample code can be found here download



Caused by: org.drools.CheckedDroolsException : Service Mix Drools

The default template generated by the maven for service mix drools su has some errors like

Caused by: org.drools.CheckedDroolsException: There were errors in the rule source:

Rule Compilation error : [Rule name=Unspecified id, agendaGroup=MAIN, salience=0, no-loop=false]
org/apache/servicemix/drools/Rule_Unspecified_id_0.java (8:292) : in cannot be resolved

it says in Cannot be resolved, the way the default router.drl was generated needs to be corrected,

to access the in Message it has to be prefixed with “$” so it should be $in

rule "Route to target1"
when
me : Exchange( status == Exchange.ACTIVE, $in : in != null )
eval( $in.xpath("/bar:test/@id = 1") )
then
jbi.route( "bytes::file_sender1" );
end





MORPHIC

Morphic is the name given to Pharo’s graphical interface.it does not have separate modes for “composing” and “running” the interface: all the graphical elements can be assembled and disassembled by the user, at any time.
You can create a morph to represent any object, although how good a representation you get depends
on the object!
To create a morph to represent a string object, execute the following code in aworkspace.

                              'Morph' asMorph openInWorld

This creates a Morph to represent the string 'Morph', and then opens it (that is, displays it) in the “world”, which is the name that Pharo gives to the screen. You should obtain a graphical element—a Morph—which you can manipulate by meta-clicking.




MANIPULATING MORPHS:
Morphs are objects, so we can manipulate them like any other object in
Smalltalk: by sending messages, we can change their properties, create new
subclasses of Morph
  • Every morph, even if it is not currently open on the screen, has a position
    and a size.
  • All morphs are considered to occupy a rectangular region of the screen; if they are irregularly shaped, their position and size are those of the smallest rectangular “box” that surrounds them, which is
    known as the morph’s bounding box, or just its “bounds”.
  • The position method returns a Point that describes the location of the morph’s upper left corner (or the upper left corner of its bounding box).
  • The origin of the coordinate system is the screen’s upper left corner, with y coordinates increasing down the screen and x coordinates increasing to the right.
  • The extent method also returns a point, but this point specifies the width and height of the morph
    rather than a location.
    "Type the following code into a workspace and do it :"
                  joe := Morph new color: Color blue.
                  joe openInWorld.
                  bill := Morph new color: Color red .
                  bill openInWorld.
    Then type joe position and print it . To move joe, execute joe position: (joe position +(10@3)) repeatedly.

Design Patterns in the JDK

Design Patterns in the JDK - Java Code Geeks

javax.jbi.messaging.MessagingException: Out not supported

Scenario: FilePoller = > EIP Pipeline => {transformer is the bean below} => File Sender { target }
the exception was at the bean. Complete Exception is below
javax.jbi.messaging.MessagingException: Out not supported
at org.apache.servicemix.jbi.messaging.MessageExchangeImpl.setMessag(MessageExchangeImpl.java:357)
at com.bytes.tutorial.MyBean.onMessageExchange(MyBean.java:46)
at org.apache.servicemix.bean.BeanEndpoint.onProviderExchange(BeanEndpoint.java:230)
at org.apache.servicemix.bean.BeanEndpoint.process(BeanEndpoint.java:217)
at org.apache.servicemix.common.AsyncBaseLifeCycle.doProcess(AsyncBaseLifeCycle.java:627)
at org.apache.servicemix.common.AsyncBaseLifeCycle.processExchange(AsyncBaseLifeCycle.java:581)
at org.apache.servicemix.common.AsyncBaseLifeCycle.onMessageExchange(AsyncBaseLifeCycle.java:535)
at org.apache.servicemix.common.SyncLifeCycleWrapper.onMessageExchangeSyncLifeCycleWrapper.java:60)
at org.apache.servicemix.jbi.messaging.DeliveryChannelImpl.processInBound(DeliveryChannelImpl.java:632)
at org.apache.servicemix.jbi.nmr.flow.AbstractFlow.doRouting(AbstractFlow.java:185)
at org.apache.servicemix.jbi.nmr.flow.seda.SedaFlow.doRouting(SedaFlow.java:168)
at org.apache.servicemix.jbi.nmr.flow.seda.SedaQueue$1.run(SedaQueue.java:134)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:650)


  public void onMessageExchange(MessageExchange exchange)
throws MessagingException {
System.out.println("Received exchange: " + exchange);
System.out.println("getPattern exchange: " + exchange.getPattern());
System.out.println("getStatus exchange: " + exchange.getStatus());
System.out.println("Role exchange: " + exchange.getRole().toString());
NormalizedMessage in = exchange.getMessage("in");
exchange.setMessage(in, "out");
channel.send(exchange);
}

is changed to have a check

        if (exchange.getStatus() == ExchangeStatus.DONE)
return;
or
  if (exchange.getStatus() == ExchangeStatus.ACTIVE){
System.out.println("Received exchange: " + exchange);
System.out.println("getPattern exchange: " + exchange.getPattern());
System.out.println("getStatus exchange: " + exchange.getStatus());
System.out.println("Role exchange: " + exchange.getRole().toString());
NormalizedMessage in = exchange.getMessage("in");
exchange.setMessage(in, "out");
channel.send(exchange);
}



Failed to read artifact descriptor for bouncycastle:bcprov-jdk13:jar

Failed to read artifact descriptor for bouncycastle:bcprov-jdk13:jar:132: Could not transfer artifact bouncycastle:bcprov-jdk13:pom:132 from java.net (http://download.java.net/maven/1): No connector available to access repository java.net (http://download.java.net/maven/1) of type legacy using the available factories WagonRepositoryConnectorFactory -> [Help 1]


WorkAround

install maven 2.2.1 it solved my problem , i was using 3.0.3 before

ServiceMix EIP Pipeline

MVN command to create a eip
mvn archetype:generate -DarchetypeGroupId=org.apache.servicemix.tooling -DarchetypeArtifactId=servicemix-eip-service-unit -DarchetypeVersion=2010.01 -DgroupId=com.bytes.tutorial -DartifactId=eip-su -Dversion=1.01
[INFO] Using property: groupId = com.bytes.tutorial
[INFO] Using property: artifactId = eip-su
[INFO] Using property: version = 1.01
[INFO] Using property: package = com.bytes.tutorial
Confirm properties configuration:
groupId: com.bytes.tutorial
artifactId: eip-su
version: 1.01
package: com.bytes.tutorial
Y: y
the eip su is then created ,add this as dependency to the SA
<dependency>

<groupId>com.bytes.tutorial</groupId>

<artifactId>eip-su</artifactId>

<version>1.01</version>

</dependency>
the XBEAN.XML generated will have all examples of the patterns that can be used.
we can use pipe pattern
Source DIR => File-Poller => bytes:pipeline=> myBeanService=> File-Sender => Destination DIR
<eip:pipeline service="bytes:eipPipeBean" endpoint="endpoint">
<eip:transformer>
<eip:exchange-target service="bytes:myBeanService" />
</eip:transformer>
<eip:target>
<eip:exchange-target service="bytes:file_sender" />
</eip:target>
</eip:pipeline>



As show above this EIP pipeline is configured in such a way that


the bean “bytes:myBeanService” processes the data and gives it to the bytes:file_sender .


that should take care of writing the files to that directory.


find the sample code here => Example

Exception Could not find route for exchange: Service Mix

WARN  - FileComponent                  - Target service (fileTObean) and endpoi
t (endpoint) specified, but no matching endpoint found.  Only the service will
e used for routing.
WARN  - DefaultBroker                  - ServiceName (fileTObean) specified for
routing, but can't find it registered
WARN  - DefaultBroker                  - ServiceName (fileTObean) specified for
routing, but can't find it registered
ERROR - FileComponent                  - Failed to process file: D:\boyapati\De
ktop\ALARM\books.xml. Reason: javax.jbi.messaging.MessagingE
ception: Could not find route for exchange: InOnly[
  id: ID:10.142.216.169-12e80f2505a-14:137

this can be because of Name space

check xbean.xml
there is a text generated "xmlns:replaceMe="http://servicemix.apache.org/replaceMe" "
this name space has to be same for a service to find another service. :)

Adding a service Mix Bean

MVN Command used to create bean SU
mvn archetype:generate -DarchetypeGroupId=org.apache.servicemix.tooling
-DarchetypeArtifactId=servicemix-bean-service-unit -DarchetypeVersion=2010.01
-DgroupId=com.bytes.tutorial -DartifactId=bean-su -Dversion=1.01
1K downloaded  (servicemix-bean-service-unit-3.2.3.pom)
[INFO] Using property: groupId = com.bytes.tutorial
[INFO] Using property: artifactId = bean-su
[INFO] Using property: version = 1.01
[INFO] Using property: package = com.bytes.tutorial
Confirm properties configuration:
groupId: com.bytes.tutorial
artifactId: bean-su
version: 1.01
package: com.bytes.tutorial
Y: y
and the bean su is added to the parent folder, for it to be part of an assembly an entry has to be added into the assembly
like this under dependencies tag of sa’s pom.xml
<dependency>
<groupId>org.apache.servicemix</groupId>
<artifactId>servicemix-core</artifactId>
<version>${servicemix-version}</version>
<scope>provided</scope>
</dependency>

A bean class MyBean is created automatically along with the entry in the xbean.xml
package com.bytes.tutorial;

import javax.annotation.Resource;
import javax.jbi.messaging.DeliveryChannel;
import javax.jbi.messaging.ExchangeStatus;
import javax.jbi.messaging.InOnly;
import javax.jbi.messaging.InOut;
import javax.jbi.messaging.MessageExchange;
import javax.jbi.messaging.MessageExchangeFactory;
import javax.jbi.messaging.MessagingException;
import javax.jbi.messaging.NormalizedMessage;
import javax.xml.namespace.QName;

import org.apache.servicemix.MessageExchangeListener;
import org.apache.servicemix.jbi.messaging.NormalizedMessageImpl;
import org.apache.servicemix.jbi.util.MessageUtil;

public class MyBean implements MessageExchangeListener {

@Resource
private DeliveryChannel channel;

public void onMessageExchange(MessageExchange exchange)
throws MessagingException {

if (exchange.getStatus() == ExchangeStatus.ACTIVE)
{
System.out.println("Received exchange: " + exchange);
System.out.println("getPattern exchange: " + exchange.getPattern());
System.out.println("getStatus exchange: " + exchange.getStatus());
System.out.println("Role exchange: " + exchange.getRole().toString());
NormalizedMessage in = exchange.getMessage("in");
exchange.setMessage(in, "out");
channel.send(exchange);
       }
}
}






the above bean just consumes a message exchange and prints the message and sends it back to the channel.
<bean:endpoint service="bytes:myBeanService" endpoint="endpoint" bean="#myBean"/>

<bean id="myBean" class="com.bytes.tutorial.MyBean"/>
Configure the bean as the above service so that it can be called from the EIP Pipeline.



we will see in the next post how to integrate the file poller the sender and this bean which will process the data in between.


http://www.snippets.24bytes.com/2011/03/servicemix-eip.html

Hello World File SU (service Mix)

Create a Maven Project
create a empty folder and create a pom.xml with the following content
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bytes.tutorial</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>bytes</name>
<url>http://servicemix.org</url>
<modules>
<module>bytes-sa</module>
</modules>
</project>

Create a Service Assembly

mvn archetype:generate -DarchetypeArtifactId=servicemix-service-assembly
-DarchetypeGroupId=org.apache.servicemix.tooling -DartifactId=bytes-sa

(Note: this will also add an entry into the above pom.xml dependency list)
you will get a prompt asking for
Define value for property 'groupId': : com.bytes.tutorial
[INFO] Using property: artifactId = bytes-sa
Define value for property 'version': 1.0-SNAPSHOT:
Define value for property 'package': com.bytes.tutorial:
Confirm properties configuration:
groupId: com.bytes.tutorial
artifactId: bytes-sa
version: 1.0-SNAPSHOT
package: com.bytes.tutorial
Y: y

Create a Service Unit(File SU)

Command to create a SU

mvn archetype:generate -DarchetypeGroupId=org.ap
ache.servicemix.tooling -DarchetypeArtifactId=servicemix-file-service-unit -Darc
hetypeVersion=2010.01 -DgroupId=com.bytes.tutorial -DartifactId=file-su


[INFO] Using property: groupId = com.bytes.tutorial
[INFO] Using property: artifactId = file-su
[INFO] Using property: version = 1.0
[INFO] Using property: package = com.bytes.tutorial
Confirm properties configuration:
groupId: com.bytes.tutorial
artifactId: file-su
version: 1.0
package: com.bytes.tutorial
Y: y

RUN mvn install

---------------------------------------------------------------
Reactor Summary:
---------------------------------------------------------------
bytes ............................................... SUCCESS
bytes SA ............................................ SUCCESS
Apache ServiceMix :: File Service Unit ................ SUCCESS

---------------------------------------------------------------
---------------------------------------------------------------
BUILD SUCCESSFUL

which means the project is configured successfully.

FILE-SU CONFIGURATION

we will configure the FILE-SU in such a way that the poller polls a director for valid XML files and gives them to file-sender ,

the file sender copies them into another folder

POLLER < =   D:\SOURCE
||
V
SENDER =>  D:\DESTINATION
open the XBEAN.XML under FILE-SU  you would find a poller tag like the one below
POLLER CONFIG
  <file:poller service="bytes:file_poller" endpoint="file-poller"
targetService="bytes:file_sender"
targetEndpoint="file-sender"
file="file:C:/SOURCE">





  1. Give the name of the poller service and its end point.
  2. The target service, end point to which the file polled is sent.
  3. File: the directory which has to be polled regularly for XML files.
SENDER CONFIG

<file:sender service="bytes:file_sender"
endpoint="file-sender"
directory="file:/D:/DESTINATION">






  1. name of the sender service should match the targetService of the poller 

  2. configure the directory where the files should be sent “directory”
do an mvn install and then go to the service assembly folder
and pick the jar file from the target and deploy in the hotdeploy 
folder of service mix and see the MAGIC.
download the sample here

Search 24 Bytes

Loading...