I want to know, why get a exception of redis.clients.jedis.exceptions.JedisConnectionException? - exception

I used jedis in my java project with one master and slave, once the slave started, it come to this in redis_slave.log:
44764 [2721] 24 Dec 14:07:41.157 * Connecting to MASTER...
44765 [2721] 24 Dec 14:07:41.158 * MASTER <-> SLAVE sync started
44766 [2721] 24 Dec 14:07:41.158 # Error condition on socket for SYNC: Connection refused
and in my java source file, I want to delete all data in redis, so I wrote this code:
public class TestJedisPool {
private Jedis jedis = null;
private JedisPool jedisPool = null;
public TestJedisPool() {
initialPool();
jedis = jedisPool.getResource();
jedis.auth("123456");
}
private void initialPool() {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(20);
config.setMaxIdle(5);
config.setMaxWait(1000L);
config.setTestOnBorrow(false);
jedisPool = new JedisPool(config, "192.168.144.3", 6397);
}
private void masterThread() {
System.out.println(jedis.flushAll());
jedisPool.returnResource(jedis);
jedis.disconnect();
}
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TestJedisPool test = new TestJedisPool();
test.masterThread();
}
}
and get a exception like this:
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:22)
at com.oppo.testpool.TestJedisPool.<init>(TestJedisPool.java:15)
at com.oppo.testpool.TestJedisPool.main(TestJedisPool.java:41)
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
any one can help me ?

I modified your code and it works for:
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
public class TestJedisPool {
static ExecutorService executor = Executors.newSingleThreadExecutor();
final static ShardedJedisPool redisStatsPool;
static {
String host = "127.0.0.1";
int port = 6379;
List<JedisShardInfo> redisClickShard = new ArrayList<JedisShardInfo>();
redisClickShard.add(new JedisShardInfo(host, port));
JedisPoolConfig config = new JedisPoolConfig();
config.maxActive = 1000;
config.maxIdle = 10;
config.minIdle = 1;
config.maxWait = 30000;
config.numTestsPerEvictionRun = 3;
config.testOnBorrow = true;
config.testOnReturn = true;
config.testWhileIdle = true;
config.timeBetweenEvictionRunsMillis = 30000;
redisStatsPool = new ShardedJedisPool( config, redisClickShard);
}
public TestJedisPool() {
}
String[] getRandomNumber(int min, int max){
String[] test = new String[8];
for (int i = 0; i < test.length; i++) {
int partition = min + (int)(Math.random() * ((max - min) + 1));
test[i] = "key"+partition;
}
return test;
}
static volatile long sum = 0;
public Runnable hincrBy(final String keyname, final String[] keyfields , final long val){
Runnable job = new Runnable() {
#Override
public void run() {
c++;
System.out.println(c);
try {
ShardedJedis shardedJedis = redisStatsPool.getResource();
final Jedis jedis = shardedJedis.getShard("") ;
Pipeline p = jedis.pipelined();
for (String keyfield : keyfields) {
p.hincrBy(keyname, keyfield, val);
sum += val;
}
p.sync();
redisStatsPool.returnResource(shardedJedis);
} catch (Exception e) {
//e.printStackTrace();
}
}
};
return job;
}
static volatile int c = 0;
static final int MAX = (int) Math.pow(10, 6);
void masterThread() {
for (int i = 0; i < MAX; i++) {
String[] keynames = getRandomNumber(100, 1000);
executor.submit(hincrBy("test10^6", keynames, 1L));
}
executor.shutdown();
while(!executor.isTerminated()){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int sumTest() {
int total = 0;
try {
ShardedJedis shardedJedis = redisStatsPool.getResource();
final Jedis jedis = shardedJedis.getShard("") ;
Map<String,String> map = jedis.hgetAll("test10^6");
Set<String> keys = map.keySet();
for (String keyfield : keys) {
int v = Integer.parseInt(map.get(keyfield));
total += v;
}
redisStatsPool.returnResource(shardedJedis);
} catch (Exception e) {
//e.printStackTrace();
}
return total;
}
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TestJedisPool test = new TestJedisPool();
test.masterThread();
System.out.println(sum);
System.out.println(test.sumTest());
System.out.println(test.sumTest() == sum);
}
}

Related

I am implementing raw h264 player using MediaCodec

I am implementing raw h264 player.
The following codes work slowly. (There is mutex problems, But ignore it.)
Also bpkt.bytes is sent each frame unit named end frame from other server via tcp.
But mH264Queue is slower for consuming data than filling data to mH264Queue.
The size of mH264Queue is increased forever.
Please see my codes.
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import android.app.Activity;
import android.media.MediaCodec;
import android.media.MediaCodec.BufferInfo;
import android.media.MediaFormat;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
public class RawH264Activity extends Activity implements SurfaceHolder.Callback
{
// private static final String filePath = Environment.getExternalStorageDirectory()+ "/ksoo.h264"; // + "/video_encoded.263";//"/video_encoded.264";
private PlayerThread mPlayer = null;
Handler handler = null;
// public static byte[] SPS = null;
// public static byte[] PPS = null;
Socket mMediaSocket = null;
BufferedInputStream mMediaSocketIn = null;
OutputStream mMediaSocketOut = null;
Thread mSocketTh = null;
Queue<SocketTool.BinaryPkt> mH264Queue = new LinkedList<SocketTool.BinaryPkt>();
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SurfaceView sv = new SurfaceView(this);
handler = new Handler();
sv.getHolder().addCallback(this);
setContentView(sv);
}
#Override
public void surfaceCreated(SurfaceHolder holder)
{
Log.d("DecodeActivity", "in surfaceCreated");
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
Log.d("DecodeActivity", "in surfaceChanged");
if (mPlayer == null)
{
Toast.makeText(getApplicationContext(), "in surfaceChanged. creating playerthread", Toast.LENGTH_SHORT).show();
mSocketTh = new Thread(new Runnable() {
#Override
public void run() {
try {
mMediaSocket = new Socket(CurrentState.get().mRemoteAddress, CurrentState.get().mRemoteMediaPort);
mMediaSocketIn = new BufferedInputStream(mMediaSocket.getInputStream());
mMediaSocketOut = mMediaSocket.getOutputStream();
mPlayer = new PlayerThread(holder.getSurface());
mPlayer.start();
while(!mSocketTh.isInterrupted()) {
SocketTool.BinaryPkt bpkt = SocketTool.readPKTBinary(mMediaSocketIn);
mH264Queue.add(bpkt);
if(mH264Queue.size() > 2000)
break;
}
try {
mMediaSocketIn.close();
mMediaSocketOut.close();
mMediaSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
// Log.i("bpkt", "" + bpkt.mCmdType);
// }
} catch (IOException e) {
// 서버 접속 끊기
e.printStackTrace();
}
}
});
mSocketTh.start();
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder)
{
if (mPlayer != null)
{
mPlayer.interrupt();
}
}
private class PlayerThread extends Thread
{
//private MediaExtractor extractor;
private MediaCodec decoder;
private Surface surface;
public PlayerThread(Surface surface)
{
this.surface = surface;
}
#Override
public void run()
{
handler.post(new Runnable()
{
#Override
public void run()
{
try {
decoder = MediaCodec.createDecoderByType("video/avc");
} catch (IOException e) {
e.printStackTrace();
}
MediaFormat mediaFormat = MediaFormat.createVideoFormat("video/avc", 1280, 720);
// Video_format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, interval);
// mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, bitRate);
// mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 60);
// mediaFormat.setByteBuffer("csd-0", ByteBuffer.wrap(new byte[] {
// 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1F, (byte)0x9D, (byte)0xA8, 0x14, 0x01, 0x6E, (byte)0x9B, (byte)0x80,
// (byte)0x80, (byte)0x80, (byte)0x81}));
// mediaFormat.setByteBuffer("csd-1", ByteBuffer.wrap(new byte[] {
// 0x00, 0x00, 0x00, 0x01, 0x68, (byte)0xCE, 0x3C, (byte)0x80 }));
// byte[] header_sps = { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, (byte)0x80, 0x0C, (byte)0xE4, 0x40, (byte)0xA0, (byte)0xFD, 0x00, (byte)0xDA, 0x14, 0x26, (byte)0xA0 };
// byte[] header_pps = {0x00, 0x00, 0x00, 0x01, 0x68, (byte)0xCE, 0x38, (byte)0x80 };
// mediaFormat.setByteBuffer("csd-0", ByteBuffer.wrap(header_sps));
// mediaFormat.setByteBuffer("csd-1", ByteBuffer.wrap(header_pps));
decoder.configure(mediaFormat, surface /* surface */, null /* crypto */, 0 /* flags */);
if (decoder == null)
{
Log.e("DecodeActivity", "Can't find video info!");
return;
}
decoder.start();
Log.d("DecodeActivity", "decoder.start() called");
ByteBuffer[] inputBuffers = decoder.getInputBuffers();
ByteBuffer[] outputBuffers = decoder.getOutputBuffers();
long startMs = System.currentTimeMillis();
int i = 0;
while(!Thread.interrupted())
{
int inIndex = 0;
while ((inIndex = decoder.dequeueInputBuffer(1)) < 0)
;
if (inIndex >= 0)
{
ByteBuffer buffer = inputBuffers[inIndex];
buffer.clear();
Log.i("queue", String.valueOf(mH264Queue.size()));
SocketTool.BinaryPkt bpkt = mH264Queue.poll();
int sampleSize = 0;
if (bpkt.bytes == null) {
Log.d("DecodeActivity", "InputBuffer BUFFER_FLAG_END_OF_STREAM");
decoder.queueInputBuffer(inIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
break;
}
else
{
sampleSize = bpkt.bytes.length;
buffer.clear();
buffer.put(bpkt.bytes);
decoder.queueInputBuffer(inIndex, 0, sampleSize, 0, 0);
}
BufferInfo info = new BufferInfo();
int outIndex = decoder.dequeueOutputBuffer(info, 10000);
switch (outIndex)
{
case MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED:
Log.d("DecodeActivity", "INFO_OUTPUT_BUFFERS_CHANGED");
outputBuffers = decoder.getOutputBuffers();
break;
case MediaCodec.INFO_OUTPUT_FORMAT_CHANGED:
// Log.d("DecodeActivity", "New format " + decoder.getOutputFormat());
break;
case MediaCodec.INFO_TRY_AGAIN_LATER:
// Log.d("DecodeActivity", "dequeueOutputBuffer timed out!");
try {
sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
default:
ByteBuffer outbuffer = outputBuffers[outIndex];
// Log.d("DecodeActivity", "We can't use this buffer but render it due to the API limit, " + outbuffer);
decoder.releaseOutputBuffer(outIndex, true);
break;
}
i++;
}
}
decoder.stop();
decoder.release();
}
});
}
}
}
The problem here is you only drain the output buffer when input buffer is available.
The fact is the output of mediacodec is often (if not always) not available instantly after queueing the input buffer.
So instead you should do something like this
while(!endReached) {
// Try drain decoder output first
BufferInfo info = new BufferInfo();
int outIndex = decoder.dequeueOutputBuffer(info, 10000);
switch (outIndex) {
...
}
// Feed decoder input
if (inputAvailable) {
int inIndex = decoder.dequeueInputBuffer(10000);
...
}
}

Received Data Provider Mismatch error

Tried below code but receiving Data Provider Mismatch error. Could anyone help out on this?
package appModules;
import org.testng.annotations.Test;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeTest;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
public class NewTest {
public WebDriver driver;
public WebDriverWait wait;
String appURL =
"https://dev.agencyport.rsagroup.ca:8443/agencyportal/ProcessLogoff";
//Locators
private By username = By.id("USERID");
private By password = By.id("PASSWORD");
#BeforeClass
public void testSetup() {
System.setProperty("webdriver.firefox.marionette",
"C:\\Automation\\geckodriver.exe");
driver=new FirefoxDriver();
driver.manage().window().maximize();
wait = new WebDriverWait(driver, 5);
}
#Test(dataProvider = "login")
public void Login(String Username, String Password) {
driver.findElement(username).sendKeys(Username);
driver.findElement(password).sendKeys(Password);
}
#DataProvider (name="login")
public Object[][] dp() throws Exception{
Object[][] arrayObject =
getExcelData("C:\\Automation\\testData.xls","New");
return arrayObject;
}
public String[][] getExcelData(String fileName, String sheetName) throws
Exception {
String[][] arrayExcelData = null;
try {
FileInputStream fs = new FileInputStream(fileName);
Workbook wb = Workbook.getWorkbook(fs);
Sheet sh = wb.getSheet(sheetName);
int totalNoOfCols = sh.getColumns();
System.out.println(totalNoOfCols);
int totalNoOfRows = sh.getRows();
System.out.println(totalNoOfRows);
arrayExcelData = new String[totalNoOfRows-1][totalNoOfCols];
for (int i=1 ; i <totalNoOfRows; i++) {
for (int j=0; j <totalNoOfCols; j++) {
arrayExcelData[i-1][j] = sh.getCell(j, i).getContents();
System.out.println(arrayExcelData[i-1][j]);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
return arrayExcelData;
}
#Test
public void tearDown() {
driver.quit();
}
}
Received below error -
org.testng.internal.reflect.MethodMatcherException:
Data provider mismatch
Method: Login([Parameter{index=0, type=java.lang.String,
declaredAnnotations=[]}, Parameter{index=1, type=java.lang.String,
declaredAnnotations=[]}])
Arguments: [(java.lang.String) agent,(java.lang.String) password,
(java.lang.String) ]
atorg.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:45)
at org.testng.internal.Parameters.injectParameters(Parameters.java:796)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:982)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)`
You are passing return value of arrayObject with incorrect Data Type,
You need to get String value from Excel File in Data Provider Method, and That you need to pass in Object.
Refer below Example, for Reference:
#DataProvider
public Iterator<Object[]> getTestData()
{
ArrayList<Object[]> testdata = new ArrayList<Object[]>();
try {
reader = new excelUtility(excelTestDataFile);
} catch (Exception e) {
e.printStackTrace();
}
sheetName = className;
for (int rowNumber = 2; rowNumber <= reader.getRowCount(sheetName); rowNumber++) {
String caseNo = reader.getCellData(sheetName, "Case", rowNumber);
String emailid = reader.getCellData(sheetName, "Email ID", rowNumber);
String password = reader.getCellData(sheetName, "Password", rowNumber);
String message = reader.getCellData(sheetName, "Expected Result", rowNumber);
Object ob[] =
{ caseNo, emailid, password, message };
testdata.add(ob);
}
return testdata.iterator();
}
And this is the #Test Receiver of Data Provider:
#Test(dataProvider = "getTestData")
public void calllogin(String caseNO, String emailid, String password, String expectedResult) throws Exception
{
******
}

Database connection error in Selenium

I've written a class which fetcheS data from the databasE:
#Test
void ts() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/selenium","root","root");
Statement stm=con.createStatement();
ResultSet rs=stm.executeQuery("select * from seleniumusers");
while(rs.next()){
name=rs.getString("firstname");
System.out.println(name);
e(mysql)
But on running the test from testng once the browser gets open it gives and error:
Warning: mysql_pconnect() [function.mysql-pconnect]: Can't connect to MySQL server on 'localhost' (10061) in C:\Program Files\vtigercrm-5.2.1\apache\htdocs\vtigerCRM\adodb\drivers\adodb-mysql.inc.php on line 373
public WebDriverWait wait;
String filename;
File file;
Workbook RegExcel = null;
Row row = null;
Sheet RegExcelSheet = null;
FileInputStream inputStream;
public String[][] excelDataValue;
public int rowCount;
public List<WebElement> AllRegularization=null;;
String projectPath = System.getProperty("user.dir");
#Given("^User Save Excel At Said Location$")
public void user_save_excel_at_said_location() throws Throwable {
filename="RegDates.xlsx";
file = new File (projectPath +"\\"+ filename);
}
#When("^Excel File Is Readable$")
public void excel_file_is_readable() throws Throwable {
inputStream = new FileInputStream(file);
String fileExtensionName = filename.substring(filename.indexOf("."));
}
#Then("^Read The Excel$")
public void read_the_excel() throws Throwable {
RegExcel = new XSSFWorkbook(inputStream);
RegExcelSheet = RegExcel.getSheet("Sheet1");
}
#And("^Create Array For Dates Data In Excel$")
public void create_array_for_dates_data_in_excel() throws Throwable {
rowCount = RegExcelSheet.getLastRowNum()-RegExcelSheet.getFirstRowNum();
excelDataValue = new String[rowCount+1][5];
for (int i = 0; i < rowCount+1; i++)
{
row = RegExcelSheet.getRow(i);
for (int j = 0; j < row.getLastCellNum(); j++)
{
excelDataValue[i][j]=row.getCell(j).toString();
//System.out.println(row.getCell(j).toString());
}
}
}

Read Time Out Exception in Cassandra using cassandra-driver-core

I am writing a Java application which reads the data from MySQL and stores it in Cassandra as Sqoop does not support a direct import to Cassandra. I am using Producer-Consumer framework to achieve the same due to high number of records (in millions) in MySQL. But I am getting ReadTimeOut Exception (com.datastax.driver.core.exceptions.DriverException: Timeout during read). I have one Producer class which reads the data from MySQL and puts it into one queue. There is one consumer class which reads the data from that queue and pushes it to Cassndra. There is one manager class which acts as a coordination bridge between these two classes.
Producer class :-
public class MySQLPrintJobProducer implements Runnable {
private BlockingQueue<PrintJobDAO> printerJobQueue = null;
private Connection conn = null;
public MySQLPrintJobProducer(BlockingQueue<PrintJobDAO> printerJobQueue) throws MySQLClientException {
this.printerJobQueue = printerJobQueue;
connect();
}
private void connect() throws MySQLClientException {
try {
Class.forName(MySQLClientConstants.MYSQL_JDBC_DRIVER);
conn = DriverManager.getConnection("jdbc:mysql://mysqlserverhose/mysqldb?user=mysqluser&password=mysqlpasswd");
} catch (ClassNotFoundException e) {
throw new MySQLClientException(ExceptionUtils.getStackTrace(e));
} catch (SQLException e) {
throw new MySQLClientException(ExceptionUtils.getStackTrace(e));
}
}
public void run() {
ResultSet rs = null;
Statement stmt = null;
PreparedStatement pStmt = null;
try {
stmt = conn.createStatement();
// Get total number of print jobs stored.
rs = stmt.executeQuery(MySQLClientConstants.PRINT_JOB_COUNT_QUERY);
int totalPrintJobs = 0;
if(rs != null) {
while(rs.next()) {
totalPrintJobs = rs.getInt(1);
}
}
// Determine the number of iterations.
int rowOffset = 1;
int totalIteration = ((totalPrintJobs / ExportManagerConstants.DATA_TRANSFER_BATCH_SIZE) + 1);
pStmt = conn.prepareStatement(MySQLClientConstants.PRINT_JOB_FETCH_QUERY);
int totalRecordsFetched = 0;
// Iterate over to fetch Print Job Records in bathces and put it into the queue.
for(int i = 1; i <= totalIteration; i++) {
pStmt.setInt(1, rowOffset);
pStmt.setInt(2, ExportManagerConstants.DATA_TRANSFER_BATCH_SIZE);
System.out.println("In iteration : " + i + ", Row Offset : " + rowOffset);
rs = pStmt.executeQuery();
synchronized (this.printerJobQueue) {
if(this.printerJobQueue.remainingCapacity() > 0) {
while(rs.next()) {
totalRecordsFetched = rs.getRow();
printerJobQueue.offer(new PrintJobDAO(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getDate(4),
rs.getTimestamp(5), rs.getInt(6), rs.getInt(7), rs.getInt(8), rs.getInt(9),
rs.getInt(10), rs.getFloat(11), rs.getFloat(12), rs.getInt(13), rs.getFloat(14), rs.getInt(15),
rs.getDouble(16), rs.getDouble(17), rs.getDouble(18), rs.getDouble(19), rs.getDouble(20),
rs.getFloat(21)));
this.printerJobQueue.notifyAll();
}
System.out.println("In iteration : " + i + ", Records Fetched : " + totalRecordsFetched +
", Queue Size : " + printerJobQueue.size());
rowOffset += ExportManagerConstants.DATA_TRANSFER_BATCH_SIZE;
} else {
System.out.println("Print Job Queue is full, waiting for Consumer thread to clear.");
this.printerJobQueue.wait();
}
}
}
} catch (SQLException e) {
System.err.println(ExceptionUtils.getStackTrace(e));
} catch (InterruptedException e) {
System.err.println(ExceptionUtils.getStackTrace(e));
} finally {
try {
if(null != rs) {
rs.close();
}
if(null != stmt) {
stmt.close();
}
if(null != pStmt) {
pStmt.close();
}
} catch (SQLException e) {
System.err.println(ExceptionUtils.getStackTrace(e));
}
}
ExportManager.setProducerCompleted(true);
}
}
Consumer Class :-
public class CassandraPrintJobConsumer implements Runnable {
private Cluster cluster = null;
private Session session = null;
private BlockingQueue<PrintJobDAO> printerJobQueue = null;
public CassandraPrintJobConsumer(BlockingQueue<PrintJobDAO> printerJobQueue) throws CassandraClientException {
this.printerJobQueue = printerJobQueue;
cluster = Cluster.builder().withPort(9042).addContactPoint("http://cassandrahost").build();
}
public void run() {
int printJobConsumed = 0;
int batchInsertCount = 1;
if(cluster.isClosed()) {
connect();
}
session = cluster.connect();
PreparedStatement ps = session.prepare(CassandraClientConstants.INSERT_PRINT_JOB_DATA);
BatchStatement batch = new BatchStatement();
synchronized (this.printerJobQueue) {
while(true) {
if(!this.printerJobQueue.isEmpty()) {
for(int i = 1; i <= ExportManagerConstants.DATA_TRANSFER_BATCH_SIZE; i++) {
PrintJobDAO printJob = printerJobQueue.poll();
batch.add(ps.bind(printJob.getJobID(), printJob.getUserID(), printJob.getType(), printJob.getGpDate(), printJob.getDateTimes(),
printJob.getAppName(), printJob.getPrintedPages(), printJob.getSavedPages(), printJob.getPrinterID(), printJob.getWorkstationID(),
printJob.getPrintedCost(), printJob.getSavedCost(), printJob.getSourcePrinterID(), printJob.getSourcePrinterPrintedCost(),
printJob.getJcID(), printJob.getCoverageC(), printJob.getCoverageM(), printJob.getCoverageY(), printJob.getCoverageK(),
printJob.getCoverageTotal(), printJob.getPagesAnalyzed()));
printJobConsumed++;
}
session.execute(batch);
System.out.println("After Batch - " + batchInsertCount + ", record insert count : " + printJobConsumed);
batchInsertCount++;
this.printerJobQueue.notifyAll();
} else {
System.out.println("Print Job Queue is empty, nothing to export.");
try {
this.printerJobQueue.wait();
} catch (InterruptedException e) {
System.err.println(ExceptionUtils.getStackTrace(e));
}
}
if(ExportManager.isProducerCompleted() && this.printerJobQueue.isEmpty()) {
break;
}
}
}
}
}
Manager Class :-
public class ExportManager {
private static boolean isInitalized = false;
private static boolean producerCompleted = false;
private static MySQLPrintJobProducer printJobProducer = null;
private static CassandraPrintJobConsumer printJobConsumer = null;
private static BlockingQueue<PrintJobDAO> printJobQueue = null;
public static boolean isProducerCompleted() {
return producerCompleted;
}
public static void setProducerCompleted(boolean producerCompleted) {
ExportManager.producerCompleted = producerCompleted;
}
private static void init() throws MySQLClientException, CassandraClientException {
if(!isInitalized) {
printJobQueue = new LinkedBlockingQueue<PrintJobDAO>(ExportManagerConstants.DATA_TRANSFER_BATCH_SIZE * 2);
printJobProducer = new MySQLPrintJobProducer(printJobQueue);
printJobConsumer = new CassandraPrintJobConsumer(printJobQueue);
isInitalized = true;
}
}
public static void exportPrintJobs() throws ExportException {
try {
init();
} catch (MySQLClientException e) {
throw new ExportException("Print Job Export failed.", e);
} catch (CassandraClientException e) {
throw new ExportException("Print Job Export failed.", e);
}
Thread producerThread = new Thread(printJobProducer);
Thread consumerThread = new Thread(printJobConsumer);
consumerThread.start();
producerThread.start();
}
}
TestNG class :-
public class TestExportManager {
#Test
public void testExportPrintJobs() {
try {
ExportManager.exportPrintJobs();
Thread.currentThread().join();
} catch (ExportException e) {
Assert.fail("ExportManager.exportPrintJobs() failed.", e);
} catch (InterruptedException e) {
Assert.fail("ExportManager.exportPrintJobs() failed.", e);
}
}
}
I have also made some configuration changes by following this link. Still I am getting following exception after inserting 18000 - 20000 records.
Exception in thread "Thread-2" com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: /192.168.10.80
(com.datastax.driver.core.exceptions.DriverException: Timeout during read))
at com.datastax.driver.core.exceptions.NoHostAvailableException.copy(NoHostAvailableException.java:64)
at com.datastax.driver.core.DefaultResultSetFuture.extractCauseFromExecutionException(DefaultResultSetFuture.java:256)
at com.datastax.driver.core.DefaultResultSetFuture.getUninterruptibly(DefaultResultSetFuture.java:172)
at com.datastax.driver.core.SessionManager.execute(SessionManager.java:91)
at com.incendiary.ga.client.cassandra.CassandraPrintJobConsumer.run(CassandraPrintJobConsumer.java:108)
at java.lang.Thread.run(Unknown Source)
Caused by: com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: /192.168.10.80 (com.datastax.drive
r.core.exceptions.DriverException: Timeout during read))
at com.datastax.driver.core.RequestHandler.sendRequest(RequestHandler.java:100)
at com.datastax.driver.core.RequestHandler$1.run(RequestHandler.java:171)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
... 1 more
I am not able to figure out the actual reason for the issue. I could not find any exception in Cassandra system log. I am using Apache Cassandra 2.0.7 and cassandra-driver-core 2.0.1.
You can increase read time out in you driver side . By using withSocket method in this you have SocketOption class using that you can read time out .By default is read time out is 10 millisecond .

Json rpc claculator

My main activity: I have created threads and handler and tried calling it to establish a connection to a particular server:
public void mResult (){
Double secondNum = Double.parseDouble(screenView.getText().toString());
Double result = 0.0;
this.result=result;
// thread call
android.os.Handler aHandler = new android.os.Handler();
DoCalc myCalc = new DoCalc(aHandler, firstNum, secondNum, Operation);
myCalc.start();
screenView.setText(String.valueOf(result));
}
my main thread
class DoCalc extends Thread {
private android.os.Handler handler;
private double left,right;
private String oper;
public DoCalc(android.os.Handler myHandle, double left, double right, String oper){
this.handler = myHandle;
this.left = left;
this.right = right;
this.oper = oper;
}
public DoCalc(Handler aHandler, Double firstNum, Double secondNum, String operation){
this.handler = aHandler;
this.left = firstNum;
this.right = secondNum;
this.oper = operation;
}
public void run(){
Double result = 0.0;
String url = "http://129.219.151.99:8080/JsonRPC/calc";
try{
HttpJsonRpcClientTransport transport =
new HttpJsonRpcClientTransport(new URL(url));
transport.call(url);
LoggingPermission JsonRpcInvoker;
org.json.rpc.client.JsonRpcInvoker invoker = new JsonRpcInvoker();
Calculator calc = invoker.get(transport, "calc", Calculator.class);
if(oper.equals("+")){
result = calc.add(left,right);
}
if(oper.equals("-")){
result = calc.subtract(left, right);
}
if(oper.equals("*")){
result = calc.multiply(left, right);
}
if(oper.equals("/")){
result = calc.divide(left, right);
}
//FinalResult set = new FinalResult(result);
handler.post(new FinalResult(result));
// set.start();
}catch (Exception ex){
android.util.Log.w("calc","exception in call: "+ex.getMessage());
}
}
class FinalResult extends Thread implements Runnable {
double result;
EditText screenView;
public FinalResult(double result){
this.result = result;
}
public void run(){
android.util.Log.w("calc","result is: "+result);
System.out.println("result is"+result);
screenView.setText(String.valueOf(result));
}
}
}
}
I think my json rpc is not working am just getting 0.0 when ever I do any calculation