Giter VIP home page Giter VIP logo

bstalkj's Introduction

##a Java beanstalkd client This is a work in progress. Client library for beanstalkd.

###Usage:

####Non pooled connection Example of a producer:

// Create a factory with given configuration
BeanstalkFactory factory = BeanstalkFactory.factory().host("localhost")
												     .port(11300)
												     .connectionTimeout(5000)
												     .tube("dummy_tube")
												     .build();
// fetch the producer
Producer producer = factory.get().producer();
// place job
producer.put(1l, 0, 5000, "this is some data".getBytes());
// print tube stats
System.out.println(producer.tubeStats());

To see more on the Producer

Example of a worker/consumer:

// We can use the default factory, poiting to localhost:11300
BeanstalkFactory factory = BeanstalkFactory.defaultFactory();

Worker worker = factory.get().worker();
BeanstalkJob job = worker.reserve(60).get();

System.out.println("Got job: " + job);

worker.delete(job);
System.out.println(worker.tubeStats());

To see more on the Consumer

####Pooled connection Example of a producer:

// Like in non-pooled client we need to configure first or connection
BeanstalkFactory factory = BeanstalkFactory.factory().host("localhost")
												     .port(11300)
												     .connectionTimeout(5000)
												     .tube("another_dummy_tube")
												     .build();

// Optionally we could configure non-default pool parameters
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(8);
poolConfig.setMaxIdle(8);

BeanstalkPool pool = new BeanstalkPool(poolConfig, factory);
// If we use default pool configuration we could do:
// BeanstalkPool pool = new BeanstalkPool(factory);
					
try (Producer producer = pool.getResource().producer()) {
	producer.put(1l, 0, 5000, "this is some data".getBytes());
	log.info(producer.tubeStats());
}						 												 

Using try-with-resource we ensure the client is returned to the pool.

And for the Worker:

try (Worker worker = pool.getResource().worker()) {
			BeanstalkJob job = worker.reserve(60).get();
			log.info("Got job: " + job);
			worker.delete(job);
}

Another fast way of creating a pool in a single line but the tube itself can be:

BeanstalkPool pool = BeanstalkFactory.factory()
                                     .host("xyz.blah.com")
                                     .port(1111)
                                     .tube("some-tube")
                                     .build()
                                     .pool();

###Credits

License is MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.