How to get same value for current time in milliseconds on localhost and server? - ntp

I have created a web listener which contains a ScheduledExecutorService to run a job on a specific date and time and after that at regular fixed time interval. This scheduler checks for a specific date and time to do the job. The scheduler works with UTC time.
The problem is when I run it on my localhost it works perfectly but when I put it on remote server (server and my localhost are not in same TimeZone) it fails to work properly( they produce different value of System.currentTimeMillis() . With the help of a logger I have checked the value of System.currentTimeMillis() both on localhost and server and they are different.
Here is my Scheduler.
private static ScheduledExecutorService execService = Executors.newScheduledThreadPool(1, namedThreadFactory);
final Calendar date = new GregorianCalendar();
date.setTimeZone(TimeZone.getTimeZone("UTC"));
execService.scheduleAtFixedRate(new Runnable() {
#Override
public void run() {
logger.info("Scheduler ran");
try {
if (date.get(Calendar.DAY_OF_MONTH) == 1) {
if (!(System.currentTimeMillis() - getStartTime().getTime() > 300000)) {
logger.info("Doing the job");
myJob();
}
else{
logger.info("Too late after scheduled time");
}
} else {
logger.info("Current Day of Month: " + date.get(Calendar.DAY_OF_MONTH) + ". Job will be done on 1st Day of the month");
}
} catch (Exception ex) {
logger.info(ex);
}
}
}, getInitialDelay(), (2*60000), TimeUnit.MILLISECONDS);
}
private static long getOffsetTime() {
Calendar dateForOffset = new GregorianCalendar();
dateForOffset.setTimeZone(TimeZone.getTimeZone("UTC"));
int days = dateForOffset.getActualMaximum(Calendar.DAY_OF_MONTH);
switch (days) {
case 31:
offsetTimeInMilli = PERIOD;
break;
case 29:
offsetTimeInMilli = (-1) * PERIOD;
break;
case 28:
offsetTimeInMilli = (-2) * PERIOD;
break;
default:
break;
}
return offsetTimeInMilli;
}
private static long getInitialDelay() {
long currentTime = System.currentTimeMillis();
logger.info( "Current Time " + currentTime);
logger.info("Set Time to do the job -->" + getStartTime().getTime() + " Milliseconds");
initialDelay = getStartTime().getTime() - currentTime;
logger.info("Initial Delay Found : " + initialDelay + " Milliseconds");
return initialDelay;
}
private static Date getStartTime() {
Calendar d10am = Calendar.getInstance();
d10am.setTimeZone(TimeZone.getTimeZone("UTC"));
//logger.info("Day of Month ----- >" + d10am.get(Calendar.DAY_OF_MONTH));
//d10am.set(Calendar.DAY_OF_MONTH, 1);
d10am.set(Calendar.HOUR_OF_DAY, 5);
d10am.set(Calendar.MINUTE, 55);
d10am.set(Calendar.SECOND, 0);
d10am.set(Calendar.MILLISECOND, 0);
d10am.get(Calendar.DAY_OF_MONTH);
//logger.info("Day of Month ----- >" + Calendar.DAY_OF_MONTH);
return d10am.getTime();
}

One or both of them must be wrong. The value System.currentTimeMillis() is independent of time zones. Really, the proper way to synchronize times on these servers is for each server to synchronize with a time server using NTP (Network Time Protocol).
This is more of a systems administration problem than a Java programming problem.

Related

How can I have a conditional statement that performs two tasks? first one and then the other in a loop

So I am trying to code a program in arduino that does a task for 2 minutes and then another task for 5 mins. first one and then the other in a loop until a different condition is met.
Ive been trying to do this with if statements and while loops but im getting lost in the time part i think
//Pneumatic feed system
double minute = 1000*60;
double vibTime = 2 * minute; //2 mins
//double vibTime = 5000;
double execTime = millis();
double waitTime = 5 * minute; //time waits between vibrating
//double waitTime = 10000;
void DoPneuFeed() {
//Serial.print("Delta:");
//Serial.println(millis()-execTime);
if(Temp_Data[T_9] < 500)
{
if(execTime + vibTime < millis())
{
//turn on vibration for 2 mins
execTime = millis();
Serial.println("VIBRATING");
}
if(execTime + waitTime < millis())
{
//turn off vibration for 5 mins
execTime = millis();
Serial.println("WAITING");
}
}
if(Temp_Data[T_9] > 500)
{
relayOff(3);
relayOff(7);
}
}
void PneuFeed()
{
if(execTime + vibTime > millis())
{
//air pressure open
relayOn(3);
//vibrator on
relayOn(7);
}
else if(execTime + waitTime > millis())
{
relayOff(3);
relayOff(7);
}
}
I want to turn on the vibration mode for 2 mins and then turn it off for 5 mins. as long as the Temp_Data(T_9) < 500. if it is greater than 500 it should stay off.
Without going into the specifics of your code (since I cannot build it to test.) I only have one quick suggestion:
Instead of using a series of if-then-else (or variations of it) consider using a state machine. Implementations often include use of a switch() inside a while(expression){...} loop. The following is a very simple example of how you might be able to do the steps you need in this construct:
(Note, this is a mix of C and pseudo code for illustration. It is close to compilable , but contains a few undefined items.)
typedef enum {
START,
SET_VIB,
CHECK_TEMP,
GET_TIME,
CLEANUP,
SLEEP,
MAX_STATE
}STATE;
enum {
IN_WORK,
FAILED,
SUCCESS
};
enum {// durations in minutes
MIN_2,
MIN_5,
MIN_10,
MAX_DUR// change/add durations as profile needs
};
const int dur[MAX_DUR] = {120, 300, 600};
int RunProcess(STATE state);
int main(void)
{
STATE state = START;
RunProcess(state);
return 0;
}
int RunProcess(STATE state)//Add arguments here for temperature,
{ //Sleep duration, Cycles, etc. so they are not hardcoded.
int status;
time_t elapsed = 0; //s
BOOL running = TRUE;
double temp, setpoint;
int duration;
time_t someMsValue;
while(running)
switch (state) {
case START:
//Power to oven
//Power to vibe
//initialize state and status variables
duration = dur[MIN_2];
state = SLEEP;
status = IN_WORK;
break;
case SET_VIB:
//test and control vibe
if(duration == dur[MIN_2])
{
duration = dur[MIN_5];
//& turn off vibe
}
else
{
duration = dur[MIN_2];
//& turn on vibe
}
//init elapsed
elapsed = 0;
status = IN_WORK;
state = CHECK_TEMP;
break;
case CHECK_TEMP:
//read temperature
if(temp < setpoint)
{
state = SLEEP;
status = IN_WORK;
}
else
{
state = CLEANUP;
status = SUCCESS;
}
break;
case GET_TIME:
elapsed = getTime();
if(elapsed > duration) state = SET_VIB;
else state = SLEEP;
status = IN_WORK;
break;
case CLEANUP:
//turn off heat
//turn off vibe
running = FALSE;
break;
case SLEEP:
Sleep(someMsValue);
state = GET_TIME;
status = IN_WORK;
break;
default:
// called with incorrect state value
// error message and exit
status = FAILED;
state = CLEANUP;
break;
}
return status;
}
Suggestion for improvement to this illustration:
Expand this code to read in a "profile" file. It could include such things as parameter values typical to your process, such as temperature profiles, vibration profiles, number of cycles, etc. With such information, all of the hard-coding used here as illustration could be replaced with run-time configurable parameters, allowing the system to use many different profiles
without having to recompile the executable each time.
Another state machine example.

Waiting time shown nothing in cloudsim why?

/*
* Title: CloudSim Toolkit
* Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation
* of Clouds
* Licence: GPL - http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2009, The University of Melbourne, Australia
*/
//package org.cloudbus.cloudsim.examples;
//package cloudIntro;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import org.cloudbus.cloudsim.Cloudlet;
import org.cloudbus.cloudsim.CloudletSchedulerTimeShared;
import org.cloudbus.cloudsim.Datacenter;
import org.cloudbus.cloudsim.DatacenterBroker;
import org.cloudbus.cloudsim.DatacenterCharacteristics;
import org.cloudbus.cloudsim.Host;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.Pe;
import org.cloudbus.cloudsim.Storage;
import org.cloudbus.cloudsim.UtilizationModel;
import org.cloudbus.cloudsim.UtilizationModelFull;
import org.cloudbus.cloudsim.Vm;
import org.cloudbus.cloudsim.VmAllocationPolicySimple;
import org.cloudbus.cloudsim.VmSchedulerTimeShared;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple;
/**
* A simple example showing how to create
* a datacenter with two hosts and run two
* cloudlets on it. The cloudlets run in
* VMs with different MIPS requirements.
* The cloudlets will take different time
* to complete the execution depending on
* the requested VM performance.
*/
public class Simulation
{
/**
* Creates main() to run this example
*/
public static void main(String[] args)
{
Log.printLine("Starting CloudSimExample3...");
try
{
int noOfTypes = 6;
// First step: Initialize the CloudSim package. It should be called
// before creating any entities.
int num_user = 1; // number of cloud users
Calendar calendar = Calendar.getInstance();
boolean trace_flag = false; // mean trace events
// Initialize the CloudSim library
CloudSim.init(num_user, calendar, trace_flag);
// Second step: Create Datacenters
//Datacenters are the resource providers in CloudSim. We need at list one of them to run a CloudSim simulation
#SuppressWarnings("unused")
Datacenter datacenter0 = createDatacenter("Datacenter_0");
//Third step: Create Broker
//Fourth step: Create one virtual machine
//VM description
int vmid = 0;
int mips = 250;
long size = 10000; //image size (MB)
int ram = 512; //vm memory (MB)
long bw = 1000;
int pesNumber = 1; //number of cpus
String vmm = "Xen"; //VMM name
//Fifth step: Create two Cloudlets
//Cloudlet properties
int id = 0;
long length = 40000;
long fileSize = 300;
long outputSize = 300;
UtilizationModel utilizationModel = new UtilizationModelFull();
int vmNumberArray[] = {1,1,1,1,1,1};
int cloudletNumberArray[] = {500,500,500,500,500,500};
ArrayList<DatacenterBroker> brokerList = new ArrayList<>();
for(int i=0;i<noOfTypes;i++) // create 6 types of vm
{
List<Cloudlet> cloudletList = new ArrayList<>();
List<Vm> vmlist = new ArrayList<>();
DatacenterBroker broker = createBroker();
brokerList.add(broker);
int brokerId = broker.getId();
//int randomNumber1 = ThreadLocalRandom.current().nextInt(1,15);
int randomNumber1 = vmNumberArray[i];
for(int j=0;j<randomNumber1;j++)
{
Vm vm = new Vm(vmid, brokerId, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());
vmid++;
vmlist.add(vm);
}
//int randomNumber2 = ThreadLocalRandom.current().nextInt(20,30);
int randomNumber2 = cloudletNumberArray[i];
for(int j=0;j<randomNumber2;j++)
{
Cloudlet cloudlet = new Cloudlet(id, length, pesNumber, fileSize, outputSize, utilizationModel, utilizationModel, utilizationModel);
id++;
cloudlet.setUserId(brokerId);
cloudletList.add(cloudlet);
}
broker.submitVmList(vmlist);
broker.submitCloudletList(cloudletList);
}
long start = System.nanoTime();
CloudSim.startSimulation();
for(int i=0;i<noOfTypes;i++)
{
List<Cloudlet> cloudletList = brokerList.get(i).getCloudletReceivedList();
printCloudletList(cloudletList);
}
CloudSim.stopSimulation();
long end = System.nanoTime();
Log.printLine("CloudSim finished");
System.out.println("total time required = "+(end-start));
}
catch (Exception e) {
e.printStackTrace();
Log.printLine("The simulation has been terminated due to an unexpected error");
}
}
private static Datacenter createDatacenter(String name)
{
// Here are the steps needed to create a PowerDatacenter:
// 1. We need to create a list to store
// our machine
List<Host> hostList = new ArrayList<Host>();
// 2. A Machine contains one or more PEs or CPUs/Cores.
// In this example, it will have only one core.
List<Pe> peList = new ArrayList<Pe>();
int mips = 60000;
// 3. Create PEs and add these into a list.
peList.add(new Pe(0, new PeProvisionerSimple(mips))); // need to store Pe id and MIPS Rating
//4. Create Hosts with its id and list of PEs and add them to the list of machines
int hostId=0;
int ram = 307230720; //host memory (MB)
long storage = 1000000; //host storage
int bw = 10000000;
hostList.add(
new Host(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList,
new VmSchedulerTimeShared(peList)
)
); // This is our first machine
//create another machine in the Data center
/*List<Pe> peList2 = new ArrayList<Pe>();
peList2.add(new Pe(0, new PeProvisionerSimple(mips)));
hostId++;
hostList.add(
new Host(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList2,
new VmSchedulerTimeShared(peList2)
)
);*/ // This is our second machine
// 5. Create a DatacenterCharacteristics object that stores the
// properties of a data center: architecture, OS, list of
// Machines, allocation policy: time- or space-shared, time zone
// and its price (G$/Pe time unit).
String arch = "x86"; // system architecture
String os = "Linux"; // operating system
String vmm = "Xen";
double time_zone = 10.0; // time zone this resource located
double cost = 3.0; // the cost of using processing in this resource
double costPerMem = 0.05; // the cost of using memory in this resource
double costPerStorage = 0.001; // the cost of using storage in this resource
double costPerBw = 0.0; // the cost of using bw in this resource
LinkedList<Storage> storageList = new LinkedList<Storage>(); //we are not adding SAN devices by now
DatacenterCharacteristics characteristics = new DatacenterCharacteristics(
arch, os, vmm, hostList, time_zone, cost, costPerMem, costPerStorage, costPerBw);
// 6. Finally, we need to create a PowerDatacenter object.
Datacenter datacenter = null;
try {
datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), storageList, 0);
} catch (Exception e) {
e.printStackTrace();
}
return datacenter;
}
//We strongly encourage users to develop their own broker policies, to submit vms and cloudlets according
//to the specific rules of the simulated scenario
private static DatacenterBroker createBroker()
{
DatacenterBroker broker = null;
try
{
broker = new DatacenterBroker("Broker");
} catch (Exception e) {
e.printStackTrace();
return null;
}
return broker;
}
/**
* Prints the Cloudlet objects
* #param list list of Cloudlets
*/
private static void printCloudletList(List<Cloudlet> list)
{
int size = list.size();
Cloudlet cloudlet;
String indent = " ";
Log.printLine();
Log.printLine("========== OUTPUT ==========");
Log.printLine("Cloudlet ID" + indent + "STATUS" + indent +
"Data center ID" + indent + "VM ID" + indent + "Time" + indent + "Start Time" + indent + "Finish Time" + indent + "waiting Time");
DecimalFormat dft = new DecimalFormat("###.##");
for (int i = 0; i < size; i++)
{
cloudlet = list.get(i);
Log.print(indent + cloudlet.getCloudletId() + indent + indent);
if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS)
{
Log.print("SUCCESS");
Log.printLine( indent + indent + cloudlet.getResourceId() + indent + indent + indent + cloudlet.getVmId() +
indent + indent + dft.format(cloudlet.getActualCPUTime()) + indent + indent + dft.format(cloudlet.getExecStartTime())+
indent + indent + dft.format(cloudlet.getFinishTime()) + dft.format(cloudlet.getWaitingTime()));
}
}
}
}
This code is modified from cloudsim example 3 code https://github.com/Cloudslab/cloudsim/blob/master/modules/cloudsim-examples/src/main/java/org/cloudbus/cloudsim/examples/CloudSimExample3.java.
Here, I create one datacenter with one host and 6 datacenter broker. Each brocker has one VM and 500 cloudlet.
output file https://paste.ubuntu.com/p/ZmmK2nFmyS/
The output shows that within a brocker each cloudlet start at a time and the waiting time shows nothing that means zero(0). How 500 tasks start at a time in a VM where there is one cpu in the VM and no waiting time?
That is because you used the CloudletSchedulerTimeShared
instead of other implementations such as the CloudletSchedulerSpaceShared.
In real operating systems, even a time-shared scheduler will make some applications to wait if the number of CPUs is lower than the number of apps (which is usually the case).
CloudSim provides an over-simplified time-shared scheduler.
There is an answer that explains how such a scheduler is implemented here.

Missing rows from list when using dynamic task in dotnet

I am new in using TPL in .Net applications. While creating a simple console application to achieve some parallel tasks those are dynamically created, I am stuck with some issues.
Problem here is that when 10 tasks are created and run, although the console is showing all the 10 tasks, when writing those into a log file after putting a delay between consoling and logging, the log file misses some of the items randomly.
Below is my sample code (This is just a skeleton of my actual code)
class Program
{
public static int datacount = 10;
static void Main(string[] args)
{
List<Task> tasks = new List<Task>();
var s1 = DateTime.Now;
var transList = GenerateTransactionList();
foreach (var transaction in transList)
{
Transactions transactionNew = new Transactions();
transactionNew = transaction;
tasks.Add(Task.Factory.StartNew(() => serialMethod(transactionNew)));
}
Task.WhenAll(tasks).Wait();
Console.WriteLine("Completed!!!");
}
private static List<Transactions> GenerateTransactionList()
{
Random r = new Random();
List<Transactions> transactionList = new List<Transactions>();
for (int i = 1; i <= datacount; ++i)
{
Transactions tr = new Transactions();
tr.ID = 0;
tr.Amount = r.Next(1, 10);
tr.Created_By = "Iteration" + i;
tr.Notes = "Iteration" + i;
tr.Created_On = DateTime.Now;
transactionList.Add(tr);
}
return transactionList;
}
private static async Task<string> serialMethod(Transactions tlist)
{
Console.WriteLine("Started Serial Iteration" + tlist.Notes);
try
{
Console.WriteLine("Finished Serial Iteration" + tlist.Notes);
Thread.Sleep(10000);//doing some time consuming process
WriteLog("Parallel2", DateTime.Now, DateTime.Now, tlist.Notes);
return "Success";
}
catch (Exception ex)
{
Console.WriteLine("serialmethod" + ex.Message);
return "Failure";
}
}
public static void WriteLog(string type,
DateTime startTime, DateTime endTime,
string dataSet)
{
try
{
string logFolderPath = AppDomain.CurrentDomain.BaseDirectory + #"\Logs";
if (!Directory.Exists(logFolderPath))
Directory.CreateDirectory(logFolderPath);
string logFilePath = logFolderPath + #"\Log_" + DateTime.Today.ToString("yyyy.MM.dd") + ".csv";
string line = string.Empty;
if (!File.Exists(logFilePath))
{
line = #"""Type"",""Start Time"",""End Time"",""Duration"",""Iteration""";
writeLineToFile(logFilePath, line);
}
string duration = (endTime - startTime).ToString();
line = "\"" + type + "\"," +
"\"" + startTime.ToString("MM/dd/yyyy hh:mm:ss tt") + "\"," +
"\"" + endTime.ToString("MM/dd/yyyy hh:mm:ss tt") + "\"," +
"\"" + duration + "\"," +
"\"" + dataSet + "\"";
writeLineToFile(logFilePath, line);
}
catch (Exception)
{
//do nothing
}
}
private static void writeLineToFile(string fileName, string line)
{
using (var writer = new StreamWriter(fileName, true))
{
writer.WriteLine(line);
}
}
}
class Transactions
{
public int ID { get; set; }
public decimal Amount { get; set; }
public int Points { get; set; }
public string Notes { get; set; }
public string Created_By { get; set; }
public DateTime Created_On { get; set; }
}
Do you have any idea why this is happening. I have tried using ConcurrentBag instead of list. But that too is not helping. Please guide and let me know if I am missing anything or my implementation is completely wrong.
There a re a bunch of error-prone lines in your code:
You're overriding the reference for transaction in your foreach loop
You're using StartNew method instead of Tas.Run
You're using blocking WaitAll instead of await WhenAll, so you do block one thread in your application for no reason
You can simply switch to Parallel.Foreach instead of foreach
And most important: you're writing to the same file from different threads simultaneously, so they are basically interrupting each other. Either use some blocking to write the file (which cannot be done in parallel) or use some library for logging, like NLog or whatever, so it will handle logging for you
Your threads can run into situation when some of them trying to create file when other already done that, so move out the creation logic for file into one place (which the libraries like NLog will do for you properly)
Try to use object initializers instead of setting one property after another:
var tr = new Transactions
{
ID = 0,
Amount = r.Next(1, 10),
Created_By = "Iteration" + i,
Notes = "Iteration" + i,
Created_On = DateTime.Now
}

How to add 30 min to current system time and start countdown on button click

I have tried a lot but my textview is not changing.
Timer should automatically add 30 min to current time and it will be my future time event on button. Click
static final long ONE_MINUTE_IN_MILLIS=60000;//millisecs
Calendar date = Calendar.getInstance();
long t= date.getTimeInMillis();
Date afterAdding30Mins=new Date(t + (30 * ONE_MINUTE_IN_MILLIS));
private void countDownStart() {
runnable = new Runnable() {
#Override
public void run() {
try {
handler.postDelayed(this, 1000);
if (!current_date.after(afterAdding30Mins)) {
long diff = t+ 1800000;
long minutes = (diff / 1000) / 60;
long seconds = (diff / 1000) % 60;
String timeLeftFormattedm = String.format(Locale.getDefault(), "%02d", minutes);
String timeLeftFormatteds = String.format(Locale.getDefault(), "%02d", seconds);
//
tv_minute.setText(timeLeftFormattedm);
tv_second.setText(timeLeftFormatteds);
} else {
linear_layout_1.setVisibility(View.VISIBLE);
linear_layout_2.setVisibility(View.GONE);
handler.removeCallbacks(runnable);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
handler.postDelayed(runnable, 0);
}
protected void onStop() {
super.onStop();
handler.removeCallbacks(runnable);
}
}
What you need is a BroadcastReceiver. With a BroadcastReceiver, you can schedule tasks or events for the future and they'd still run/work even when the app is destroyed/shutdown. In your case, you can call the BroadcastReceiver immediately after your button click and schedule for 30 minutes.
To understand how BroadcastReceivers work: Read through this guide or take a look at this question.
This YouTube video would also give you a headstart,

GAE, Local datastore does not create

I have no idea in what extend GAE is not easy to understand :(
My servlet manipulate a json string and then I'm trying to store it in datastore.
When I run the application I'm getting this output:
Jan 27, 2014 6:59:04 PM com.google.appengine.api.datastore.dev.LocalDatastoreService load
INFO: The backing store, D:\Android\IntelliJ IDEA\workspace\EyeBall\AppEngine\out\artifacts\AppEngine_war_exploded\WEB-INF\appengine-generated\local_db.bin, does not exist. It will be created.
1
2
3
4
5
7
***
***
***
***
***
8
9
Although it's mentioned that local_db.bin will be created but when I navigate to that directory the file is not there. Also, when I open http://localhost:8080/_ah/admin/datastore in browser nothing displays in Entity Kind drop down list.
So wtf happene to local_db.bin? Why it doesn't generates?
any suggestion would be appreciated. thanks.
==================
UPDATE:
I added my code based on request.
private static final String NO_DEVICE_ID = "FFFF0000";
private static final String SAMPLE_JSON = "{\"history\":[{\"date\":null,\"info\":null,\"title\":\"Maybank2u.com\",\"url\":\"https://www.maybank2u.com.my/mbb/Mobile/info.do\",\"visits\":14},{\"date\":null,\"info\":null,\"title\":\"Maybank2u.com\",\"url\":\"https://www.maybank2u.com.my/mbb/Mobile/adaptInfo.do\",\"visits\":4},{\"date\":null,\"info\":null,\"title\":\"Maybank2u.com\",\"url\":\"http://www.maybank2u.com.my/mbb_info/m2u/public/personalBanking.do\",\"visits\":16},{\"date\":null,\"info\":null,\"title\":\"Maybank2u.com Online Financial Services\",\"url\":\"https://www.maybank2u.com.my/mbb/m2u/common/M2ULogin.do?action=Login\",\"visits\":52},{\"date\":null,\"info\":null,\"title\":\"‭BBC\",\"url\":\"http://www.bbc.co.uk/persian/\",\"visits\":16}]}";
private static final String QUERY_HISTORY_DEVICE = "SELECT m FROM HistoryDeviceJPA m WHERE m.userUUID = :keyword ORDER BY m.domain ASC";
private static final String QUERY_HISTORY = "SELECT m FROM HistoryJPA m WHERE m.pageAddress = :keyword";
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// displayError(response, "The page doesn't support httpGet");
String deviceId = NO_DEVICE_ID;
String content = SAMPLE_JSON;
System.out.println("1");
HistoryBrowser historyBrowser = parseJson(content);
if(historyBrowser == null)
return;
System.out.println("2");
List<HistoryBrowser.BrowserInfo> historyList = historyBrowser.getHistory();
if(historyList == null)
return;
System.out.println("3");
List<HistoryDeviceJPA> historyDeviceJPAList = new ArrayList<HistoryDeviceJPA>(historyList.size());
for(int i=0; i<historyList.size(); i++) {
try {
HistoryBrowser.BrowserInfo browser = historyList.get(i);
HistoryDeviceJPA historyDeviceJPA = new HistoryDeviceJPA();
historyDeviceJPA.setUserUUID(deviceId);
historyDeviceJPA.setDomain(getDomainName(browser.getUrl()));
historyDeviceJPA.setPageAddress(browser.getUrl());
historyDeviceJPA.setPageTitle(browser.getTitle());
historyDeviceJPA.setPageVisits(browser.getVisits());
historyDeviceJPAList.add(historyDeviceJPA);
} catch (URISyntaxException e) {
System.out.println(e.getMessage());
}
}
System.out.println("4");
// get history of device from data store
EntityManager em = EMF.get().createEntityManager();
Query q = em.createQuery(QUERY_HISTORY_DEVICE).setParameter("keyword", deviceId);
#SuppressWarnings("unchecked")
List<HistoryDeviceJPA> dbList = (List<HistoryDeviceJPA>) q.getResultList();
System.out.println("5");
// If there is no result (shows there is no record for that device)
if(dbList == null)
addHistoryDeviceJPAToDs(historyDeviceJPAList);
else {
System.out.println("7");
// find each item in datastore and replace them if needed
// if current page visit is less ot equal than previous visit don't do anything (remove item form historyDeviceJPAList)
outerLoop:
for(int i=0; i<historyDeviceJPAList.size(); i++) {
HistoryDeviceJPA deviceItem = historyDeviceJPAList.get(i);
System.out.println("***");
for(int j=0; j<dbList.size(); j++) {
HistoryDeviceJPA dbItem = dbList.get(j);
if(deviceItem.getPageAddress().equalsIgnoreCase(dbItem.getPageAddress())) {
if(deviceItem.getPageVisits() > dbItem.getPageVisits()) {
long diff = deviceItem.getPageVisits() - dbItem.getPageVisits();
dbItem.setPageVisits(deviceItem.getPageVisits());
HistoryJPA historyJPA = findHistoryJPA(dbItem.getPageAddress());
historyJPA.setPageVisits(historyJPA.getPageVisits() + diff);
// update datastore
addHistoryDeviceJPAToDs(dbItem);
addHistoryJPAToDs(historyJPA);
// don't check other items of j list
break outerLoop;
}
}
}
}
System.out.println("8");
}
System.out.println("9");
// http://www.sohailaziz.com/2012/06/scheduling-activities-services-and.html
// https://dev.twitter.com/docs/api/1.1
// https://developers.google.com/appengine/docs/java/datastore/jdo/creatinggettinganddeletingdata?csw=1#Updating_an_Object
// http://en.wikibooks.org/wiki/Java_Persistence/Inheritance
}
and 6 is here:
private void addHistoryDeviceJPAToDs(List<HistoryDeviceJPA> list) {
System.out.println("6");
EntityManager em = EMF.get().createEntityManager();
try {
for (int i=0; i<list.size(); i++) {
System.out.println("=> " + i + " - " + list.get(i).toString());
em.getTransaction().begin();
em.persist(list.get(i));
em.getTransaction().commit();
}
} finally {
em.close();
}
}
after debug I found the problem is in this line:
List<HistoryDeviceJPA> dbList = (List<HistoryDeviceJPA>) q.getResultList();
if(dbList == null)
addHistoryDeviceJPAToDs(historyDeviceJPAList);
'dbList' is never null and it's size is 0 if there is nothing in datastore. That's why addHistoryDeviceJPAToDs method never invoked. By changing the code to following problem solved and local db created.
List<HistoryDeviceJPA> dbList = (List<HistoryDeviceJPA>) q.getResultList();
if(dbList == null)
return;
System.out.println("5");
// If there is no result (shows there is no record for that device)
if(dbList.size() == 0)
addHistoryDeviceJPAToDs(historyDeviceJPAList);
For other people who come across the same issue --
GAE will not create local_db.bin until you put data in the datastore. So if the file is not there, there is likely a bug in the application code.

Resources