GAE/J, PayPal request - NoClassDefFoundError for AuthorizationRequiredException - google-app-engine

I am new to Google App Engine (Java) and PayPal process. I am using tutorial provided http://googleappengine.blogspot.com/2010/06/paypal-introduces-paypal-x-platform.html and NOT sure why I am getting following exception:
Uncaught exception from servlet
java.lang.NoClassDefFoundError: com/paypal/adaptive/exceptions/AuthorizationRequiredException
Here is my Servlet Class file which suppose to do preapproval and provide preapproval key and authorization url ..
import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.paypal.adaptive.api.requests.fnapi.ParallelPay;
import com.paypal.adaptive.api.responses.PayResponse;
import com.paypal.adaptive.core.APICredential;
import com.paypal.adaptive.core.AckCode;
import com.paypal.adaptive.core.CurrencyCodes;
import com.paypal.adaptive.core.PaymentType;
import com.paypal.adaptive.core.Receiver;
import com.paypal.adaptive.core.ServiceEnvironment;
import com.paypal.adaptive.exceptions.AuthorizationRequiredException;
import com.paypal.adaptive.exceptions.InvalidAPICredentialsException;
import com.paypal.adaptive.exceptions.InvalidResponseDataException;
import com.paypal.adaptive.exceptions.MissingAPICredentialsException;
import com.paypal.adaptive.exceptions.MissingParameterException;
import com.paypal.adaptive.exceptions.NotEnoughReceivers;
import com.paypal.adaptive.exceptions.PayPalErrorException;
import com.paypal.adaptive.exceptions.PaymentExecException;
import com.paypal.adaptive.exceptions.PaymentInCompleteException;
import com.paypal.adaptive.exceptions.ReceiversCountMismatchException;
import com.paypal.adaptive.exceptions.RequestAlreadyMadeException;
import com.paypal.adaptive.exceptions.RequestFailureException;
#SuppressWarnings("serial")
public class CWEMartServlet extends HttpServlet {
private static final Logger log = Logger.getLogger(CWEMartServlet.class.getName());
private static APICredential credentialObj;
#Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config);
// Get the value of APIUsername
String APIUsername = getServletConfig().getInitParameter("PPAPIUsername");
String APIPassword = getServletConfig().getInitParameter("PPAPIPassword");
String APISignature = getServletConfig().getInitParameter("PPAPISignature");
String AppID = getServletConfig().getInitParameter("PPAppID");
String AccountEmail = getServletConfig().getInitParameter("PPAccountEmail");
if(APIUsername == null || APIUsername.length() <= 0
|| APIPassword == null || APIPassword.length() <=0
|| APISignature == null || APISignature.length() <= 0
|| AppID == null || AppID.length() <=0 ) {
// requires API Credentials not set - throw exception
throw new ServletException("APICredential(s) missing");
}
credentialObj = new APICredential();
credentialObj.setAPIUsername(APIUsername);
credentialObj.setAPIPassword(APIPassword);
credentialObj.setSignature(APISignature);
credentialObj.setAppId(AppID);
credentialObj.setAccountEmail(AccountEmail);
log.info("Servlet initialized successfully");
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
try {
String id = req.getParameter("id");
String title = req.getParameter("title");
String order = req.getParameter("order");
String returnParam = req.getParameter("return");
String cancel = req.getParameter("cancel");
if(cancel != null && cancel.equals("1")) {
// user canceled the payment
getServletConfig().getServletContext().getRequestDispatcher("/paymentcancel.jsp").forward(req, resp);
} else if(returnParam != null && returnParam.equals("1")){
getServletConfig().getServletContext().getRequestDispatcher("/paymentsuccess.jsp").forward(req, resp);
} else if(order != null && order.length() > 0){
// process order
try {
StringBuilder url = new StringBuilder();
url.append(req.getRequestURL());
String returnURL = url.toString() + "?return=1&payKey=${payKey}&id="+ id + "&title=" + title;
String cancelURL = url.toString() + "?cancel=1&id="+ id + "&title=" + title;
//String ipnURL = url.toString() + "?action=ipn";
ParallelPay parallelPay = new ParallelPay(2);
parallelPay.setCancelUrl(cancelURL);
parallelPay.setReturnUrl(returnURL);
parallelPay.setCredentialObj(credentialObj);
parallelPay.setUserIp(req.getRemoteAddr());
parallelPay.setApplicationName("Sample app on GAE");
parallelPay.setCurrencyCode(CurrencyCodes.USD);
parallelPay.setEnv(ServiceEnvironment.SANDBOX);
//parallelPay.setIpnURL(ipnURL);
parallelPay.setLanguage("en_US");
parallelPay.setMemo(title);
// set the receivers
Receiver primaryReceiver = new Receiver();
primaryReceiver.setAmount(5.0);
primaryReceiver.setEmail("jagdis_1325390370_biz#yahoo.com");
primaryReceiver.setPaymentType(PaymentType.GOODS);
parallelPay.addToReceivers(primaryReceiver);
// set the second receivers
Receiver rec1 = new Receiver();
rec1.setAmount(3.0);
rec1.setEmail("jagdis_1331173124_biz#yahoo.com");
rec1.setPaymentType(PaymentType.GOODS);
parallelPay.addToReceivers(rec1);
PayResponse payResponse = parallelPay.makeRequest();
log.info("Payment success - payKey:" + payResponse.getPayKey());
} catch (IOException e) {
resp.getWriter().println("Payment Failed w/ IOException");
} catch (MissingAPICredentialsException e) {
// No API Credential Object provided - log error
e.printStackTrace();
resp.getWriter().println("No APICredential object provided");
} catch (InvalidAPICredentialsException e) {
// invalid API Credentials provided - application error - log error
e.printStackTrace();
resp.getWriter().println("Invalid API Credentials " + e.getMissingCredentials());
} catch (MissingParameterException e) {
// missing parameter - log error
e.printStackTrace();
resp.getWriter().println("Missing Parameter error: " + e.getParameterName());
} catch(ReceiversCountMismatchException e){
// missing receiver - log error
e.printStackTrace();
resp.getWriter().println("Receiver count did not match - expected: "
+ e.getExpectedNumberOfReceivers()
+ " - actual:" + e.getActualNumberOfReceivers());
} catch (RequestFailureException e) {
// HTTP Error - some connection issues ?
e.printStackTrace();
resp.getWriter().println("Request HTTP Error: " + e.getHTTP_RESPONSE_CODE());
} catch (InvalidResponseDataException e) {
// PayPal service error
// log error
e.printStackTrace();
resp.getWriter().println("Invalid Response Data from PayPal: \"" + e.getResponseData() + "\"");
} catch (PayPalErrorException e) {
// Request failed due to a Service/Application error
e.printStackTrace();
if(e.getResponseEnvelope().getAck() == AckCode.Failure){
// log the error
resp.getWriter().println("Received Failure from PayPal (ack)");
resp.getWriter().println("ErrorData provided:");
resp.getWriter().println(e.getPayErrorList().toString());
if(e.getPaymentExecStatus() != null){
resp.getWriter().println("PaymentExecStatus: " + e.getPaymentExecStatus());
}
} else if(e.getResponseEnvelope().getAck() == AckCode.FailureWithWarning){
// there is a warning - log it!
resp.getWriter().println("Received Failure with Warning from PayPal (ack)");
resp.getWriter().println("ErrorData provided:");
resp.getWriter().println(e.getPayErrorList().toString());
}
} catch (RequestAlreadyMadeException e) {
// shouldn't occur - log the error
e.printStackTrace();
resp.getWriter().println("Request to send a request that has already been sent!");
} catch (PaymentExecException e) {
resp.getWriter().println("Failed Payment Request w/ PaymentExecStatus: " + e.getPaymentExecStatus().toString());
resp.getWriter().println("ErrorData provided:");
resp.getWriter().println(e.getPayErrorList().toString());
}catch (PaymentInCompleteException e){
resp.getWriter().println("Incomplete Payment w/ PaymentExecStatus: " + e.getPaymentExecStatus().toString());
resp.getWriter().println("ErrorData provided:");
resp.getWriter().println(e.getPayErrorList().toString());
} catch (NumberFormatException e) {
// invalid number passed
e.printStackTrace();
resp.getWriter().println("Invalid number of receivers sent");
} catch (NotEnoughReceivers e) {
// not enough receivers - min requirements for Parallel pay not met
e.printStackTrace();
resp.getWriter().println("Min number of receivers not met - Min Required:"
+ e.getMinimumRequired() + " - actual set:" + e.getActualNumber());
} catch (AuthorizationRequiredException e) {
// redirect the user to PayPal for Authorization
resp.getWriter().println("\"PPAuthzUrl\": \"" + e.getAuthorizationUrl(ServiceEnvironment.SANDBOX) + "\", \"Status\": \"CREATED\"");
// resp.sendRedirect(e.getAuthorizationUrl(ServiceEnvironment.SANDBOX));
// resp.getWriter().println("\"PPAuthzUrl\": \"" + e.getEmbeddedPaymentsAuthorizationUrl(ServiceEnvironment.SANDBOX, ExpType.LIGHTBOX) + "\", \"Status\": \"CREATED\"");
}
} else if(id == null || id.length() <= 0) {
getServletConfig().getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp);
} else {
getServletConfig().getServletContext().getRequestDispatcher("/order.jsp").forward(req, resp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Check if you have the paypal.jar in lib directory. If you are not sure, unjar the paypal related lib files and check if you have AuthorizationRequiredException.class in at least one of them.

Related

Extent report version 4 - Create two extent reports instead of one html report for all extectued testcases

I am using extent reportversion 4 and want one .html report after executing of all the testcases but it creates two html reports for executing the 3 methods in testclass
In the testclass, I have writteh code like that #beforemethod will execute before executing each testcase, followed by executing the testcase & in #aftermethod it will flush the repot to generate Html report and afterthat using #afterclass annotations to quit the driver**
**Testclass:**
public class HomePageTest extends BaseClass {
HomePage homePage;
public HomePageTest() {
super();
}
#BeforeMethod
#Parameters({ "platformName", "url", "udid" })
public void setUpHomePageClass(String platformName, String url, String udid) throws Exception {
try {
BaseClass baseClass = new BaseClass();
baseClass.initialize_driver(platformName, url, udid);
homePage = new HomePage(driver);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
#BeforeMethod
#Parameters({ "platformName", "url", "udid" })
public void setUpHomePageClass(String platformName, String url, String udid) throws Exception {
try {
BaseClass baseClass = new BaseClass();
baseClass.initialize_driver(platformName, url, udid);
homePage = new HomePage(driver);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Test(priority = 1, description = "Verify element i.e Top50 Txt on homepage test")
#Severity(SeverityLevel.NORMAL)
#Description("TestCase Description: Verify element i.e Top50 Txt on homepage")
public void verifyeElementsOnHomePageTest() throws Exception {
log.info("***Executing verifyElementsOnHomeScreenTest***");
logger = extent.createTest("Verify the elements on HomePage after redirecting to the splash screen");
log.info("wait for continue_button to be clickable");
TestUtil.waitForElementToBeClickable(By.id("continue_button"));
homePage.clickContinueBtnAfterSplashScreen();
log.info("Clicked on continue_button");
log.info("waitForUserNameToBeClickable - username");
boolean flag = homePage.validateTop50Txt();
Assert.assertTrue(flag);
log.info("Top50Txt isDisplayed");
log.info("verifyElementsonHomeScreenTest Ended");
}
#Test(priority = 2, description = "Swipe to next video test")
#Severity(SeverityLevel.NORMAL)
#Description("TestCase Description: Swipe from one video to another")
public void swipeToNxtVideoTest() throws InterruptedException {
try {
logger = extent.createTest("Swipe from one video to another & get the username ");
log.info("***Executing swipeToNxtVideoTest***");
log.info("waitForElementToPresenceOfElementLocated - username");
TestUtil.waitForElementToPresenceOfElementLocated(By.id("user_name"));
log.info("swipeverticalDown for nxt video");
TestUtil.swipeverticalDown();
log.info("swipeToNxtVideoTest Ended");
} catch (Exception e) {
e.printStackTrace();
log.error("Found Exception - swipeToNxtVideoTest");
}
}
/*
* #Test(priority = 3, retryAnalyzer =
* com.automation.listeners.RetryAnalyzer.class ) public void checkFailure() {
* Assert.assertEquals(true, false); System.out.println("failed");
*
* }
*/
#AfterMethod
public void getResult(ITestResult result) throws Exception {
if (result.getStatus() == ITestResult.FAILURE) {
logger.log(Status.FAIL,
MarkupHelper.createLabel(result.getName() + " - Test Case Failed", ExtentColor.RED));
logger.log(Status.FAIL,
MarkupHelper.createLabel(result.getThrowable() + " - Test Case Failed", ExtentColor.RED));
String screenshotPath = TestUtil.captureScreenAsBase64(driver, result.getName());
logger.fail("Snapshot below: " + logger.addScreenCaptureFromPath(screenshotPath));
} else if (result.getStatus() == ITestResult.SKIP) {
logger.log(Status.SKIP,
MarkupHelper.createLabel(result.getName() + " - Test Case Skipped", ExtentColor.ORANGE));
} else if (result.getStatus() == ITestResult.SUCCESS) {
logger.log(Status.PASS,
MarkupHelper.createLabel(result.getName() + " Test Case PASSED", ExtentColor.GREEN));
}
extent.flush();
}
#AfterClass
public void quitDriver() {
getDriver().quit();
}
Please do let me know where I have been lacking in code; I might have a intuitions that there is an issue in testng annotations in my code
Base Class:
DesiredCapabilities capabilities = new DesiredCapabilities();
public void setDriver(AppiumDriver<MobileElement> driver) {
tdriver.set(driver);
}
public static synchronized AppiumDriver<MobileElement> getDriver() {
return tdriver.get();
}
public BaseClass() {
try {
prop = new Properties();
FileInputStream ip = new FileInputStream(
System.getProperty("user.dir") + "/src/main/java/com/automation/config/config.properties");
prop.load(ip);
// extend reports
Date date = new Date();
SimpleDateFormat dateFormatFolder = new SimpleDateFormat("dd_MMM_yyyy");
File ResultDir = new File(System.getProperty("user.dir") + File.separator + "/FrameworkReports/"
+ dateFormatFolder.format(date));
// Defining Directory/Folder Name
if (!ResultDir.exists()) { // Checks that Directory/Folder Doesn't Exists!
ResultDir.mkdir();
}
SimpleDateFormat dateFormat = new SimpleDateFormat("dd_MMM_yyyy_hh_mm_ssaa");
htmlReporter = new ExtentHtmlReporter(
ResultDir + "/" + "Report" + " " + dateFormat.format(date) + " .html");
htmlReporter.config().setDocumentTitle("Automation Report");
htmlReporter.config().setReportName("YOVO AUTOMATION");
htmlReporter.config().setTheme(Theme.DARK);
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
extent.setSystemInfo("Host Name", "localhost");
extent.setSystemInfo("Environment", "Windows 7");
extent.setSystemInfo("User Name", "Abhishek Chauhan");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void initialize_driver(String platformName, String url, String udid) throws Exception {
log = LogManager.getLogger(BaseClass.class);
BasicConfigurator.configure();
File appDir = new File("/src/main/resources/apk");
File app = new File(appDir, "yovoapp-release.apk");
mDirpath = System.getProperty("user.dir");
mApkfilepath = mDirpath + "/app/yovoapp-release.apk";
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, platformName);
capabilities.setCapability(MobileCapabilityType.UDID, udid);
switch (platformName) {
case "Android":
capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 60);
capabilities.setCapability("appPackage", prop.getProperty("androidAppPackage"));
capabilities.setCapability("appActivity", prop.getProperty("androidAppActivity"));
capabilities.setCapability("app", mApkfilepath);
capabilities.setCapability("noReset", true);
driver = new AppiumDriver<MobileElement>(new URL(url), capabilities);
// tdriver.set(driver);
// return getDriver();
case "IOS":
File classpathRoot = new File(System.getProperty("user.dir"));
// File appDir = new File(classpathRoot, "/build/");
// File app = new File(appDir, "WordPress.app");
capabilities.setCapability("platformVersion", "9.2");
capabilities.setCapability("deviceName", "iPhone 6");
capabilities.setCapability("app", app.getAbsolutePath());
// driver = new IOSDriver<MobileElement>(new
// URL("http://127.0.0.1:4723/wd/hub"), caps);
break;
default:
throw new Exception("Invalid platform! - " + platformName);
}
setDriver(driver);
}

LoginFailed on connecting outlook 365

package com.email.test;
import java.util.Arrays;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Flags.Flag;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.FlagTerm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class SampleEmailReader {
public static final int SUCCESS = 0;
public static final int FAILURE = -1;
private transient static final Logger logger = LoggerFactory.getLogger(SampleEmailReader.class);
public SampleEmailReader() {
}
public void processEmailAttachements() {
try {
logger.info("Started processEmailAttachements ");
Properties emailProperties = new Properties();
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
emailProperties.setProperty("mail.imaps.socketFactory.class", SSL_FACTORY);
emailProperties.setProperty("mail.imaps.socketFactory.fallback", "false");
emailProperties.setProperty("mail.imaps.port", "993");
emailProperties.setProperty("mail.imaps.socketFactory.port", "993");
emailProperties.setProperty("mail.imaps.auth", "true");
emailProperties.setProperty("mail.imaps.host", "outlook.office365.com");
emailProperties.setProperty("mail.imaps.auth.plain.disable", "true");
emailProperties.setProperty("mail.imaps.auth.gssapi.disable", "true");
emailProperties.setProperty("mail.imaps.auth.ntlm.disable", "true");
emailProperties.setProperty("mail.imaps.ssl.enable", "true");
Session session = Session.getInstance(emailProperties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("test#outlook.com", "password#2016");
}
});
Store store = session.getStore("imaps");
store.connect();
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
logger.info("Inbox MessageCount-->" + inbox.getMessageCount());
Message messages[] = inbox.search(new FlagTerm(
new Flags(Flag.SEEN), false));
logger.info("Number of UnRead Mails = " + messages.length);
for (Message inboxMessage : messages) {
int status = processMessageBody(inboxMessage);
if (status == SUCCESS) {
inboxMessage.setFlag(Flag.SEEN, true);
} else {
inboxMessage.setFlag(Flag.SEEN, false);
}
}
inbox.close(true);
store.close();
} catch (Exception ex) {
ex.printStackTrace();
logger.error("Error in processEmailAttachements", ex);
}
}
private int processMessageBody(Message message) {
int status = FAILURE;
try {
logger.info("Started processMessageBody*******");
logger.info("Message Subject******" + message.getSubject());
logger.info("From Address *******"
+ Arrays.toString(message.getFrom()));
Object content = message.getContent();
if (content instanceof String) {
logger.error("Invalid Content Type.No need to process the Mail with Subject "
+ message.getSubject());
} else if (content instanceof Multipart) {
Multipart multiPart = (Multipart) content;
try {
for (int i = 0; i < multiPart.getCount(); i++) {
BodyPart bodyPart = multiPart.getBodyPart(i);
if (bodyPart.getDisposition() != null
&& bodyPart.getDisposition().equalsIgnoreCase(
Part.ATTACHMENT)
&& bodyPart.getFileName() != null) {
status = readExcel(bodyPart);
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error("BatchException in procesMultiPart", e);
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error("Exception in processMessageBody", e);
}
return status;
}
public abstract int readExcel(BodyPart bodyPart);
}
Executing
package EmailTable;
import javax.mail.BodyPart;
public class Email {
public static void main(String[] args) {
// TODO Auto-generated method stub
SampleEmailReader reader = new SampleEmailReader() {
#Override
public int readExcel(BodyPart bodyPart) {
// TODO Auto-generated method stub
return 0;
}
};
reader.processEmailAttachements();
}
}
Getting Autheticate Failed.. But username password is correct. and i am able to login outlook 365 with the username and password. ONLY difference I am seeing there is certitificate displayed on the browser.. Do I need to use any certificate to read the mail?
javax.mail.AuthenticationFailedException: LOGIN failed.
at com.sun.mail.imap.IMAPStore.protocolConnect

How to correctly access rocksdb in flink stream task?

I tried to use rocksdb to cache information required by a ProcessFunction, and following seems to be the only way to get it to work by far:
(1) load data from datastore (eg. mysql) and put the data into rocksdb then close the rocksdb handle in open().
(2) open & close rocksdb handle whenever the processElement() is invoked.
like this:
public static class MatchFunction extends ProcessFunction<TaxiRide, TaxiRide> {
// keyed, managed state
// holds an END event if the ride has ended, otherwise a START event
private ValueState<TaxiRide> rideState;
private RocksDB rocksdb = null;
private String dbPath = null;
#Override
public void close() throws Exception {
super.close();
if(rocksdb != null) {
rocksdb.close();
}
}
#Override
public void open(Configuration config) {
ValueStateDescriptor<TaxiRide> startDescriptor =
new ValueStateDescriptor<>("saved ride", TaxiRide.class);
rideState = getRuntimeContext().getState(startDescriptor);
if(rocksdb == null) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connect = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connect = DriverManager
.getConnection("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&"
+ "user=user&password=password");
preparedStatement = connect.prepareStatement("select * from test.feature");
resultSet = preparedStatement.executeQuery();
RocksDB.loadLibrary();
try (final Options options = new Options().setCreateIfMissing(true)) {
// a factory method that returns a RocksDB instance
dbPath = "/tmp/checkpoints/rocksdb/test01_" + UUID.randomUUID();
try (final RocksDB db = RocksDB.open(options, dbPath)) {
rocksdb = db;
System.out.println("db opened: " + dbPath);
String key01 = "key01";
String val01 = "val01";
while (resultSet.next()) {
key01 = resultSet.getString(1);
val01 = resultSet.getString(2);
System.out.println("before put " + key01 + ":" + val01);
rocksdb.put(key01.getBytes(), val01.getBytes());
System.out.println("after put " + key01 + ":" + val01);
}
}
} catch (RocksDBException e) {
// do some error handling
e.printStackTrace();
} finally {
if(rocksdb != null) {
rocksdb.close();
System.out.println("db closed: " + dbPath);
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connect != null) {
try {
connect.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
#Override
public void processElement(TaxiRide ride, Context context, Collector<TaxiRide> out) throws Exception {
TimerService timerService = context.timerService();
try (final Options options = new Options().setCreateIfMissing(true)) {
// a factory method that returns a RocksDB instance
try (final RocksDB db = RocksDB.open(options, dbPath)) {
rocksdb = db;
// System.out.println("db opened: " + dbPath);
String val01 = new String(rocksdb.get("f8416af7-b895-4f28-bcea-be1eef6bbdb2".getBytes()));
// System.out.println(">>> val01 = " + val01);
rocksdb.close();
// System.out.println("db closed: " + dbPath);
}
} catch (RocksDBException e) {
// do some error handling
e.printStackTrace();
}
if (ride.isStart) {
// the matching END might have arrived first (out of order); don't overwrite it
if (rideState.value() == null) {
rideState.update(ride);
}
} else {
rideState.update(ride);
}
timerService.registerEventTimeTimer(ride.getEventTime() + 120 * 60 * 1000);
}
#Override
public void onTimer(long timestamp, OnTimerContext context, Collector<TaxiRide> out) throws Exception {
TaxiRide savedRide = rideState.value();
if (savedRide != null && savedRide.isStart) {
out.collect(savedRide);
}
rideState.clear();
}
}
This is very inefficient since lots of IO happens in processElement(). This ProcessFunction was able to process all data in 10 minutes, it takes more then 40 minutes to process partial data after adding the rocksdb related lines. So I tried to resuse the rocksdb handled created in open() with the following implementation.
public static class MatchFunction extends ProcessFunction<TaxiRide, TaxiRide> {
// keyed, managed state
// holds an END event if the ride has ended, otherwise a START event
private ValueState<TaxiRide> rideState;
private RocksDB rocksdb = null;
private String dbPath = null;
#Override
public void close() throws Exception {
super.close();
if(rocksdb != null) {
rocksdb.close();
}
}
#Override
public void open(Configuration config) {
ValueStateDescriptor<TaxiRide> startDescriptor =
new ValueStateDescriptor<>("saved ride", TaxiRide.class);
rideState = getRuntimeContext().getState(startDescriptor);
if(rocksdb == null) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connect = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connect = DriverManager
.getConnection("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&"
+ "user=user&password=password");
preparedStatement = connect.prepareStatement("select * from test.feature");
resultSet = preparedStatement.executeQuery();
RocksDB.loadLibrary();
try (final Options options = new Options().setCreateIfMissing(true)) {
// a factory method that returns a RocksDB instance
dbPath = "/tmp/checkpoints/rocksdb/test01_" + UUID.randomUUID();
try (final RocksDB db = RocksDB.open(options, dbPath)) {
rocksdb = db;
System.out.println("db opened: " + dbPath);
String key01 = "key01";
String val01 = "val01";
while (resultSet.next()) {
key01 = resultSet.getString(1);
val01 = resultSet.getString(2);
System.out.println("before put " + key01 + ":" + val01);
rocksdb.put(key01.getBytes(), val01.getBytes());
System.out.println("after put " + key01 + ":" + val01);
}
}
} catch (RocksDBException e) {
// do some error handling
e.printStackTrace();
} finally {
// if(rocksdb != null) {
// rocksdb.close();
// System.out.println("db closed: " + dbPath);
// }
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connect != null) {
try {
connect.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
#Override
public void processElement(TaxiRide ride, Context context, Collector<TaxiRide> out) throws Exception {
TimerService timerService = context.timerService();
//try (final Options options = new Options().setCreateIfMissing(true)) {
// // a factory method that returns a RocksDB instance
// try (final RocksDB db = RocksDB.open(options, dbPath)) {
// rocksdb = db;
// System.out.println("db opened: " + dbPath);
String val01 = new String(rocksdb.get("f8416af7-b895-4f28-bcea-be1eef6bbdb2".getBytes()));
// System.out.println(">>> val01 = " + val01);
// rocksdb.close();
// System.out.println("db closed: " + dbPath);
// }
//} catch (RocksDBException e) {
// // do some error handling
// e.printStackTrace();
//}
if (ride.isStart) {
// the matching END might have arrived first (out of order); don't overwrite it
if (rideState.value() == null) {
rideState.update(ride);
}
} else {
rideState.update(ride);
}
timerService.registerEventTimeTimer(ride.getEventTime() + 120 * 60 * 1000);
}
#Override
public void onTimer(long timestamp, OnTimerContext context, Collector<TaxiRide> out) throws Exception {
TaxiRide savedRide = rideState.value();
if (savedRide != null && savedRide.isStart) {
out.collect(savedRide);
}
rideState.clear();
}
}
The problem with this implementation is that it just doesn't work and here is the error message I got:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x000000012c94cf55, pid=64626, tid=39683
#
# JRE version: Java(TM) SE Runtime Environment (8.0_60-b27) (build 1.8.0_60-b27)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.60-b23 mixed mode bsd-amd64 compressed oops)
# Problematic frame:
# [thread 39171 also had an error]
06:52:56.163 [pool-11-thread-1] INFO o.a.flink.contrib.streaming.state.RocksDBKeyedStateBackend - Asynchronous RocksDB snapshot (File Stream Factory # file:/tmp/checkpoints/53224270d2f2be67a9d20f9deac66d09, asynchronous part) in thread Thread[pool-11-thread-1,5,Flink Task Threads] took 10 ms.
06:52:56.163 [pool-16-thread-1] INFO o.a.flink.contrib.streaming.state.RocksDBKeyedStateBackend - Asynchronous RocksDB snapshot (File Stream Factory # file:/tmp/checkpoints/53224270d2f2be67a9d20f9deac66d09, asynchronous part) in thread Thread[pool-16-thread-1,5,Flink Task Threads] took 12 ms.
C06:52:56.163 [pool-13-thread-1] INFO o.a.flink.contrib.streaming.state.RocksDBKeyedStateBackend - Asynchronous RocksDB snapshot (File Stream Factory # file:/tmp/checkpoints/53224270d2f2be67a9d20f9deac66d09, asynchronous part) in thread Thread[pool-13-thread-1,5,Flink Task Threads] took 9 ms.
[librocksdbjni-osx.jnilib+0x3ff55] _Z18rocksdb_get_helperP7JNIEnv_PN7rocksdb2DBERKNS1_11ReadOptionsEPNS1_18ColumnFamilyHandleEP11_jbyteArrayii+0xe5
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
06:52:56.163 [pool-12-thread-1] INFO o.a.flink.contrib.streaming.state.RocksDBKeyedStateBackend - Asynchronous RocksDB snapshot (File Stream Factory # file:/tmp/checkpoints/53224270d2f2be67a9d20f9deac66d09, asynchronous part) in thread Thread[pool-12-thread-1,5,Flink Task Threads] took 13 ms.
[thread 46339 also had an error]
# An error report file with more information is saved as:
# /Users/abc/MyFiles/workspace/flink-java-project/hs_err_pid64626.log
[thread 22279 also had an error]
[thread 33027 also had an error]
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
Detail trace from "/Users/abc/MyFiles/workspace/flink-java-project/hs_err_pid64626.log" can be found in this link (http://memyselfandtaco.blogspot.tw/2018/04/how-to-correctly-access-rocksdb-in.html)

Apache Camel hanging/locking up - threadsafe issue?

Very old project I have 'inherited', Apache 2.2.0 and apparently has never 'worked right'.
The process seems to 'lock up' occasionally and never completes. If the process is killed, files moved back to inbound, and restarted it seems to work. So it's a weird intermittent problem.
I believe the issue may be due to a custom RoutePolicy implementation;
package com.company.integration.management;
import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.Route;
import org.apache.camel.SuspendableService;
import org.apache.camel.component.seda.SedaEndpoint;
import org.apache.camel.impl.ThrottlingInflightRoutePolicy;
import org.apache.log4j.Logger;
public class ParserInflightRoutePolicy extends ThrottlingInflightRoutePolicy {
private static Route fileRoute;
private static final int MAX_FILES_INFLIGHT = 3;
private static final int MSG_HIGH_WATERMARK = 100;
private static final int MSG_LOW_WATERMARK = 25;
private Logger logger = Logger.getLogger(getClass());
#Override
public void onExchangeBegin(Route route, Exchange exchange) {
int sedaMessagesInflight = 0;
sedaMessagesInflight = sedaQueueSize(route);
if (route.getId().equalsIgnoreCase("initialRoute")) {
fileRoute = route;
try {
if ((sedaQueueSize(route, "seda://parser") >= MAX_FILES_INFLIGHT) || (sedaMessagesInflight >= MSG_HIGH_WATERMARK)) {
if ( ! ((SuspendableService)fileRoute.getConsumer()).isSuspended() ) {
logger.error("FLOW CONTROL: stop file component");
((SuspendableService)fileRoute.getConsumer()).suspend();
}
}
}
catch (Exception ex) {
logger.error("Error stopping route", ex);
}
}
if (route.getId().equalsIgnoreCase("pricingRoute") || route.getId().equalsIgnoreCase("persistenceRoute")) {
try {
if ((sedaQueueSize(route, "seda://parser") == 0) && (sedaMessagesInflight <= MSG_LOW_WATERMARK)) {
if ( ((SuspendableService)fileRoute.getConsumer()).isSuspended() ) {
logger.error("FLOW CONTROL: start file component");
((SuspendableService)fileRoute.getConsumer()).resume();
}
}
}
catch (Exception ex) {
logger.error("Error starting route", ex);
}
}
}
int sedaQueueSize(Route route) {
int sedaMessagesInflight = 0;
CamelContext camelContext = route.getRouteContext().getCamelContext();
for (Route rte : camelContext.getRoutes()) {
Endpoint endpoint = rte.getEndpoint();
if (endpoint instanceof SedaEndpoint) {
SedaEndpoint seda = (SedaEndpoint)endpoint;
if (seda.getQueue().size() > 0) {
sedaMessagesInflight += seda.getQueue().size();
}
}
}
logger.debug("SEDA messages inflight [" + sedaMessagesInflight + "]");
return sedaMessagesInflight;
}
int sedaQueueSize(Route route, String endPointUri) {
CamelContext camelContext = route.getRouteContext().getCamelContext();
Endpoint endpoint = camelContext.getEndpoint(endPointUri);
if (endpoint instanceof SedaEndpoint) {
return ((SedaEndpoint) endpoint).getQueue().size();
}
return 0;
}
#Override
public void onExchangeDone(Route route, Exchange exchange) {
;
}
}
Notice that this code does not appear to be threadsafe - no locking mechanism as implemented in ThottlingInflightRoutePolicy. Could this be causing the intermittent issue?

How to print from servlet to webpage?

Hi I am trying to print the list from servlet to web screen (jsp)
I am using log and it's not working.
Is there anyway to do or am I using this wrong?
private static final Logger log = Logger.getLogger(TodoServiceServlet.class.getName());
.....
Todo tmp = pm.getObjectById(Todo.class, user.getEmail());
System.out.println("user email: " + user.getEmail());
if(tmp==null){
log.info("You have not stored any todo lists yet");
}else{
System.out.println("user email is there?: " + tmp.getEmail());
System.out.println("start printing");
ArrayList<String> todolists = tmp.getList();
if(todolists==null)
System.out.println("Arraylist null");
if(!todolists.isEmpty()){
for(String t : todolists){
System.out.println("In the list: " + t);
log.info("You need to do: " + t);
}
}else{
log.info("You have nothing to do chil out!");
}
System.out would print to "standard out" on the web server (usually the console) not to the screen. What you have to do is instead write to the HttpServletResponse
so something like this:
public void doGet(HttpServletRequest req, HttpServletResponse resp) {
try {
resp.getWriter().println("user email is there?: " + tmp.getEmail());
} catch (IOException e) {
// handle your error here
}
}

Resources