<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Wilton Costa on Medium]]></title>
        <description><![CDATA[Stories by Wilton Costa on Medium]]></description>
        <link>https://medium.com/@wiltongomesjr?source=rss-10443fd257a1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*__fvxGBFfIRPryhRDxqaoQ.jpeg</url>
            <title>Stories by Wilton Costa on Medium</title>
            <link>https://medium.com/@wiltongomesjr?source=rss-10443fd257a1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 01:47:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@wiltongomesjr/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[AWS Elastic Beanstalk — Setting up a Mysql datasource with Glassfish preconfigured docker]]></title>
            <link>https://itnext.io/aws-elastic-beanstalk-setting-up-a-mysql-datasource-with-glassfish-preconfigured-docker-d8d91cdcfe62?source=rss-10443fd257a1------2</link>
            <guid isPermaLink="false">https://medium.com/p/d8d91cdcfe62</guid>
            <category><![CDATA[glassfish]]></category>
            <category><![CDATA[elastic-beanstalk]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Wilton Costa]]></dc:creator>
            <pubDate>Mon, 22 Apr 2019 23:35:19 GMT</pubDate>
            <atom:updated>2019-04-26T16:05:26.701Z</atom:updated>
            <content:encoded><![CDATA[<h3>AWS Elastic Beanstalk — Setting up a Mysql datasource with Glassfish preconfigured docker</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t9pQvj1UAqt8CAeMpsmjmw.jpeg" /></figure><p>If you are willing to deploy your java web application to AWS without having to deal with infrastructure configuration you will probably think about using Elastic Beanstalk to achieve this. Amazon Elastic Beanstalk service offers two pre-configured java web application platforms to choose, one that comes with Tomcat and one that comes with Glassfish. If you want to go with Glassfish this article may be useful for you.</p><p>The Glassfish platform is setup using docker, so, if you need to do further configurations on your enviroment like adding a JDBC connection a Dockerfile will be necessary to customize the base image. That said, let’s start showing up the steps needed to setup a Mysql datasource on your Glassfish Elastic Beanstalk platform.</p><p>Basically, what we are gonna do here is write a Dockerfile using the commands below:</p><pre># Use the AWS Elastic Beanstalk Glassfish image<br>FROM        amazon/aws-eb-glassfish:5.0-al-onbuild-2.11.1</pre><pre># Exposes port 8080<br>EXPOSE      8080</pre><pre># Install Datasource dependencies and configure datasource in Glassfish<br>RUN         curl -L -o /usr/local/glassfish5/glassfish/domains/domain1/lib/mysql-connector-java-8.0.13.jar <a href="http://central.maven.org/maven2/mysql/mysql-connector-java/8.0.13/mysql-connector-java-8.0.13.jar">http://central.maven.org/maven2/mysql/mysql-connector-java/8.0.13/mysql-connector-java-8.0.13.jar</a> &amp;&amp; \<br>            asadmin start-domain domain1 &amp;&amp; \<br>            asadmin create-jdbc-connection-pool --datasourceclassname com.mysql.cj.jdbc.MysqlDataSource --restype javax.sql.DataSource --property user=your_username:password=your_password:serverName=your_server_name:portNumber=3306:databaseName=ebdb:useSSL=false MysqlConnPool &amp;&amp; \<br>            asadmin create-jdbc-resource --connectionpoolid MysqlConnPool jdbc/your_jdni</pre><p>I will try to explain each section fo the file.</p><p>First, we have a FROM section which is required and defines the base image that will be used to build the customized docker container image. You can find the base image name of the platform you are using here: h<a href="https://docs.aws.amazon.com/elasticbeanstalk/latest/platforms/platforms-supported.html#platforms-supported.dockerpreconfig">ttps://docs.aws.amazon.com/elasticbeanstalk/latest/platforms/platforms-supported.html#platforms-supported.dockerpreconfig</a>.</p><p>For this example, we are using “amazon/aws-eb-glassfish:5.0-al-onbuild-2.11.1&quot; image.</p><pre># Use the AWS Elastic Beanstalk Glassfish image<br>FROM        amazon/aws-eb-glassfish:5.0-al-onbuild-2.11.1</pre><p>After that, we have the EXPOSE section that contains one or more port numbers. This port is used by Elastic Beanstalk to estabilish a connection between the host and the docker container, so public internet requests can be routed. For Glassfish, we need to use 8080.</p><pre># Exposes port 8080<br>EXPOSE      8080</pre><p>And finally, we have the most important part for what we want to do in this article. The RUN section. Here we can issue commands to customize our preconfigured image as desired. Note that each command needs to be separated by the string “&amp;&amp; \”.</p><pre># Install Datasource dependencies and configure datasource in Glassfish<br>RUN         curl -L -o /usr/local/glassfish5/glassfish/domains/domain1/lib/mysql-connector-java-8.0.13.jar <a href="http://central.maven.org/maven2/mysql/mysql-connector-java/8.0.13/mysql-connector-java-8.0.13.jar">http://central.maven.org/maven2/mysql/mysql-connector-java/8.0.13/mysql-connector-java-8.0.13.jar</a> &amp;&amp; \<br>            asadmin start-domain domain1 &amp;&amp; \<br>            asadmin create-jdbc-connection-pool --datasourceclassname com.mysql.cj.jdbc.MysqlDataSource --restype javax.sql.DataSource --property user=your_username:password=your_password:serverName=your_server_name:portNumber=3306:databaseName=ebdb:useSSL=false MysqlConnPool &amp;&amp; \<br>            asadmin create-jdbc-resource --connectionpoolid MysqlConnPool jdbc/your_jdni</pre><p>Let’s see what the Docker file is doing command by command.</p><pre>curl -L -o /usr/local/glassfish5/glassfish/domains/domain1/lib/mysql-connector-java-8.0.13.jar <a href="http://central.maven.org/maven2/mysql/mysql-connector-java/8.0.13/mysql-connector-java-8.0.13.jar">http://central.maven.org/maven2/mysql/mysql-connector-java/8.0.13/mysql-connector-java-8.0.13.jar</a></pre><p>In the snippet above we are using curl to download the mysql jdbc connector from maven repository to Glassfish lib folder. We will need it to setup our datasource.</p><pre>asadmin start-domain domain1</pre><p>After the lib download we call asadmin, the glassfish command line tool, using start-domain to start the default domain configured by the Elastic Beanstalk image which is called “domain1”.</p><p>In the command below we create a jdbc connection pool inside glassfish for the application using create-jdbc-connection-pool command. We need to pass some parameters here:</p><ul><li>datasourceclassname: The class name for a datasource inside Mysql lib. In this case “com.mysql.cj.jdbc.MysqlDataSource”</li><li>restype: The connection pool resource type. In this case “javax.sql.DataSource”</li><li>property: The database specific properties like user, password, portNumber, databaseName. In my case I needed to disable SSL with useSSL=false. This is optional. All properties are separated by colon.</li></ul><p>At the end of the command we provide the connection pool name. Here the name was set to “MysqlConnPool”.</p><pre>asadmin create-jdbc-connection-pool --datasourceclassname com.mysql.cj.jdbc.MysqlDataSource --restype javax.sql.DataSource --property user=your_username:password=your_password:serverName=your_server_name:portNumber=3306:databaseName=ebdb:useSSL=false MysqlConnPool</pre><p>To finish the file content we have the create-jdbc-resource command, used to configure a JNDI resource associated with the connection pool that was created before through the parameter connectionpoolid.</p><pre>asadmin create-jdbc-resource --connectionpoolid MysqlConnPool jdbc/your_jdni</pre><p>In the last part of the command we give the resource a name. In this case “jdbc/your_jdni”.</p><p>At last, with the Dockerfile ready all you have to do is name it “Dockerfile” and package it within your war file at the top level of its directory structure. If you did everything ok, when you deploy the war file to the server the configuration will take place.</p><p>Thank’s for reading. :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d8d91cdcfe62" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/aws-elastic-beanstalk-setting-up-a-mysql-datasource-with-glassfish-preconfigured-docker-d8d91cdcfe62">AWS Elastic Beanstalk — Setting up a Mysql datasource with Glassfish preconfigured docker</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Room persistence library — Using MutableLiveData observable  to update the UI after a database…]]></title>
            <link>https://itnext.io/room-persistence-library-using-mutablelivedata-observable-to-update-the-ui-after-a-database-6836d25e8267?source=rss-10443fd257a1------2</link>
            <guid isPermaLink="false">https://medium.com/p/6836d25e8267</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[livedata]]></category>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[viewmodel]]></category>
            <category><![CDATA[room]]></category>
            <dc:creator><![CDATA[Wilton Costa]]></dc:creator>
            <pubDate>Thu, 18 Apr 2019 19:26:58 GMT</pubDate>
            <atom:updated>2020-01-17T15:12:21.979Z</atom:updated>
            <content:encoded><![CDATA[<h3>Room persistence library — Using MutableLiveData observable to update the UI after a database operation</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LFiJenS3MWyUQ6zElDdv9Q.jpeg" /></figure><p>Months ago I wrote an article about how you could update the UI after a room background database task using delegate. However, although this approach works, it could lead to some problems related to activity lifecycle and memory management. So, after receiving some feedbacks I decided to write about a better way to achieve the same result using ViewModel and MutableLiveData. There’s other options like using rxjava and new alpha and beta room releases features, but I will use only the core Android features and room stable version for this example.</p><p>If you are using the room persistence library to handle Sqlite database operations on your android app, you should probably know that the library forces the developer to do all its db calls inside a background thread. So, if you are using the DAO pattern and wants to keep database logic, including the asynchronous executions encapsulated in your DAO classes, you will need to design them in a way that they can notify the calling activity after the background process finishes. After that, UI updates can take place on the main thread.</p><p>Now, I will show you how I could achieve this by making using of ViewModel and MutableLiveData. For this example, we will need to write 5 classes and 1 interface and I will try to explain the code of the most important portions. Explaining basic Room concepts is not the objective of this article and we are going to focus on the main theme here. Before we can go ahead, I want to make clear that I’m not trying to reinvent the wheel. Some concepts used in this example are well known and I’m just put things together to do what we need. So, let’s go.</p><p>First, let’s create a simple class for the model we are using in this example.</p><pre>import android.arch.persistence.room.Entity;<br>import android.arch.persistence.room.PrimaryKey;</pre><pre><a href="http://twitter.com/Entity">@Entity</a><br>public class Person {</pre><pre><a href="http://twitter.com/PrimaryKey">@PrimaryKey</a>(autoGenerate = true)<br>    private int id;<br>    private String name;</pre><pre>public int getId() {<br>        return id;<br>    }</pre><pre>public void setId(int id) {<br>        this.id = id;<br>    }</pre><pre>public String getName() {<br>        return name;<br>    }</pre><pre>public void setName(String name) {<br>        this.name = name;<br>    }</pre><pre>}</pre><p>Now, we are gonna need a Dao interface to define the DB basic operations.</p><pre>import android.arch.lifecycle.LiveData;<br>import android.arch.persistence.room.Dao;<br>import android.arch.persistence.room.Insert;<br>import android.arch.persistence.room.Query;<br><br>import java.util.List;<br><br>@Dao<br>public interface PersonDao {<br><br>    @Insert<br>    void insert(Person person);<br><br>    @Query(&quot;select * from Person order by name&quot;)<br>    LiveData&lt;List&lt;Person&gt;&gt; getAllPersons();<br><br>}</pre><p>After that, let’s write the database class.</p><pre>import android.arch.persistence.room.Room;<br>import android.arch.persistence.room.RoomDatabase;<br>import android.arch.persistence.room.Database;<br>import android.content.Context;</pre><pre><a href="http://twitter.com/Database">@Database</a>(entities = {Person.class}, version = 1)<br>public abstract class AppDatabase extends RoomDatabase {</pre><pre>private static volatile AppDatabase INSTANCE;</pre><pre>private static final String DATABASE_NAME = &quot;PersonDB&quot;;</pre><pre>public abstract PersonDao personDao();</pre><pre>public static synchronized AppDatabase getInstance(Context context) {<br>         if (INSTANCE == null) {<br>             //Create database object<br>             INSTANCE = Room.databaseBuilder(context,       AppDatabase.class, DATABASE_NAME).build();</pre><pre>}</pre><pre>return INSTANCE;<br>     }<br>}</pre><p>As you can see, we are using a singleton pattern for the Room database, by doing this, we can have a single database instance for the entire application.</p><p>The next step consists in writing a repository class that will handle UI persistence calls. This design type is useful to let the DAO do only database operations besides providing a way to to handle multiple datasources in the app. To clarify this concept, imagine that your application needs to do a persistence operation on a local database and on a back end API. The repository class would be responsible for managing the calls to these two layers. That said, we can continue with the repository coding.</p><pre>import android.arch.lifecycle.MutableLiveData;<br>import android.content.Context;<br><br>public class PersonRepository {<br><br>    private final PersonDao personDao;<br>    private MutableLiveData&lt;Integer&gt; insertResult = new MutableLiveData&lt;&gt;();<br><br>    public PersonRepository(Context context) {<br>        AppDatabase db = AppDatabase.<em>getInstance</em>(context);<br>        personDao = db.personDao();<br>    }<br><br>    public void insert(Person person) {<br>        insertAsync(person);<br>    }<br><br>    public MutableLiveData&lt;Integer&gt; getInsertResult() {<br>        return insertResult;<br>    }<br><br>    private void insertAsync(final Person person) {<br><br>        new Thread(new Runnable() {<br>            @Override<br>            public void run() {<br>                try {<br>                    personDao.insert(person);<br>                    insertResult.postValue(1);<br>                } catch (Exception e) {<br>                    insertResult.postValue(0);<br>                }<br>            }<br>        }).start();<br><br>    }<br><br>}</pre><p>Let’s try to understand what we did here by explaining parts of the code.</p><pre>private final PersonDao personDao;<br>    private MutableLiveData&lt;Integer&gt; insertResult = new MutableLiveData&lt;&gt;();<br><br>    public PersonRepository(Context context) {<br>        AppDatabase db = AppDatabase.<em>getInstance</em>(context);<br>        personDao = db.personDao();<br>    }<br><br>    public void insert(Person person) {<br>        insertAsync(person);<br>    }<br><br>    public MutableLiveData&lt;Integer&gt; getInsertResult() {<br>        return insertResult;<br>    }</pre><p>In the snippet above, we have two member variables, one for the DAO instance (personDAO) and one for the MutableLiveData that will be observed. After that, we have a constructor that receives an Application Context and instantiates a database class, getting the DAO that will be needed from it to store in the personDAO variable. Then, we have the insert method which receives a model object (person) and calls the private method insertAsync passing the person object. Finishing the snippet above, there’s a getter for the MutableLiveData member variable.</p><p>From now on, I will try to explain the last part of the repository class, that consists on a private method used to perform the background database operation required by the Room library through a thread.</p><pre>private void insertAsync(final Person person) {<br><br>    new Thread(new Runnable() {<br>        @Override<br>        public void run() {<br>            try {<br>                personDao.insert(person);<br>                insertResult.postValue(1);<br>            } catch (Exception e) {<br>                insertResult.postValue(0);<br>            }<br>        }<br>    }).start();<br><br>}</pre><p>Basically, what we have here is a background thread which calls the Dao insert method inside a Try-Catch. If the insert occurs without errors the MutableLiveData value is set to 1 using the postValue method. If an error occurs the value is set to 0. Note that we are using the MutableLiveData postValue method because we are setting the value inside a background thread. If you are setting a value to the MutableLiveData object from the main thread you should use setValue.</p><p>Now, I will show the ViewModel class in the code below.</p><pre>import android.app.Application;<br>import android.arch.lifecycle.AndroidViewModel;<br>import android.arch.lifecycle.LiveData;<br>import android.support.annotation.NonNull;<br><br>public class PersonViewModel extends AndroidViewModel {<br><br>    private PersonRepository personRepository;<br>    private LiveData&lt;Integer&gt; insertResult;<br><br>    public PersonViewModel(@NonNull Application application) {<br>        super(application);<br>        personRepository = new PersonRepository(application);<br>        insertResult = personRepository.getInsertResult();<br>    }<br><br>    public void insert(Person person) {<br>        personRepository.insert(person);<br>    }<br><br>    public LiveData&lt;Integer&gt; getInsertResult() {<br>        return insertResult;<br>    }<br>}</pre><p>So, what is the big deal with the ViewModel class? Well, besides the design pattern advantages that we can have using it, the ViewModel class is able to easily deal with the Activity lifecycle. Thus, when an activity that references a a ViewModel is destroyed an recreated, the ViewModel can reconnect to it automatically and deliver any pending notifications.</p><p>Our ViewModel class will be responsible for calling repository tasks and sending the results to the Activity. Inside the constructor we get an instance of the repository and an instance of the MutableLiveData object to store in member variables. Besides that, we have an insert method that is exposed to the activity and calls the repository insert. And at the end, we have a getter for the LiveData observable.</p><p>Finally, let’s write the Activity class, where all the process begin.</p><pre>import android.arch.lifecycle.Observer;<br>import android.arch.lifecycle.ViewModelProviders;<br>import android.support.annotation.Nullable;<br>import android.support.v7.app.AppCompatActivity;<br>import android.os.Bundle;<br>import android.widget.Toast;<br><br>public class MainActivity extends AppCompatActivity {<br><br>    private PersonViewModel personViewModel;<br><br>    @Override<br>    protected void onCreate(Bundle savedInstanceState) {<br>        super.onCreate(savedInstanceState);<br>        setContentView(R.layout.<em>activity_main</em>);<br><br>        personViewModel = ViewModelProviders.<em>of</em>(this).get(PersonViewModel.class);<br><br>        Person person = new Person();</pre><pre>        person.setName(&quot;Someone&quot;);</pre><pre>        personViewModel.getInsertResult().observe(this, new Observer&lt;Integer&gt;() {<br>            @Override<br>            public void onChanged(@Nullable Integer result) {<br>                if (result == 1) {<br>                    Toast.<em>makeText</em>(MainActivity.this, &quot;Person successfully saved&quot;, Toast.<em>LENGTH_SHORT</em>).show();<br>                } else {<br>                    Toast.<em>makeText</em>(MainActivity.this, &quot;Error saving person&quot;, Toast.<em>LENGTH_SHORT</em>).show();<br>                }<br>            }<br>        });<br><br>        personViewModel.insert(person);<br>    }<br>}</pre><p>The activity above is very simple and the only thing it does is inserting a person record in the database through the ViewModel instance. As we can see, the person data is hardcoded, there is no form for the user to input data. That’s good enough for this example. All we need to achieve here is to update the user interface using the main thread after the execution of a secondary thread. That’s done by observing the LiveData that we get from the ViewModel. That said, the most important code parts here are the next ones:</p><pre>personViewModel = ViewModelProviders.<em>of</em>(this).get(PersonViewModel.class);</pre><p>Initializing the ViewModel and connecting it to the acitivity like the code snippet above.</p><pre>personViewModel.getInsertResult().observe(this, new Observer&lt;Integer&gt;() {<br>            @Override<br>            public void onChanged(@Nullable Integer result) {<br>                if (result == 1) {<br>                    Toast.<em>makeText</em>(MainActivity.this, &quot;Person successfully saved&quot;, Toast.<em>LENGTH_SHORT</em>).show();<br>                } else {<br>                    Toast.<em>makeText</em>(MainActivity.this, &quot;Error saving person&quot;, Toast.<em>LENGTH_SHORT</em>).show();<br>                }<br>            }<br>        });</pre><p>Observing the LiveData value. That way, we can receive the notification from the background thread and properly update the UI after a database operation like we wanted.</p><p>Thus, we reached the end of this article. I hope that I was clear and you could make some use of this example. Thanks for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6836d25e8267" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/room-persistence-library-using-mutablelivedata-observable-to-update-the-ui-after-a-database-6836d25e8267">Room persistence library — Using MutableLiveData observable  to update the UI after a database…</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>