Monday, September 23, 2013

Bluetooth 4 LE Mobile Rest client on iOS 7/Android 4 with abackendJAX-RS JPA 2 Java cloud service on Oracle WebLogic 12c

Bluetooth 4 LE Mobile Rest client on iOS 7/Android 4.3 with a backend JAX-RS JPA 2 Java cloud service on Oracle WebLogic 12c

The following post details my ongoing work in getting a mobile client [iOS 7 enabled iPhone 4s] to upload external biometric data from a Bluetooth 4.0 LE device such as the MIO watch or the Wahoo chest strap to a cloud based [Oracle WebLogic 12c] service.  I will also upload all available device data such as acceleration, rotation, gravity, light, humidity, temperature, magnetic fields, longitude/latitude, heading and altitude.

JAX-RS Testing

Here is a nearly ready for Apple submission - iPhone 5 based prototype that reads from two heart rate monitors simultaneously.  The data is uploaded real time to my Oracle Cloud data center where the two rates are correlated and used to eliminate heart rate spikes specific to a single device - for redundancy




BOM (Bill of Materials): 

- A Bluetooth 4.0 LE/Smart compatible device like the Apple iPhone 4s, 5 or 5c or the iPad mini 2nd gen
   or an Android 4.3+ device like the Samsung Galaxy S4 or Google Nexus 5
   or a BlackBerry 10 device like the Q10 or Z10
- a biometric device that emits low power Bluetooth 4.0 data like the Kickstarter LED pulse watch - MIO Alpha or more reliable wahoo chest strap which conforms to the Bluetooth Heart Rate Profile
- a cloud based data center service like Oracle WebLogic 12c or Cloudbees or even GAE that includes both a Java EE (preferably JPA 2 based backend with integrated Database like Oracle Database 11g.
- a local development version of Oracle WebLogic 12 with integrated OEPE Eclipse Kepler Java EE IDE
http://www.oracle.com/technetwork/middleware/weblogic/downloads/wls-main-097127.html
oepe-12.1.2.1-kepler-installer-win32.exe


Features

F1: Device independence or coverage (Android 4.3+ or Android 4.2.2 (Samsung), Blackberry 10, iOS 7 and optionally PhoneGap3/Webkit
F2: Sensor capture (device local) of positional, magnetic, acceleration, gravitational, rotational, light, pressure, humidity, sound data
F3: Sensor capture (device remote) of Bluetooth 4 LE (Smart) biometric data
F3.1: Multiple remote BT 4.0 capture (IE: Mio Alpha watch + Wahoo chest)
F4: Sensor display (device local and remote web monitoring)
F5: Sensor persistence (device local and online/offline remote data center)

Requirements

R1: Mobile sensor data capture
R2: Mobile data upload to data center
R3: Mobile data viewing
R4: Realtime local device based data analysis
R5: Realtime web based data analysis
R6: Mobile based data storage/caching
R7: Volumetrics: Time and Space performance analysis (best,average,worst)
R8: Resiliency: handle network timeout, unavailability through caching and concurrency
R9:

Architecture

Front end clients


Back end as a service (BaaS)


Development

We will be using the Core Bluetooth API introduced in iOS5 but targeting iOS7 just released in Sept 2013.
We will also be using Android 4.2.2 with the proprietary Samsung BLE API until I get get Android 4.3 on it containing generic BLE support.

Design Issues


DI1:  Handling network outages

DI2: Database for Backend
We will be using postgreSQL as the database backend.




Experimental Results

UUID, characteristics and data for the MIO Alpha bluetooth watch.

2013-09-26 21:48:54.395 Biometric[289:60b] CoreBluetooth[WARNING] <CBCentralManager: 0x16e994f0> is disabling duplicate filtering, but is using the default queue (main thread) for delegate events
2013-09-26 21:48:54.795 Biometric[289:60b] Connecting to peripheral <CBPeripheral: 0x16db2580 identifier = 398D853C-FE6D-A669-0E72-4A19D103CF0D, Name = "MIO GLOBAL", state = disconnected>
2013-09-26 21:48:55.501 Biometric[289:60b] Service found with UUID: Unknown (<6c721826 5bf14f64 9170381c 08ec57ee>)
2013-09-26 21:48:55.503 Biometric[289:60b] Service found with UUID: Device Information
2013-09-26 21:48:55.508 Biometric[289:60b] Service found with UUID: Unknown (<180d>)
2013-09-26 21:48:55.515 Biometric[289:60b] Discovered characteristic <CBCharacteristic: 0x16dc8020> UUID: Unknown (<6c722a0a 5bf14f64 9170381c 08ec57ee>)
2013-09-26 21:48:55.520 Biometric[289:60b] Discovered characteristic <CBCharacteristic: 0x16dd0850> UUID: Unknown (<6c722a0b 5bf14f64 9170381c 08ec57ee>)
2013-09-26 21:48:55.524 Biometric[289:60b] Discovered characteristic <CBCharacteristic: 0x16dc0460> UUID: Serial Number String
2013-09-26 21:48:55.527 Biometric[289:60b] Discovered characteristic <CBCharacteristic: 0x16ddb060> UUID: Manufacturer Name String
2013-09-26 21:48:55.530 Biometric[289:60b] Discovered characteristic <CBCharacteristic: 0x16dd23f0> UUID: System ID
2013-09-26 21:48:55.533 Biometric[289:60b] Discovered characteristic <CBCharacteristic: 0x16dd0df0> UUID: Firmware Revision String
2013-09-26 21:48:55.536 Biometric[289:60b] Discovered characteristic <CBCharacteristic: 0x16dca2b0> UUID: Hardware Revision String
2013-09-26 21:48:55.539 Biometric[289:60b] Discovered characteristic <CBCharacteristic: 0x16dd1280> UUID: Model Number String
2013-09-26 21:48:55.543 Biometric[289:60b] Discovered characteristic <CBCharacteristic: 0x16db5300> UUID: Software Revision String
2013-09-26 21:48:55.547 Biometric[289:60b] Discovered characteristic <CBCharacteristic: 0x16ea3f70> UUID: Unknown (<2a37>)
2013-09-26 21:48:55.551 Biometric[289:60b] Discovered characteristic <CBCharacteristic: 0x16ea3c70> UUID: Unknown (<2a38>)
2013-09-26 21:48:55.557 Biometric[289:60b] size: 4 value:<04030201>
2013-09-26 21:48:55.617 Biometric[289:60b] size: 4 value:<00000002
2013-09-26 21:48:55.677 Biometric[289:60b] size: 8 value:<30303030 46333344> 0000F33D
2013-09-26 21:48:55.737 Biometric[289:60b] size: 10 value:<4d494f20 474c4f42 414c> MIO GLOBAL
2013-09-26 21:48:55.797 Biometric[289:60b] size: 10 value:<3df30000 d86433d9 c9e9> (null)
2013-09-26 21:48:55.857 Biometric[289:60b] size: 5 value:<30322e30 33> 02.03
2013-09-26 21:48:55.917 Biometric[289:60b] size: 5 value:<30333233 38> 03238
2013-09-26 21:48:55.977 Biometric[289:60b] size: 10 value:<414c5048 41203533 5000> ALPHA 53P
2013-09-26 21:48:56.037 Biometric[289:60b] size: 8 value:<30312e30 392e3035> 01.09.05
2013-09-26 21:48:56.097 Biometric[289:60b] size: 4 value:<10367204> 6r

2013-09-26 21:48:56.157 Biometric[289:60b] size: 1 value:<02> 

Found the heart rate characteristic via the BLE specification site
https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
It turns out the rate is encoded in the 2A37 characteristic


After fixing my callback I now see the embedded heart rate value in 2A37

2013-10-01 15:42:42.929 Biometric[536:60b] size: 4 value:<104b3303> K3 75
2013-10-01 15:42:43.926 Biometric[536:60b] size: 4 value:<104c2803> L( 76
2013-10-01 15:42:44.924 Biometric[536:60b] size: 4 value:<104d1e03> M 77
2013-10-01 15:42:45.921 Biometric[536:60b] size: 6 value:<104d1e03 1e03> M 77
2013-10-01 15:42:46.919 Biometric[536:60b] size: 4 value:<104d1e03> M 77
2013-10-01 15:42:47.916 Biometric[536:60b] size: 4 value:<104d1e03> M 77
2013-10-01 15:42:48.913 Biometric[536:60b] size: 6 value:<104d1e03 1e03> M 77
2013-10-01 15:42:49.911 Biometric[536:60b] size: 4 value:<104c2803> L( 76
2013-10-01 15:42:50.909 Biometric[536:60b] size: 4 value:<104c2803> L( 76

Setup

Android 4.2.2/4.3 on Samsung Galaxy S4 - deprecated

2014 Jan (use Android 4.4 on a Google Nexus instead)
Start with the blog entry below to enable your phone for development
http://stackoverflow.com/questions/16256048/eclipse-android-sdk-cannot-see-new-samsung-galaxy-s4-device-via-a-usb-connection
http://www.samsung.com/us/support/owners/product/SGH-I747MBBATT
You don't need KIES
http://www.samsung.com/in/support/usefulsoftware/KIES/JSP
http://www.androidcentral.com/how-get-developer-settings-galaxy-s4

study the API for Samsung BLE until Android 4.3 is enabled in Feb 2014 with standardized LE 4.0
http://developer.samsung.com/ble

References 

1) iOS6 Programming: Pushing the Limits, Rob Napier and Mugunth Kumar 2013, CH14: REST for the Weary, p.243
2) Bluetooth Smart Devices Technology website
3) https://developer.bluetooth.org/Pages/default.aspx
4) http://weblog.invasivecode.com/post/39707371281/core-bluetooth-for-ios-6-core-bluetooth-was
5) http://oleb.net/blog/2013/04/starting-bluetooth-low-energy-development-ios/
6) http://adcdownload.apple.com//wwdc_2012/wwdc_2012_session_pdfs/session_705__advanced_core_bluetooth.pdf
7)
Pro JPA 2.1 2nd edition by Mike Keith and Merrick Schincariol available at Apress, Amazon or partially at Google Books.

log

20131026: IOS 7 upload of heart rate via NSURLConnection

The IOS class NSURLConnection works about the same as the Java/Android HttpUrlConnection class and if easy to use.  I hardcoded the url and appended the heart rate from the 2a37 characteristic to test.

- (void) httpPushToDataCenter { NSMutableString *url = [[NSMutableString alloc ]init ]; [url appendString: cloudURLString]; [url appendString: @"&u=201310272&pr=ios"]; [url appendString: @"&hr="]; NSString *rateString = [NSString stringWithFormat:@"%hu", self.heartRate]; [url appendString: rateString]; NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString: url ] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
I see the following on the Oracle data center
select IDENT_ID,TSSTOP,HEART from GPS_RECORD where userid=201310272

IDENT_IDTSSTOPHEART
1630901138280629409571
1630951138280629531971
1631001138280629633671
1631051138280629734271
1631101138280629831872
1631151138280629930771
1631201138280630031071
1631251138280630132571
1631301138280630287371
1631351138280630387172

20131027: Hiding the iOS keyboard for textviews

Apple has a bit of an issue surrounding closing the popup keyboard (Android gives you a done key).  It is the responsibility of the app to close the keyboard somehow - usually by clicking on empty app real estate or by assigning an action to another view.  Here I use the read-only text field for the heart rate to close any open keyboard.

- (IBAction)rateDown:(id)sender { [[self view] endEditing:YES]; }


20131029: Connecting to a WAHOO chest heart rate monitor

This HR monitor works well in an office/gym environment but it cuts out periodically in the field when rollerblading (unlike the MIO Alpha wrist HR monitor) - especially in temperatures down to +3 deg C.

However, my iOS 7 app picks up the monitor no problem - the issue now is I need to track both HR monitors simultaneously to get a better overall number.

2013-10-30 12:37:55.760 Biometric[806:60b] Setup
2013-10-30 12:37:55.798 Biometric[806:60b] Connecting to peripheral <CBPeripheral: 0x15557710 identifier = 4A90672B-EC3A-BEC2-5833-AD5A559DEE87, Name = "Wahoo HRM v2.1", state = disconnected>
2013-10-30 12:37:59.064 Biometric[806:60b] Connected to peripheral <CBPeripheral: 0x15557710 identifier = 4A90672B-EC3A-BEC2-5833-AD5A559DEE87, Name = "Wahoo HRM v2.1", state = connected>
2013-10-30 12:37:59.367 Biometric[806:60b] Service found with UUID: Device Information
2013-10-30 12:37:59.370 Biometric[806:60b] Service found with UUID: Unknown (<180d>)
2013-10-30 12:37:59.373 Biometric[806:60b] Service found with UUID: Battery
2013-10-30 12:37:59.375 Biometric[806:60b] Discovered characteristic <CBCharacteristic: 0x156531b0> UUID: Serial Number String
2013-10-30 12:37:59.377 Biometric[806:60b] Discovered characteristic <CBCharacteristic: 0x15647ce0> UUID: Manufacturer Name String
2013-10-30 12:37:59.378 Biometric[806:60b] Discovered characteristic <CBCharacteristic: 0x15647eb0> UUID: System ID
2013-10-30 12:37:59.380 Biometric[806:60b] Discovered characteristic <CBCharacteristic: 0x15652800> UUID: Firmware Revision String
2013-10-30 12:37:59.381 Biometric[806:60b] Discovered characteristic <CBCharacteristic: 0x156528c0> UUID: Hardware Revision String
2013-10-30 12:37:59.384 Biometric[806:60b] Discovered characteristic <CBCharacteristic: 0x15630ae0> UUID: Unknown (<2a37>)
2013-10-30 12:37:59.386 Biometric[806:60b] Discovered characteristic <CBCharacteristic: 0x15645240> UUID: Unknown (<2a38>)
2013-10-30 12:37:59.388 Biometric[806:60b] Discovered characteristic <CBCharacteristic: 0x15652ec0> UUID: Battery Level
2013-10-30 12:37:59.390 Biometric[806:60b] Discovered characteristic <CBCharacteristic: 0x15652150> UUID: Unknown (<2a1b>)
2013-10-30 12:37:59.392 Biometric[806:60b] Discovered characteristic <CBCharacteristic: 0x15652910> UUID: Unknown (<2a1a>)
2013-10-30 12:37:59.427 Biometric[806:60b] size: 6 value:<14439f03 9f03> C 67
2013-10-30 12:37:59.428 Biometric[806:60b] Sending: https://obrienscience-obrienlabs.*r?action=setGps&u=20131027&pr=ios&hr=67
2013-10-30 12:37:59.540 Biometric[806:60b] Notification began on <CBCharacteristic: 0x15630ae0>
2013-10-30 12:37:59.607 Biometric[806:60b] size: 6 value:<14439f03 9f03> C 67
2013-10-30 12:37:59.608 Biometric[806:60b] Sending: https://obrienscience-obrienlabs.*r?action=setGps&u=20131027&pr=ios&hr=67
2013-10-30 12:38:00.447 Biometric[806:60b] size: 16 value:<1442a403 a403a303 a3038b03 7c037603> B¤¤££|v 66

20131102: issues with Oracle WebLogic 12c response time on sub-second inserts using EclipseLink 2.3
I need to optimize the insert code as currently if I run 2 clients - an iOS7 app uploading heart rate data and an Android 4.2.2 app uploading everything else (all 27 data points) - both running every second - I get some network timeouts.
I also need to implement device side caching in period where the response time is reduced.
Here is an illustration of a field test while I was rolerblading - you will see some period where the Bluetooth 4 connection dropped to the Mio or Wahoo heart rate monitor.

I store and retrieve all my heart rate and other biometric data from the oracle cloud instance.
Reference rollerblade peak intervals
10 min exercise on treadmill - day 1


I had a major issue at 22 min after not running for 4 days - a 20 BPM spike from 158 to 178 and had to stop after 2 min.  I took the Mavik ACE inhibitor pill a bit earlier at 1800 - which was 2.5 hours before the run - when the full effects of the blood pressure lowering medication to kick in.  Later 30 min after the end of the run my BP was very low at 115/84/76 where it normally would be 145/105/80 before I started taking the ACE inhibitor 4 weeks ago. at 2330 I had a slight tight feeling around my heart.



In 7 seconds I went from 153 to 173 - a 20 BPM change around 22 min

1385084885943 1.002 149 151
1385084886944 1.001 151 151
1385084887904 0.96 153 152
1385084888959 1.055 156 153
1385084889914 0.955 156 153
1385084890893 0.979 156 157
1385084891867 0.974 165 160
1385084892920 1.053 172 160
1385084893928 1.008 174 168
1385084894903 0.975 174 171
1385084895941 1.038 174 171
1385084896903 0.962 174 173
1385084897928 1.025 176 174
1385084898938 1.01 178 175
1385084899897 0.959 178 176
1385084900919 1.022 178 176
1385084901924 1.005 176 176
Ms since 1970,diff, polar HR, mio HR

Connecting to two Heart Rate monitors for redundancy

The MIO Alpha watch has spikes of up to 200% where it will go from 80 BPM to 160 and return within a couple seconds.  These spikes can be filtered out in software.  There are also smaller spikes of 15-20 BPM that last up to 20 seconds that are hard to determine if they are due to the MIO HR watch itself or the wearer (an extra heartbeat confuses the multiplexed LED sensor?) - to workaround this my HR monitor/upload app can connect to two devices simultaneously - the heart rate values are both sent to the data center.
Here for example I am connected to the MIO Alpha Heart Rate watch and the Wahoo 2.1 Heart Rate chest strap.

Enabling the iOS Magnetometer in Xcode 5

You need to add "magnetometer" to the required device capabilities list in the plist file.

Adding CoreGraphics to an iOS UIView

Using color coded buttons with the current sensor value is good for a static snapshot but some historical context is required.  You add graphics capability under the standard views by subclassing UIView and assigning this subclass to your ViewController.  You must override the following callback.

- (void)drawRect:(CGRect)rect {
  CGContextRef g = UIGraphicsGetCurrentContext();
}

Adding Core Data persistence in iOS 7


Results
2014-05-25 00:24:28.809 Biometric[269:60b] <Device: 0x1780b9860> (entity: Device; id: 0xd000000000040000 <x-coredata://F02B5272-245D-4A76-94D9-3EB8EB92CD64/Device/p1> ; data: {
    id = iphone5;

})

20131129: Google Bluetooth 4.0 BLE API support in 4.3

Samsung pushed the Android 4.3- API18 update yesterday to the Galaxy S4 - therefore I can use standard Bluetooth 4.0 LE Heart Rate Profile spec code.  Since I got a Google Nexus 5 phone with Android 4.4 API19 - I now have 2 phones with spec BLE.



TODO

webservice
rest service
configurable length queue for http requests - each with a success/fail callback
option for local persistence
queue if fed by default from local persistence
refactor for key/value pair for each data point


local ui:


web ui:

Saturday, June 1, 2013

Java Cloud Providers - Review

Purpose:

I need a PaaS cloud/hosting provider for my Java EE applications so they can run as thin client applications and also as BaaS providers for mobile applications.

Google Appspot and GAE

This service is based on the DataNucleus JPA API - which runs fine

Oracle Cloud

Oracle's service is a straight PaaS offering that runs your database as a multitenant partition.  The difference is that you can be walked through the use and installation of the paid service in person.

The Java and DB instances are priced separately at a minimum combined cost of $533/month or $6400/year.
https://java-trialanzh.java.us1.oraclecloudapps.com
https://obrienscience-obrienlabs.java.us1.oraclecloudapps.com/
http://www.obrienlabs.org/opendata/FrontController?action=image

https://cloud.oracle.com/mycloud/f?p=service:database_pricing:0:::::
Database S5 $175 / Month
1 Schema Oracle Database 11g Release 2
5 GB Oracle Database Storage
30 GB Data Transfer

https://cloud.oracle.com/mycloud/f?p=service:java_pricing:0:::::
Java S1 $249 / Month
1 Oracle WebLogic Server 12.1.1+
1.5 GB RAM for Java Heap
5 GB File Storage
50 GB Data Transfer


Eclipse Integration
Get the OEPE from oracle that includes Eclipse Kepler, the OEPE plugins and WebLogic Server.
http://www.oracle.com/technetwork/developer-tools/eclipse/overview/index.html

Issues:


EMF injection requires static/dynamic weaving (the example entity is statically weaved)
http://static.springsource.org/spring/docs/3.0.0.RC3/reference/html/ch13s05.html
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Weaving/Dynamic_Weaving
http://www.oracle.com/technetwork/articles/java/jsf-2-cloud-part1-1562825.html

Questions: Oracle Cloud using WebLogic 12.1.1.0 and JPA 2.0
https://forums.oracle.com/forums/thread.jspa?messageID=11052991#11052991|Thread: Database access from Oracle Java Cloud Service application
https://forums.oracle.com/forums/message.jspa?messageID=11052937#11052937|Thread: Related Annotations
https://forums.oracle.com/forums/message.jspa?messageID=11052868#11052868|Thread: EJB 3.1 Timer @Singleton does not start on Oracle Cloud - works locally

20130608: Oracle cloud running fine except for the Java EE container managed proxy issues.
20130608: Redeploy application (#40) job get stuck for 3 days now - there is no way for the user of the Oracle Java cloud instance to stop the misbehaving server job - rendering the cloud instance effectively offline.
20130614: solved by SR - job reset after 7 days was an anomaly - it would be good to have a watchdog process or an option to kill a long running process in the future.




Administration
https://database-trialanzh.db.us1.oraclecloudapps.com/apex/f?p=4500:1001:14650370459182::NO:::
Logs:
http://docs.oracle.com/cloud/123/developer_services/CSJSU/java-admin.htm#CACBHCGJ

Procedures:

Download WebLogic 12c via OEPE or Netbeans 7.3 to test your applications locally.
Update Netbeans to be cloud aware.
https://netbeans.org/kb/docs/web/oracle-cloud.html#install-plugin

You will need the matching source of the excellent JPA provider (EclipseLink) that ships with WebLogic Cloud
http://www.eclipse.org/eclipselink/releases/2.3.0.php
http://wiki.eclipse.org/EclipseLink/Examples/JPA/Composite

Get the version of EclipseLink by doing something that causes an exception and viewing the stacktrace
Currently as of 20131204 the version of EclipseLink - the default JPA provider in Oracle Cloud 12c is SVN rev# 10243 version 2.3.1 from 20111018

Caused by: Exception [EclipseLink-8034] (Eclipse Persistence Services - 2.3.1.v20111018-r10243): org.eclipse.persistence.exceptions.JPQLException


Cloudbees 


get git - http://git-scm.com/download/win
generating keys
https://help.github.com/articles/generating-ssh-keys
https://grandcentral.cloudbees.com/#CB_clickstart=https://raw.github.com/CloudBees-community/jboss-db-example/master/clickstart.json
http://opendatalink.obrienlabs.cloudbees.net/index.jsf
https://forge.cloudbees.com/a/obrienlabs/repositories/opendatalink

Jelastic


OpenShift (RedHat)


Cloud Foundry (VMWare)


AppFog


Amazon AWS / EC2 / Elastic Beanstalk


Your own router

Rogers allows a DMZ but Bell does not.  The performance of the upstream connection is too slow though.
local


Wednesday, May 8, 2013

EclipseLink JPA on Oracle WebLogic 12c with a JAX-RS based JSF and JQuery thin client

EclipseLink JPA on Oracle WebLogic 12c with a JAX-RS based JSF and JQuery thin client


Enable JPA Metamodel Modelgen compilation in your Eclipse project

Turn on APT in your project settings and add EclipseLink jars

An automatic rebuild will show the new _ underscore JPA canonical metamodel classes that can be used for typesafe Criteria API queries.



Parsing JSON via AJAX call from JQuery client







JAX-RS backend server

@Path("/read")
public class RestServiceImpl extends Application {
 // not supported until 2.0 https://java.net/jira/browse/JERSEY-517
 //@EJB(name="ejb/ApplicationService") // not for JAX-RS
 //@Inject
 private ApplicationServiceLocal applicationServiceLocal;
   @GET
   @Path("/json/latest/{id}")  
   //@Consumes(MediaType.TEXT_PLAIN)
   @Produces(MediaType.APPLICATION_JSON)
   public List getLatestJSON(@PathParam("id")String identifier) {    
    return getApplicationService().latest(identifier);
   }
....

@Stateless(mappedName = "applicationService")
public class ApplicationService implements ApplicationServiceLocal {
 @PersistenceContext(unitName="gps", type=PersistenceContextType.TRANSACTION)
 private EntityManager entityManager;

    public List latest(String user) {
 CriteriaBuilder cb = entityManager.getCriteriaBuilder();
     CriteriaQuery query = cb.createQuery(Record.class);
     Root target = query.from(Record.class);
     SingularAttribute anAttribute = entityManager.getMetamodel()
       .entity(Record.class).getSingularAttribute("tsStop", Long.class);
     query.orderBy(cb.desc(target.get(anAttribute)));
     query.where(
       cb.equal(target.get("userId"), user));
     TypedQuery typedQuery = entityManager.createQuery(query);
     typedQuery.setMaxResults(1);
     Record result = (Record)typedQuery.getSingleResult();
     List list = new ArrayList<>();
     list.add(result);
 return list;         
    }
...

Thursday, May 2, 2013

JPA 2 Entity Caching

JPA 2 Entity Caching


Enabling Caching
- use a named query and set the cachable hint to true (how do we cache criteria API queries?)
- set the cache mode

       <shared-cache-mode>ALL</shared-cache-mode>
       <!-- shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode--><!-- override with @Cacheable(true) -->
       <!-- shared-cache-mode>DISABLE_SELECTIVE</shared-cache-mode--><!-- override with @Cacheable(false) -->
       <!-- shared-cache-mode>NONE</shared-cache-mode--><!-- do not use -->

Use the default
<property name="javax.persistence.cache.retrieveMode" value="true"/>


EclipseLink via Oracle Coherence

Hibernate via Terracotta ehCache

Alternate JGroups cache coordination
Instead of using standard JMS if you are not on a full EE application server - you can use JGroups

http://ehcache.org/documentation/replication/jgroups-replicated-caching
http://bugs.eclipse.org/282074


Remote JVisualVM Security

        permission java.util.PropertyPermission "java.rmi.server.ignoreSubClasses", "write";
        permission java.lang.RuntimePermission  "accessClassInPackage.sun.tools.jstatd";
        permission java.lang.RuntimePermission  "accessClassInPackage.sun.jvmstat.*";
        permission java.lang.RuntimePermission  "accessClassInPackage.sun.management";
        permission java.lang.RuntimePermission  "sun.misc.Perf.getPerf";
        permission java.util.PropertyPermission "sun.jvmstat.monitor.package","read";
        permission java.util.PropertyPermission "sun.jvmstat.*", "read";
        permission java.util.RuntimePermission  "sun.jvmstat.*";
        permission java.util.PropertyPermission "java.io.tmpdir", "read";
        permission java.io.FilePermission "/tmp", "read";
        permission java.io.FilePermission "/tmp/hsperfdata_weblogic/*", "read";
        permission java.io.FilePermission "/tmp/*", "read";
        permission java.io.FilePermission "/tmp/hsperfdata_root/*", "read";
        permission java.net.SocketPermission "127.0.0.1:1099", "connect,resolve";      
        permission java.net.SocketPermission "127.0.0.1:*", "accept,resolve";

Sunday, February 17, 2013

Google Web Toolkit and Google App Engine With PhoneGap for Mobile Apps

Google Web Toolkit and Google App Engine With PhoneGap for Mobile Apps

To write Java EE server side code and Java client code that deploys to the cloud and serves thin, mobile and headless clients - I find Google Web Toolkit used within Eclipse that deploys to Google App Engine using a Google Cloud SQL instance works very well.  If you go one step further and package your client HTML5/JS client code using PhoneGap for iOS and Android then you have a full end to end solution.

The following diagram illustrates various start and end points when developing thin or mobile clients.

Options for generating native, thin and hybrid mobile clients


Sequence diagram for generating HTML5 based mobile apps from Java using GWT and PhoneGap

Getting Started

I am interested in getting the JPA 2.0 provider working in Google App Engine.  Unfortunately Google does not use the open source EclipseLink JPA 2.0 provider - instead it uses the DataNucleus provider.

There are a lot of differences and constraints on the JPA 2 provider - here are some of the highlights.

You must use the Key PK wrapper - which shares implementation with JDO

import com.google.appengine.api.datastore.Key
[0] Nutrient (id=164) id Key (id=169) jdoDetachedState null jdoFlags 1 jdoStateManager JDOStateManager (id=171) name "aName" (id=178)


Software

Eclipse 3.8/4.2 with DTP, GWT
Google Web Toolkit 2.5
Vaadin for GWT - use the Eclipse Plugin
Google App Engine
PhoneGap/Cordova 2.4

Google App Services

Google Datastore using the paxos algorithm:  

Links


Sunday, February 10, 2013

Blackberry 10 Development

Blackberry Z10 Development


20140720: Update: After 14 years using RIM devices from the 957,6700,8800,9000,bold and Q10 - I have retired using blackberrys - for the first time my Q10 keyboard has become defective - I officially move to full time iPhone 5s usage with Android 4.4 on the side.  The lack of support for clinical devices such as the hexoskin, muse and Bluetooth Smart based systems is also a factor.  The iOS 8 ecosystem is also easier to use.

RIM/Blackberry has done an excellent job bringing out Blackberry OS 10 and the Z10/Q10 devices (I have both) which I like better than the iPhone 4s except for the # of apps available ofcourse.  The Q10 device however has a darker and slightly inferior screen compared to the Z10 but the keyboard is excellent.

The following blog details steps in getting up and running developing natively, using PhonGap or porting over Android apps.

Development Environment

see https://developer.blackberry.com/ as of 20130210 the 10.1 beta is out.
Start by downloading both the Native API - Cascades for and Blackberry 10
Get the API's https://developer.blackberry.com/develop/platform_choice/index.html
Android 4.2 for Blackberry http://developer.blackberry.com/android/
Get the simulators https://developer.blackberry.com/develop/simulator/index.html

Before running a native C/C++ app on the Blackberry Z10 or Q10 you need to enable development mode and download .bar certificates to the devices - do this by selecting to generate a new debug key.

PhoneGap/HTML5


Android Emulation

Get the eclipse plugin for android porting http://developer.blackberry.com/android/documentation/install_plugin_prerequisites_1895476_11.html
Currently only Android 2.3.3 (API 10) is supported on the Blackberry.
Install the eclipse plugin for Blackberry / Android development tools (using jcommon-1.0.12.jar and kxml2-2.3.0.jar) https://developer.blackberry.com/android/downloads/eclipseplugin/.
To get around Unable to read repository at https://dl-ssl.google.com/android/eclipse/plugins/com.android.ide.eclipse.ddms_21.0.1.v201212060256-543035.jar.
try the install again
To get around the error after restarting eclipse

java.lang.RuntimeException: Application "org.eclipse.ui.ide.workbench" could not be found in the registry. The applications available are: org.eclipse.cdt.codan.core.application, org.eclipse.cdt.core.GeneratePDOM, org.eclipse.equinox.app.error, org.eclipse.equinox.p2.director, org.eclipse.equinox.p2.garbagecollector.application, org.eclipse.equinox.p2.publisher.InstallPublisher, org.eclipse.equinox.p2.publisher.EclipseGenerator, org.eclipse.equinox.p2.publisher.ProductPublisher, org.eclipse.equinox.p2.publisher.FeaturesAndBundlesPublisher, org.eclipse.equinox.p2.reconciler.application, org.eclipse.equinox.p2.repository.repo2runnable, org.eclipse.equinox.p2.repository.metadataverifier, org.eclipse.equinox.p2.artifact.repository.mirrorApplication, org.eclipse.equinox.p2.metadata.repository.mirrorApplication, org.eclipse.equinox.p2.updatesite.UpdateSitePublisher, org.eclipse.equinox.p2.publisher.UpdateSitePublisher, org.eclipse.equinox.p2.publisher.CategoryPublisher, org.eclipse.ant.core.antRunner, org.eclipse.help.base.infocenterApplication, org.eclipse.help.base.helpApplication, org.eclipse.help.base.indexTool, org.eclipse.jdt.apt.core.aptBuild, org.eclipse.jdt.core.JavaCodeFormatter, org.eclipse.jdt.core.JavaIndexer.




Total Pageviews

Followers