Thursday, September 17, 2015

ReactiveX in Java

Dependency:

<dependency>
    <groupId>io.reactivex</groupId>
    <artifactId>rxjava</artifactId>
    <version>1.0.14</version>
</dependency>

Code:

Observable:

ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 5, 3000L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue());

Observable<ChannelItemType> myObservable = Observable.create(
        new Observable.OnSubscribe<ChannelItemType>() {
            @Override
            public void call(Subscriber<? super ChannelItemType> sub) {
                sub.onNext(item);
                sub.onCompleted();
            }
        }
).subscribeOn(Schedulers.from(executor));
myObservable.subscribe(new ChannelConsumer(channelRepo));

Above snippet, we create an Observable, sent item on onNext() function to Consumer (in this case ChannelConsumer). One interesting thing here is we thread consuming by using executor. We create 3 thread in executor. Obervable automatically pick thread in executor and send data.

Consumer:
public class ChannelConsumer extends Subscriber<ChannelItemType> {
    @Override
    public void onCompleted() {
        //Clean up your process when complete
    }
    @Override
    public void onError(Throwable throwable) {

    }
    @Override
    public void onNext(ChannelItemType channelItem) {
        //put your code here
    }
}

Thursday, August 20, 2015

Monday, August 17, 2015

Make Virtualbox display full Linux client


References:
https://help.ubuntu.com/community/VirtualBox/GuestAdditions
http://www.binarytides.com/vbox-guest-additions-ubuntu-14-04/

Also:  Try going to Software and Updates -> Additional Drivers and choose: Use x86 visualization solution

After all remember choosing: View -> Auto-resize Guest Display

Batch insert with JDBC and Spring

JDBC:

String sql = "insert into employee (name, city, phone) values (?, ?, ?)";
Connection connection = new getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
final int batchSize = 1000;
int count = 0;
for (Employee employee: employees) {
     ps.setString(1, employee.getName());
     ps.setString(2, employee.getCity());
     ps.setString(3, employee.getPhone());
     ps.addBatch();

     if(++count % batchSize == 0) {
          ps.executeBatch();
     }
}
ps.executeBatch(); // insert remaining records

ps.close();
connection.close();

Spring Data:

public class EmployeeRepository extends JdbcDaoSupport { 

    public void insertEmployeeBatch(List<Employee> employees) {

        try {
            String sql = "
insert into employee (name, city, phone) values (?, ?, ?)";

            getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                     Employee employee = employees.get(i);                    

                     ps.setString(1, employee.getName());
                     ps.setString(2, employee.getCity());
                     ps.setString(3, employee.getPhone());
                }

                public int getBatchSize() {
                    return
employees.size();
                }
            });
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
        }
    } 

}