-
Notifications
You must be signed in to change notification settings - Fork 3
Running an advanced Java EE application
In this page we will introduce an advanced version of Simple project showed here using the Eclipse IDE.
Clone the project from the "Dat250TweetAdvanced" repository.
In our Eclipse we need to import the project. File / import / Existing project into Workspace and launch the project. We do not need to configure GlassFish again since we already did it for the sample case.
This advanced example contains new features, and some need to be previously configured:
-
The Database will store now the content (message), the topic and the author of a Tweet.
-
It has been created a "Tweets" class that will manage a List of Tweets.
-
It has been implemented a login page. This one is not real in the sense that there is not user credentials checking. The password field has not any format checking neither. The login has been implemented just to simulate a session. When the user "log in" the session will keep the username and password until the "logout" button is pressed. The username is used to identify the sender of the Tweet.
-
We have introduced two REST (GET) operations to show how it works. One for retrieve all tweets and the other to get one tweet (asked by id).
-
There has been implemented a Java Message Service based on Topic. Apart from adding the tweets to the database, if the tweet is published in a topic called "dweet", it will be processed and sent (POST operation) to the Dweet platform. We will see further how to use it and check our tweets.
Start the server from the Servers tab in Eclipse.
To be able to use JMS, we need first to create two resources in our GlassFish control panel:
-
Connection Factory: Clients use connection factories to create the connections to the JMS provider.
-
Destination Resource: Object (tipically a Queue or a Topic) to which the messages are addressed and sent, and from where the messages are received.
In our browser navigate to http://localhost:4848/common/index.jsf to open it (NOTE: Glassfish need to be up to be able to access to such panel).
First, go to "Resources" > "JMS Resources" > "Connection Factories" and press "New...". Picture below shows the configuration properties to be set. Click OK after it.
Second, in "Resources" > "JMS Resources" > "Destination Resources" press "New...". Configure it with the configuration provided in Picture below.
Restart the server to apply the new configuration.
Before running the application we will configure the Dweet thing name where each one will publish when using the "dweet" topic.
In the source code of the project, go to src/main/java/jms/ and open DweetConnection.java
The "thingName" attribute is where the content will be published. Choose a name, change the attribute and save (Note that publications are public, so if more than one people publishes in the same thing it will affect the rest). For the example I will use "dat250" as name.
With this properties configured, the application should be ready to be deployed. With the server started, add the project to the server with the "Add and Remove..." option that appears right clicking in the server.
In the browser navigate to the project (use the http://localhost:8080/Dat250TweetAdvanced/ url. NOTE: Go always through this URL instead of going straight to the http://localhost:8080/Dat250TweetAdvanced/index.html page. It is necessary to pass the login page first).
Introduce the username and password to go to the tweets panel. It is slightly different to the Simple version. Now the Author is recognised from the session and the fields we can fill are Message and Topic".
Introduce a first tweet with the next information and send it:
Message: First Tweet.
Topic: firsttopic.
Now introduce a second tweet with the next information and send the tweet.
Message: Second Tweet.
Topic: dweet.
If we navigate to the Console in Eclipse there should be some message similar to:
Information: Sending tweet to dweet...
Information: {"User":"Alex","Message":"Second Tweet"}
Information: {"this":"succeeded","by":"dweeting","the":"dweet","with":"thing":"dat250","created":"2018-06-04T15:29:03.157Z","content":{"User":"Alex","Message":"Second Tweet"},"transaction":"bca0fd34-60c8-4bec-aaed-ff63baaf3670"}}
Information: Sending tweet to dweet...
To verify that the tweet was truly sent to the dweet platform, navigate to https://dweet.io:443/get/dweets/for/dat250 in the browser. Remember to change "dat250" with the thing name provided in the DweetConnection class. Picture below shows how the result should look like.
As it was mentioned before, we have created two REST GET operations. The root path for every REST operation has been included in the web.xml file in src/main/webapp/WEB-INF/ directory. It can be found in the line:
<url-pattern>/rest/*</url-pattern>
The two GET operations have been defined in the TestRest.java class in src/main/java/rest.
To see the results in the browser we just need to navigate to:
http://localhost:8080/Dat250TweetAdvanced/rest/tweets for all tweets.
Result:
http://localhost:8080/Dat250TweetAdvanced/rest/tweets/2 to get the tweet with ID = 2.
Result:
NOTE: To test the REST operations is not demanded to go through the login, only for the use of the application.