JavaFX and Couchbase Mobile is a great combo for creating desktop class applications with rich user interfaces and fast data access. In this tutorial, you will learn the following core concepts:
- Learn about replication, security and deployment using the TodoLite sample
- How to start a new JavaFX project with Couchbase Mobile as a dependency
We won’t cover TodoLite for Android in this post but you could have both applications running side by side and replicating documents and attachments via Sync Gateway:
You can find the source code of TodoLite JavaFX on github.
TodoLite JavaFX
To speed things up, you will clone some source code with the following:
1 2 |
git 복제 git@github.com:CouchbaseTutorials/todolite-javafx.git cd todolite-javafx |
Open this folder in IntelliJ and click the 실행 button in the upper right corner. A new window should open (of course your’s won’t have any data in it :D):
Go ahead and create a new List and Task. At the time of this writing, the JavaFX version of TodoLite doesn’t implement all of the features. Right now, you’re logged in as the user with name wayne and the password is pass. So if you run the TodoLite Android version, you can login with those credentials or create a new user on the Sign Up page and share Lists with other Users. Both applications would be syncing to a Sync Gateway instance running at http://9cec7a6e-jamiltz.node.tutum.io:4984.
데이터 모델
TodoLite has the following data model. Notice how documents are kept small in size and can reference other documents’ _id field (you could think of them as foreign keys):
배포
The back-end for TodoLite is composed of Sync Gateway and an App Server to allow users to Sign Up. Again, at the time of this writing, the Login and Sign Up screens have not been implemented for the JavaFX version of TodoLite. You can follow the README in the todolite-development repository to get both servers running locally.
How-To Creating a new JavaFX + Couchbase Mobile application
Open IntelliJ IDEA and choose the Create New Project menu. On the left pane, select the JavaFX application template and set the Project SDK to 1.8. Name the application as you wish and click 완료, this will open your newly created project in a new window. Before we begin writing code, there are a couple of configuration settings to change. Select the Edit Configurations… menu in the top right corner which will open a new window. Check the Single instance only box to ensure that the IDE doesn’t start a new instance of the application every time you click the Run button.
With that, let’s turn our attention to adding Couchbase Lite as a dependency to the project. Select File\Project Structure… from the top menu bar, a new window will open and on the Modules tab, add a new Library from Maven:
A search field will appear in a popup window, type com.couchbase.lite:couchbase-lite-java:1.1.0 를 클릭하고 확인. This will download the library and add it to the project. Next, you will add a JAR file that contains the native library for the platform you’re running the application on (Windows, Mac…). For OS X users, download this JAR file and add it in a new directory called libraries in your project. Return the Modules window and add this JAR file from the Add > JARs or directories… menu:
클릭 확인 and run the application. You should see a blank window with the message 헬로 월드 in the status bar:
Open a new Database and save a Document
In your new project, open Main.java and create a new method called startCouchbase
를 다음과 같이 설정합니다:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
비공개 void startCouchbase() { 자바 컨텍스트 컨텍스트 = new 자바 컨텍스트(); 관리자 관리자 = null; 시도 { 관리자 = new 관리자(컨텍스트, 관리자.기본_옵션); } catch (IOException e) { e.프린트스택트레이스(); } 데이터베이스 데이터베이스 = null; 시도 { 데이터베이스 = 관리자.getDatabase("myapp"); } catch (카우치베이스 라이트 예외 e) { e.프린트스택트레이스(); } 지도<문자열, 개체> 속성 = new 해시맵<문자열, 개체>(); 속성.put("세션", "Couchbase Mobile"); 속성.put("conference", "JavaOne"); 문서 문서 = 데이터베이스.createDocument(); 시도 { 문서.putProperties(속성); } catch (카우치베이스 라이트 예외 e) { e.프린트스택트레이스(); } } |
Here, you’re opening a new database called myapp and persisting a new document with a session and conference field. You can read more about the different APIs in the documentation guides or refer to the TodoLite-JavaFX sample app.
전화로 startCouchbase
메서드의 시작
method of Main.java
동기화 게이트웨이
The quickiest way to get started with replication is to download the latest version of Sync Gateway:
http://www.couchbase.com/nosql-databases/downloads#Couchbase_Mobile
And use one of the provided configuration template as the command line argument when starting Sync Gateway:
1 |
$ ~/다운로드/카우치베이스-동기화-게이트웨이/bin/동기화 게이트웨이 기본-바다코끼리-버킷.json |
The specified configuration file has the following:
1 2 3 4 5 6 7 8 9 |
{ "log": ["*"], "데이터베이스": { "db": { "서버": "월러스:", "users": { "GUEST": { "disabled": false, "admin_channels": ["*"] } } } } } |
Here, you’re creating a database called db and enable the 게스트 which means that unauthenticated requests should be allowed and processed.
That’s it! You now have a Sync Gateway database running on your machine that can be reached at http://localhost:4984/db/.
Adding Sync
Head back to the JavaFX application and add a new 시작복제
메서드를 사용합니다:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public void 시작복제() { URL remoteURL = null; 시도 { remoteURL = new URL(원격 데이터베이스); } catch (MalformedURLException e) { e.프린트스택트레이스(); } 복제 pull = 데이터베이스.createPullReplication(remoteURL); 복제 push = 데이터베이스.createPushReplication(remoteURL); pull.setContinuous(true); push.setContinuous(true); pull.시작(); push.시작(); } |
전화로 시작복제
below the startCouchbase
method and restart the application. You should now see the document you created previously in the Sync Gateway Admin UI (http://localhost:4985/_admin/).
다음 단계로 이동
Congratulations! You’ve built your first JavaFX + Couchbase Mobile application with replication. You’re now ready to learn the following concepts:
- 그리고 동기화 기능 to define access rules
- 카우치베이스 라이트 뷰 를 사용하여 사용자 지정 쿼리를 작성합니다.
아래 댓글이나 포럼에서 여러분의 의견과 결과를 공유하거나 궁금한 점이 있으면 언제든지 질문해 주세요. 곧 연락드리겠습니다!