package com.vladmihalcea.book.hpjp.hibernate.jmx;

import com.vladmihalcea.book.hpjp.util.AbstractTest;
import org.hibernate.cfg.AvailableSettings;
import org.junit.Ignore;
import org.junit.Test;

import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author Vlad Mihalcea
 */
public class JmxTest extends AbstractTest {

    @Override
    protected Class<?>[] entities() {
        return new Class<?>[] {
            Person.class,
        };
    }

    @Override
    protected void additionalProperties(Properties properties) {
        properties.put(AvailableSettings.GENERATE_STATISTICS, Boolean.TRUE.toString());
        properties.put("hibernate.jmx.enabled", Boolean.TRUE.toString());
        properties.put("hibernate.cache.use_second_level_cache", Boolean.TRUE.toString());
        properties.put("hibernate.cache.region.factory_class", "ehcache");
    }

    private int seconds = 120;

    @Test
    @Ignore
    public void test() {
        long startNanos = System.nanoTime();

        AtomicLong id = new AtomicLong();

        while (TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - startNanos) < seconds) {
            doInJPA(entityManager -> {
                if (Math.random() < 0.1) {
                    Person person = new Person();
                    person.setId(id.incrementAndGet());

                    entityManager.persist(person);
                }
            });
        }
    }

    @Entity(name = "Person")
    public static class Person  {

        @Id
        private Long id;

        private String firstName;

        private String lastName;

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    }
}

@@ -252,16 +252,67 @@
 nce.Id;%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A
 import j
@@ -857,32 +857,116 @@
 UE.toString());%0A
+        properties.put(%22hibernate.jmx.usePlatformServer%22, Boolean.TRUE.toString());%0A
         properti
@@ -1268,16 +1268,22 @@
 cLong id
+Holder
  = new A
@@ -1291,25 +1291,108 @@
 omicLong();%0A
-%0A
+        List%3CLong%3E ids = new ArrayList%3C%3E();%0A%0A        doInJPA(entityManager -%3E %7B%0A    
         whil
@@ -1486,34 +1486,24 @@
     
-doInJPA(entityManager -%3E %7B
+    sleep(1000);
 %0A   
@@ -1537,17 +1537,18 @@
 m() %3C 0.
-1
+25
 ) %7B%0A    
@@ -1567,23 +1567,145 @@
     
-Person person =
+Long id = idHolder.incrementAndGet();%0A                    ids.add(id);%0A                    entityManager.persist(%0A                       
  new
@@ -1713,17 +1713,16 @@
 Person()
-;
 %0A       
@@ -1734,22 +1734,24 @@
         
-person
+        
 .setId(i
@@ -1755,78 +1755,595 @@
 d(id
-.incrementAndGet());%0A%0A                    entityManager.persist(person
+)%0A                            .setFirstName(String.format(%22First Name - %25d%22, id))%0A                            .setLastName(String.format(%22Last Name - %25d%22, id))%0A                    );%0A                %7D else %7B%0A                    List%3CPerson%3E persons = entityManager.createQuery(%22%22%22%0A                        select p%0A                        from Person p%0A                        where p.id in :ids%0A                        %22%22%22, Person.class)%0A                    .setParameter(%22ids%22,ids)%0A                    .getResultList();%0A                    LOGGER.info(%22Person count: %7B%7D%22, persons.size()
 );%0A 
@@ -2372,18 +2372,16 @@
        %7D
-);
 %0A       
@@ -2378,24 +2378,26 @@
  %7D%0A        %7D
+);
 %0A    %7D%0A%0A    
@@ -2638,28 +2638,30 @@
      public 
-void
+Person
  setId(Long 
@@ -2688,24 +2688,49 @@
 is.id = id;%0A
+            return this;%0A
         %7D%0A%0A 
@@ -2819,28 +2819,30 @@
      public 
-void
+Person
  setFirstNam
@@ -2895,32 +2895,57 @@
 me = firstName;%0A
+            return this;%0A
         %7D%0A%0A     
@@ -3032,20 +3032,22 @@
  public 
-void
+Person
  setLast
@@ -3100,32 +3100,57 @@
 ame = lastName;%0A
+            return this;%0A
         %7D%0A    %7D%0A

Fix the JMXTest to work with Hibernate 5.4