Announcement Announcement Module
Collapse
No announcement yet.
TaskExecutor for GridGain Page Title Module
Move Remove Collapse
This topic is closed
X
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • TaskExecutor for GridGain

    Hi all,

    I wrote a TaskExecutor for GridGain. GridGain is the easiest way to set up a grid computer for Java applications.

    With this implementation you can send Runnable objects over the network for execution. GridGain will take care of classloading issues and the like, works like a charm.

    To use the TaskExecutor follow these steps:

    1) Configure the class in Spring. The default configuration should be fine but you can customize your node through the gridConfiguration property.
    2) Set the GRIDGAIN_HOME environment variable.
    3) Use the TaskExecutor object as usual. The only constraint is that any objects contained by Runnable instances must be Serializable.

    Here's the implementation:

    Code:
    package gridgain;
    
    import org.gridgain.grid.Grid;
    import org.gridgain.grid.GridConfiguration;
    import org.gridgain.grid.GridFactory;
    import org.gridgain.grid.gridify.GridifyArgument;
    import org.gridgain.grid.gridify.GridifyArgumentAdapter;
    import org.gridgain.grid.kernal.GridifyDefaultTask;
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.core.task.TaskExecutor;
    
    public class GridGrainTaskExecutor implements TaskExecutor, InitializingBean, DisposableBean {
    
    	private Grid grid;
    
    	
    	public void setGrid(Grid grid) {
    		this.grid = grid;
    	}
    	
    	private GridConfiguration gridConfiguration;
    	
    	public void setGridConfiguration(GridConfiguration gridConfiguration) {
    		this.gridConfiguration = gridConfiguration;
    	}
    
    	
    	public void execute(Runnable runnable) {
    		if (runnable != null) {
    			GridifyArgument argument = 
    				new GridifyArgumentAdapter(
    						runnable.getClass(),
    						"run",
    						new Class[] {},
    						new Object[] {},
    						runnable);
    			
    			grid.execute(GridifyDefaultTask.class.getName(), argument, 0);
    		}
    	}
    
    
    	private boolean gridCreated = false;
    	
    	public void afterPropertiesSet() throws Exception {
    		if (grid == null) {
    			if (gridConfiguration != null) {
    				grid = GridFactory.start(gridConfiguration);
    			} else {
    				grid = GridFactory.start();
    			}
    			gridCreated = true;
    		}
    	}
    
    
    
    	public void destroy() throws Exception {
    		if (gridCreated) {
    			GridFactory.stop(true);
    		}
    	}
    
    }
    Enjoy.

    Tentacle

  • #2
    Nice to dig out this thread through searching. I think Tentacle has been working on GridGain and Spring Batch. Any new interesting findings, problems or codes to share?

    We will plan to study to use GridGain to integrate into Spring Batch. We are not thinking to do it at taskexecutor side but at job launcher side. e.g.

    Code Without Grid:
    JobExectuion = jobLaucher.run(Job, JobParameters)

    Code with Grid:
    @Gridify
    jobLaucher.run(Job, JobParameters)

    this gridify code actually break down the job parameters into multiple pieces and execute a standalone Job for each piece. After that, it consolidate the results and construct back a JobExectuion object. With this way, we can achieve code transparent to developers.

    Any comments on the approach?

    Comment


    • #3
      I would like to poll for any updates on the thread. Thanks in advance.

      Comment


      • #4
        Looks pretty cool, but i would change the code to the following now that GridGain 2.0.3 implements an ExecutorService. Just makes the execute method a call thru.

        package gridgain;

        import org.gridgain.grid.Grid;
        import org.gridgain.grid.GridConfiguration;
        import org.gridgain.grid.GridFactory;
        import org.gridgain.grid.gridify.GridifyArgument;
        import org.gridgain.grid.gridify.GridifyArgumentAdapter;
        import org.gridgain.grid.kernal.GridifyDefaultTask;
        import org.springframework.beans.factory.DisposableBean;
        import org.springframework.beans.factory.InitializingBean ;
        import org.springframework.core.task.TaskExecutor;

        public class GridGrainTaskExecutor implements TaskExecutor, InitializingBean, DisposableBean {

        private Grid grid;

        private ExecutorService gridExecutor;


        public void setGrid(Grid grid) {
        this.grid = grid;
        }

        private GridConfiguration gridConfiguration;

        public void setGridConfiguration(GridConfiguration gridConfiguration) {
        this.gridConfiguration = gridConfiguration;
        }


        public void execute(Runnable runnable) {
        if (runnable != null) {
        gridExecutor.execute(runnable);
        }
        }


        private boolean gridCreated = false;

        public void afterPropertiesSet() throws Exception {
        if (grid == null) {
        if (gridConfiguration != null) {
        grid = GridFactory.start(gridConfiguration);
        } else {
        grid = GridFactory.start();
        }
        gridExecutor = grid.newGridExecutorService();
        gridCreated = true;
        }
        }



        public void destroy() throws Exception {
        if (gridCreated) {
        gridExecutor.shutdown();
        GridFactory.stop(true);
        }
        }

        }

        Comment

        Working...
        X