יום רביעי, 21 במאי 2014

Ruby define_method simple sample‏

The following Ruby code demonstrates the use of rubies define_method in order to define methods dynamically.

class MyCls
      %w(getData1 getData2).each do |meth|
      define_method(meth) {
        @data[meth.to_sym] = @data[:User] * 2
        @data[meth.to_sym]
        }
  def initialize()
    @data = {}
    @data[:User]= 15 
    end
  end
end
theMyCls = MyCls.new()
puts  theMyCls.getData2
 

Resources
#http://ruby-doc.org/core-2.0/Module.html#method-i-define_method
#http://www.trottercashion.com/2011/02/08/rubys-define_method-method_missing-and-instance_eval.html

יום שלישי, 20 במאי 2014

Zero MQ Pub Sub Simple Sample

The following is a simple 0MQ tcp pub sub simple sample:
The main:

        ZMQ.Context context = ZMQ.context(1);
   
        ZeroMqPubServer theZeroMqPubServer = new ZeroMqPubServer(context);
        theZeroMqPubServer.start();
      
        ZeroMqSubClient theZeroMqSubClient = new ZeroMqSubClient(context);
        
        theZeroMqSubClient.start();
The server

import java.util.logging.Level;
import java.util.logging.Logger;
import org.zeromq.ZMQ;
public class ZeroMqPubServer  extends Thread{
    
    ZMQ.Context  mContext;
    public ZeroMqPubServer(ZMQ.Context pContext) {
        this.mContext = pContext;
    }
    
     @Override
     public void run()
     {
         ZMQ.Socket publisher = mContext.socket(ZMQ.PUB);
        publisher.bind("tcp://*:5556");
        int theCounter = 0 ; 
         while (!Thread.currentThread ().isInterrupted ()) {
            try {
                theCounter++;
                //  Send message to all subscribers
                String update = String.format("MyTopic %d ", theCounter);
                
                publisher.send(update, 0);
                
                Thread.sleep(100);
            }
            // context.term ();
            catch (InterruptedException ex) {
                Logger.getLogger(ZeroMqPubServer.class.getName()).log(Level.SEVERE, null, ex);
            }          
        }
     }
}

The client


import java.nio.charset.Charset;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zeromq.ZMQ;
public class ZeroMqSubClient  extends Thread{
    
      ZMQ.Context  mContext;
    public ZeroMqSubClient(ZMQ.Context pContext) {
        this.mContext = pContext;
    }
    
    
     @Override
     public void run()
     {
          try {
              Thread.sleep(3000 );
              
              ZMQ.Socket subscriber = mContext.socket(ZMQ.SUB);
              subscriber.connect("tcp://localhost:5556");
              
              //  Subscribe to zipcode, default is NYC, 10001
              String filter =  "MyTopic";
              subscriber.subscribe(filter.getBytes());
              
              while (!Thread.currentThread ().isInterrupted ()) {
                  
                  //  Use trim to remove the tailing '0' character
                  String theStr = subscriber.recvStr(Charset.defaultCharset());
                  
                  System.out.println("Recieved:" + theStr);
              }
              subscriber.close();
              //  mContext.term();
          } catch (InterruptedException ex) {
              Logger.getLogger(ZeroMqSubClient.class.getName()).log(Level.SEVERE, null, ex);
          }
    }
}

The result
debug:
Start
Recieved:MyTopic 31
Recieved:MyTopic 32
Recieved:MyTopic 33
Recieved:MyTopic 34
Recieved:MyTopic 35
Recieved:MyTopic 36
Recieved:MyTopic 37
Recieved:MyTopic 38
Recieved:MyTopic 39
Recieved:MyTopic 40
Recieved:MyTopic 41
Recieved:MyTopic 42
Recieved:MyTopic 43
Recieved:MyTopic 44
Recieved:MyTopic 45
Recieved:MyTopic 46
Recieved:MyTopic 47
Recieved:MyTopic 48
Recieved:MyTopic 49
Recieved:MyTopic 50
Recieved:MyTopic 51
Recieved:MyTopic 52
Recieved:MyTopic 53
Recieved:MyTopic 54
Recieved:MyTopic 55

יום רביעי, 14 במאי 2014

functools partial

The functools.partial allow to  wrapped a method with anew method that has only  partial of the parameters of the wrapped function .
In constract to using lambda for this ‘the functools.partial keep the parameters as reference that can be changed after declaring the wrapper function
Example:

import functools
fullName = lambda pName , pFamily : pName + " "+ pFamily
n = "Zvika"
GetName = lambda y: fullName(n, y)
GetName2 = functools.partial(fullName, n)
print ("n set before creating the lambda")
print (GetName("Peer"), GetName2("Peer"))
print ("n set after creating the lambda")
n = "Alon"
print (GetName("Peer"), GetName2("Peer"))

And the results: 
 n set before creating the lambda
Zvika Peer Zvika Peer
n set after creating the lambda
Alon Peer Zvika Peer


Resources:
http://stackoverflow.com/questions/3252228/python-why-is-functools-partial-necessary

functools @wraps

The functools @wrap allow to keep attributed function original  name
For an example:

from functools import wraps
def myAttribute(func):
    @wraps(func)
    def WrapprintName (*args, **kwargs):
        print ("The method is: " + func.__name__ )
        return func(*args, **kwargs)
    return WrapprintName
@myAttribute
def printName (pName ):
    """Print my name """
    print (pName)
print (printName.__name__)  # prints 'f'
print (printName.__doc__)   # prints 'does some math'
printName ("Zvika")
And the result:
printName
Print my name
The method isprintName
Zvika

The printName method name was kept although the printName method was annotated with @myAttribute

יום ראשון, 11 במאי 2014

Using Jetty and Camel

I wants to switch from Tomcat to jetty in my Camel CFXRS sample .
The first stage is a basic test of the combination of Jetty and Camel.
Add to the pom:

<groupId>org.apache.camel</groupId>
	<artifactId>camel-jetty</artifactId>
	<version>2.12.1</version>
Note: All the Camel compoenents version must match otherwise linkage between components problems may raise.
Add a very simple Route :

   public void configure() {
       
        from("jetty:http://localhost:8080/SayHello").process(new ReplaySimpleHtml());

The request – replay processor:


public class ReplaySimpleHtml implements Processor {
    public void process(Exchange exchange) throws Exception {
        // just get the body as a string
        String body = exchange.getIn().getBody(String.class);
 
        // we have access to the HttpServletRequest here and we can grab it if we need it
        HttpServletRequest req = exchange.getIn().getBody(HttpServletRequest.class);
     
        // send a html response
        exchange.getOut().setBody("<html><body><H3>Hello Mazal Saadon</H3></body></html>");
    }
}

Executing the sample result:


Capture59
Resources:
http://camel.apache.org/jetty.html

should-you-always-pass-the-bare-minimum-data-needed-into-a-function‏

I have found the following architecture dilemma in the following post In my opinion the method: void Authenticate (User pUser )  cause decoupling between the User class and the authenticator class.
In case we will want to authenticate a Roll the Roll should implement inherits from 
User. 
What about IAuthenticatee interface?
This raise the problem of authenticate not using name password group , so I change the name of the interface to INPGAuthenticatee  
Note : 
I think that creating an interface INPGAuthenticatee   that inherits from IAuthenticatee  make thinks too complicated at the first stages of the development and design process in order to KISS at the first stage we will use just the INPGAuthenticatee    interface and if there will be a need to authenticate using other information the authentication  in advanced stages  of the project the authentication mechanism will be refactor. 

I don’t like the option to give the User option to call authenticate directly because it will prevent the option to apply policy for example after 3 authentication failure to lock the account. 
(The policy component should be separate from the authenticate component)

Capture58

Resources:

http://programmers.stackexchange.com/questions/216371/should-you-always-pass-the-bare-minimum-data-needed-into-a-function‏


 

יום שישי, 9 במאי 2014

asyncRequestBody con’t 1

I wanted to check the asyncRequestBody method in more complicated scenarios from the last post.
I Created the following route

   public void configure() {
        
        from("direct:cafeEnterence").to("bean:hostess?method=getTable").to("seda:nextStage");
        
        from("seda:nextStage").to("bean:hostess?method=getVipTable");

and I changed the hostess class to


public class hostess {
    public CafeTable getTable (Guest pGuest)
    {
       System.out.println ("No of people  :" + Integer.toString(pGuest.getmNoOfP()));
        
        return new CafeTable ("Not in the smoking Zone");
    }    
    public CafeTable getVipTable (CafeTable pCafeTable)
    {
        return new CafeTable (pCafeTable.getmLocation() + " and in vip area." );
    }
}

Executing the code:
No of people  :4
The table :CafeTable{mLocation=Not in the smoking Zone and in vip area.}

The future waits for the route to end until it executes its code.

Now lets split the route
Add the guestspliter


public class GuestSplitter {
      public List<Guest> split(Guest pGuest) {
          List<Guest> RetValue = new ArrayList<Guest>();
          for ( int i = 0 ; i < pGuest.getmNoOfP() ; i ++)
          {
              RetValue.add(new Guest ( i) );
          }
          
          return RetValue;
    }
}

Change the route


 from("direct:cafeEnterence").split().method("GuestSplitter").to("bean:hostess?method=getTable");

Now the response of
CafeTable response = future.get(1, TimeUnit.MINUTES);
is null




How to run ZeroMQ with Java in windows7

In order to run 0MQ with java in windows follow the following steps:
1.Compile the libzmq dll 
Capture52

Set the output lib of the libzmq project
Capture51

Don’t forget to compile for x64 Bit

The compilation result should be:
1> Creating library libzmq.lib and object libzmq.exp

 

1> libzmq.vcxproj -> H:\deleteme100\zeromq-4.0.4\builds\msvc\libzmq\../../../bin/x64/libzmq_d.dll

========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

 

2.Compile the jzmq dll.
Capture53
Set the libzmq library in the project additional dependencies
Capture54
Set the jdk and the zero mq header files pathes in the c++ include directory .
Capture55
Compile the project in x64 bit .
The output should be :

1>------ Rebuild All started: Project: jzmq, Configuration: Release x64 ------

1> The syntax of the command is incorrect.

1> Note: Some input files use or override a deprecated API.

1> Note: Recompile with -Xlint:deprecation for details.

1> Generating JNI header

1> Generating JNI header

1> Generating JNI header

1> Generating JNI header

1> Generating JNI header

1> Generating JNI header

1> Generating JNI header

1> Context.cpp

1> Poller.cpp

1>..\..\..\src\main\c++\Poller.cpp(76): warning C4244: '=' : conversion from 'jint' to 'short', possible loss of data

1> Socket.cpp

1>..\..\..\src\main\c++\Socket.cpp(266): warning C4267: 'argument' : conversion from 'size_t' to 'jsize', possible loss of data

1>..\..\..\src\main\c++\Socket.cpp(272): warning C4267: 'argument' : conversion from 'size_t' to 'jsize', possible loss of data

1>..\..\..\src\main\c++\Socket.cpp(834): warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data

1>..\..\..\src\main\c++\Socket.cpp(862): warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data

1> util.cpp

1> ZMQ.cpp

1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\Microsoft.CppBuild.targets(1137,5): warning MSB8012: TargetPath(H:\deleteme101\jzmq-master\builds\msvc\x64\Release\jzmq.dll) does not match the Linker's OutputFile property value (H:\deleteme101\jzmq-master\lib\jzmq.dll). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Link.OutputFile).

1> Creating library H:\deleteme101\jzmq-master\builds\msvc\x64\Release\jzmq.lib and object H:\deleteme101\jzmq-master\builds\msvc\x64\Release\jzmq.exp

1> Generating code

1> Finished generating code

1> jzmq.vcxproj -> H:\deleteme101\jzmq-master\builds\msvc\x64\Release\jzmq.dll

1> added manifest

1> adding: org/zeromq/App.class(in = 2433) (out= 1352)(deflated 44%)

1> adding: org/zeromq/EmbeddedLibraryTools.class(in = 4442) (out= 2473)(deflated 44%)

1> adding: org/zeromq/ZContext.class(in = 2621) (out= 1333)(deflated 49%)

1> adding: org/zeromq/ZDispatcher$1.class(in = 206) (out= 161)(deflated 21%)

1> adding: org/zeromq/ZDispatcher$SocketDispatcher$1.class(in = 861) (out= 413)(deflated 52%)

1> adding: org/zeromq/ZDispatcher$SocketDispatcher$2.class(in = 2034) (out= 829)(deflated 59%)

1> adding: org/zeromq/ZDispatcher$SocketDispatcher$ZMessageBuffer.class(in = 1395) (out= 653)(deflated 53%)

1> adding: org/zeromq/ZDispatcher$SocketDispatcher.class(in = 4184) (out= 1790)(deflated 57%)

1> adding: org/zeromq/ZDispatcher$ZMessageHandler.class(in = 340) (out= 213)(deflated 37%)

1> adding: org/zeromq/ZDispatcher$ZSender.class(in = 780) (out= 436)(deflated 44%)

1> adding: org/zeromq/ZDispatcher.class(in = 2778) (out= 1254)(deflated 54%)

1> adding: org/zeromq/ZFrame.class(in = 4765) (out= 2642)(deflated 44%)

1> adding: org/zeromq/ZMQ$Context.class(in = 1262) (out= 711)(deflated 43%)

1> adding: org/zeromq/ZMQ$Error.class(in = 2545) (out= 1433)(deflated 43%)

1> adding: org/zeromq/ZMQ$Event.class(in = 1444) (out= 732)(deflated 49%)

1> adding: org/zeromq/ZMQ$Poller.class(in = 4092) (out= 2007)(deflated 50%)

1> adding: org/zeromq/ZMQ$PollItem.class(in = 1651) (out= 736)(deflated 55%)

1> adding: org/zeromq/ZMQ$Socket.class(in = 11809) (out= 4589)(deflated 61%)

1> adding: org/zeromq/ZMQ.class(in = 3529) (out= 1717)(deflated 51%)

1> adding: org/zeromq/ZMQException.class(in = 771) (out= 485)(deflated 37%)

1> adding: org/zeromq/ZMQForwarder.class(in = 1464) (out= 870)(deflated 40%)

1> adding: org/zeromq/ZMQQueue.class(in = 1860) (out= 1041)(deflated 44%)

1> adding: org/zeromq/ZMQStreamer.class(in = 424) (out= 260)(deflated 38%)

1> adding: org/zeromq/ZMsg.class(in = 10214) (out= 4286)(deflated 58%)

========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

3.Copy the generated dlls into the system 32 directory
Capture56

Note:
Copy the libzmq_d.dll into libzmq.dll

4.Add the zmq jar to your project
found in zmq-master\lib
Capture57 

5.Enjoy 0mq.

References:
http://stackoverflow.com/questions/11455588/how-to-run-zeromq-with-java

Camel asyncRequestBody

A simple sample of using Camel asyncRequestBody method .
The sample is based on the Camel: Camel :: Example :: Cafe 2.12.1 sample.
The main :

 public void runCafeTableFinder () throws Exception {      
        DefaultCamelContext camelContext = new DefaultCamelContext();
        camelContext.setRegistry(createRegistry());
        camelContext.addRoutes(this);
        camelContext.start();
        
        ProducerTemplate template = camelContext.createProducerTemplate();
       
        Guest guest = new Guest (4);       
        
        Future<CafeTable> future = template.asyncRequestBody("direct:cafeEnterence", guest , CafeTable.class);
        
        CafeTable response = future.get(1, TimeUnit.MINUTES);
        
        System.out.println ("The table :"  + response.toString());
        
        camelContext.stop();
     }
The sample use JNDI in order to cache beans.
The hostes that finds table code:
public class hostess {
    public CafeTable getTable (Guest pGuest)
    {
       System.out.println ("No of people  :" + Integer.toString(pGuest.getmNoOfP()));
        
        return new CafeTable ("Not in the smoking Zone");
    }
}

The registration in the JNDI :

  protected JndiRegistry createRegistry() throws Exception {
        JndiRegistry jndi = new JndiRegistry();     
        jndi.bind("hostess", new hostess());
        return jndi;
    }

The table and the guests classes are simple pojos:

public class Guest {
    
    private int mNoOfP;
    public int getmNoOfP() {
        return mNoOfP;
    }
    public void setmNoOfP(int mNoOfP) {
        this.mNoOfP = mNoOfP;
    }
    
    public Guest(int pNoOfP) {
        mNoOfP = pNoOfP;
    }
    
}

and  the CafeTable pojo

public class CafeTable {
    private String mLocation = "";
    public CafeTable (String pLocation)
    {
        mLocation = pLocation;
    }
    @Override
    public String toString() {
        return "CafeTable{" + "mLocation=" + mLocation + '}';
    }
}

The result:
No of people  :4
The table :CafeTable{mLocation=Not in the smoking Zone}

יום חמישי, 8 במאי 2014

Writing custom openstack wighter

Two options are available in order to guide the OpenStack cloud infrastructure to schedule  the next Virtual machine on a host with minimum benchmark execution time left :
1.Write a custom Nova scheduling filter.
2.Write a custom Nova scheduling  weigher   .
Due to the fact that the filter logic in this case is complicated a wighter class is more appropriated for this task.

The wighter classes location :
The scheduling implementation in Nova is located in \nova\scheduler directory
The weights folder contains build in Weighers for example the RAMWeigher that returns host score  according to its available ram .

Open stack Weigher classes relationships 

Capture4
A concrete Weigher implements the weights.BaseHostWeigher interface (its abc python interface ).
The BaseWeigher interface declares the following methods:
The def _weigh_object(self, host_state, weight_properties):
method returns the host un normalized score.
The def weigh_objects(self, weighed_obj_list, weight_properties):
method may used to calculate the weight of an host when information from all hosted is required  .(Not our case ).

We need to create a new concrete Weighter class that's overrides the _weigh_object method .

class BenchMarkResourcesWeigher(weights.BaseHostWeigher):
    def _weight_multiplier(self):
        """the default weight multiplier."""
        return 1
    def _weigh_object(self, host_state, weight_properties):
        """Higher weights win.  """
        return -1 * host_state.BenchMarks_Total_Work_Left


In the next Post we will add the new member : BenchMarks_Total_Work_left  to the host_state class 

יום שלישי, 6 במאי 2014

Spring inheritance tree

With Spring you can collect classes and there inherits tree into list .
For an example :
Use the autowire spring context declaration:
@Configuration
@ComponentScan("com.model.cars")
public class SpringContext {
}

We can declare a ctor in a @component Road class
 @Autowired
public Road(List<Car> pCar) {
 
}
The auto wired process scans the  com.model.cars directory for all the components that inherit from Car class (and the car class itself if exists in the scanning directory )  and construct the car Ctor parameter pCar list accordingly .
So if the com.model.cars path contains the following component
@Component
Class sosita extends Car
and
@Component
Class Subaru extends
sosita
The  Road ctor will be called with a 2 items list of sosita and Subaru

Reference:
http://lkrnac.net/blog/2014/04/30/load-inheritance-tree/

Simple 0MQ request replay client and server

The biggest problem of 0MQ in my opinion is it’s installation process.
In contradiction to Rabbit , MSMQ and ActiveMQ its very hard to install it and use it.
But there are a lot of samples in any language.
The project:
Capture50
The main

package test0mq2;
import java.lang.reflect.Field;
public class Test0Mq2 {
    public static void main(String[] args) throws Exception {
 
        SetLibraryPath();
 
        StartServer();
        
        SendMsgToServer();
       
    }
    private static void StartServer()
    {
        ZeroMQRepServer theZeroMQRepServer = new ZeroMQRepServer();
    
        theZeroMQRepServer.Startlisten();
    }
    
    private static void SendMsgToServer()
    {
        ZeroMQREQClient theZeroMQREQClient = new ZeroMQREQClient();
        
        theZeroMQREQClient.SendMsg ("Hello Zvika");
    }
    private static void SetLibraryPath() throws SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchFieldException {
        String libPathProperty = System.getProperty("java.library.path");
        
        System.out.println(libPathProperty);
        
        System.setProperty( "java.library.path", "H:\\deleteme100\\zeromq-4.0.4\\bin\\Win32" );
        
        Field fieldSysPath = ClassLoader.class.getDeclaredField( "sys_paths" );
        fieldSysPath.setAccessible( true );
        fieldSysPath.set( null, null );
    
        libPathProperty = System.getProperty("java.library.path");
        System.out.println(libPathProperty);
    }
}

Note:
The SetLibraryPath method is used to set the path of the jzmq
The jzmq.dll is a jni native extension used for the 0MQ
The Server


package test0mq2;
import org.zeromq.ZMQ;
public class ZeroMQRepServer extends Thread{
    
     public void Startlisten ()
     {
         (new ZeroMQRepServer()).start();
     }
     @Override
     public void run() {
          
        ZMQ.Context context = ZMQ.context(1);
        //  Socket to talk to clients
        ZMQ.Socket responder = context.socket(ZMQ.REP);
        responder.bind("tcp://*:5555");
        while (!Thread.currentThread().isInterrupted()) {
            // Wait for next request from the client
            byte[] request = responder.recv(0);
           
            // Send reply back to client
            String theReplay = "Get:" + new String(request);
            responder.send(theReplay.getBytes(), 0);
        }
        responder.close();
        context.term();
     }
}

 The Client


package test0mq2;
import org.zeromq.ZMQ;
public class ZeroMQREQClient {
    
    public void SendMsg (String pMsg)
    {
        ZMQ.Context context = ZMQ.context(1);
       //Connecting to server with REQ mode 
        ZMQ.Socket requester = context.socket(ZMQ.REQ);
        
        requester.connect("tcp://localhost:5555");
        requester.send(pMsg.getBytes(), 0);
        byte[] reply = requester.recv(0);
        
        System.out.println("Received:" + new String(reply));
        
        requester.close();
        
        context.term();
    }
}