-
Notifications
You must be signed in to change notification settings - Fork 3
Running an advanced Java EE application
We consider an advanced version of the earlier simple tweet application project here using the Eclipse IDE.
Start by importing the project into Eclipse using File > Import ... > Existing project into Workspace. We do not need to configure the GlassFish server again since we already did it for the simple variant of the application.
This advanced example contains additional features
-
The database will store now the content (message), the topic, and the author of a Tweet.
-
A
Tweets
class has been created which is used to manage a list of tweets. -
A login page has been added.
The login page is not real in the sense that there is no 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 the Tweets.
-
Two REST (GET) operations has been implemented proving a simple web service for the application. One for retrieving all tweets and one to get a single tweet (identified by an identifier).
-
A topic relying on the Java Message Service based has been added. In addition, to storing tweets in the database, the tweets are published to a topic called "dweet" which will be processed and sent (POST operation) to the Dweet platform.
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: Administrated object (typically a Queue or a Topic) to which the messages are addressed and sent, and from where the messages are received.
In a browser, navigate to http://localhost:4848/common/index.jsf to open it (NOTE: The GlassFish server needs to be up and running to access the administration panel).
- Go to Resources > JMS Resources > Connection Factories and press New.... The figure below shows how to configure the properties of the connection factory. Click OK.
- Go to Resources > JMS Resources > Destination Resources press New.... Configure the JMS topic according to the properties shown in the figure below.
Restart the GlassFish server to apply the new configuration.
The JMS resources can also be created via the asadmin commandline interface Using
asadmin create-jms-resource --restype javax.jms.ConnectionFactory --description "Advanced Tweet app" jms/dat250/ConnectionFactory
asadmin create-jms-resource --restype javax.jms.Topic --property Name=dat250Topic jms/dat250/Topic
Before running the application we will configure the Dweet thing name where tweets will be published via the dweet JMS topic.
-
Go to
src/main/java/jms/
and openDweetConnection.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 person publishes in the same thing, it will affect the rest). I For the example I will use "dat250" as name.
-
With the properties configured, the application is ready to be deployed. With the server started, add the project to the server with the Add and Remove... option that appears by 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).
-
Login by providing a username and password to go to the tweets panel. It is slightly different compared to the simple version. Now the Author is obtained from the session, and the fields that can be filled in 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 Server.log file in the GlassFish folder, 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...
Verify that the tweet was indeed sent to the dweet platform by navigating to https://dweet.io:443/get/dweets/for/dat250 or to https://dweet.io/follow/dat250 in the browser. Remember to replace dat250 with the thing name provided in the DweetConnection class. The figure below shows what the output should look like.
The application provides a simple REST web service that can be accessed via HTTP GET operations.
The root path for every REST operation is specified 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 RestService.java
class in src/main/java/rest
.
To access the REST web service, navigate to http://localhost:8080/Dat250TweetAdvanced/rest/tweets for all tweets.
which should give a result similar to
and http://localhost:8080/Dat250TweetAdvanced/rest/tweets/2 to get the tweet with identifier 2.
which should give a result similar to