Sunday 7 April 2019

Singleton Bean working in Spring

There are five main scopes for creating a bean in spring.
  1. singleton
  2. prototype
  3. request
  4. session
  5. global-session
 Singleton is default scope type if we don't mention any scope then its singleton by default. A general perception about spring singleton bean is that it behaves same as java singleton class but there are few fundamental differences. Spring IoC container creates only one object for singleton bean and store it in cache memory of all singleton beans. All requests for bean with same name or id will get the same object, It does not mean that class defined as bean is singleton also. lets take an example where we create two beans for same class with different bean name and id then spring will create two different objects in shared memory and will return object based upon name or id given in request.

@Configuration
public class AppConfig {
    @Bean(name={"sineltonTest"})
    @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
    public singeltonTest getSingeltonTest() {
        return new singeltonTest();
    }
   
    @Bean (name={"sineltonTest1"})
    @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
    public singeltonTest getSingeltonTest1() {
        return new singeltonTest();
    }
}

In above example spring will create two different objects for two beans ("sineltonTest", "sineltonTest1") in shared cache and if we use @Autowired with @Qualifire("singeltonTest1") then it will get object from cache created for bean with name "sineltonTest1" so bean is sigleton not the class. There is one more catch w.r.t singleton bean in spring that what happen when we create object of that class with new operator instead of @autowired? This question is asked basically to confuse the person in interview. The clear answer is that spring will not return object from cache now it depends upon exact implementation of the bean class if it is created as java singleton class then one instance is created and every time same instance will be returned otherwise every time new instance is created and returned.

Monday 18 March 2019

Monitor JVM (memory utilization etc.) with JConsole on remote server

It's very common requirement to monitor the memory utilization by our application. This can be done with JConsole. We can monitor JVM on local machine or on remote machine with JConsole.
Firstly we have to run the JConsole, it can be done as:
  1. Go to bin folder of your JDK and run the jconole.exe file.
  2. Open command line and change directory to bin folder of JDK folder and write jconsole.exe and press enter.


Now you can monitor any local process as shown in above image or you can monitor any process on remote machine. Monitoring process on remote machine involves some additional steps, we consider here to monitor tomcat on remote server as:

  1. Go to tomcat folder and locate setenv.bat file(if it does not exist then create an empty file in conf folder). Then add the below lines in setenv.bat file.
    1. set CATALINA_OPTS=-Dcom.sun.management.jmxremote
        -Dcom.sun.management.jmxremote.port=PORT number
        -Dcom.sun.management.jmxremote.ssl=false
        -Dcom.sun.management.jmxremote.authenticate=false 
       
      If you want to further authenticate the monitoring then follow the link
      https://tomcat.apache.org/tomcat-8.0-doc/monitoring.html
       
  2.  Restart tomcat server.
  3. Open JConsole on you system. Select "Remote Process" and enter IP address of remote server with port you have given in step 1.
     
  4.  
     
     

Friday 15 March 2019

OTRS Rest API integration

OTRS is a tool to log tickets online. This is very basic requirement to log tickets and track them online. OTRS gives this functionality but if you want to integrate this functionality in you application then OTRS provide you with some basic API's like to create ticket, get ticket by id, update ticket, search ticket. Please follow below screenshots to enable in it your OTRS tool. Here we are assuming that OTRS tool is already installed.

  1. Login in OTRS with admin user.
2. Goto "Admin" tab.


3. Find  WebService link as:



4. Click on WebService.



5. Click on "add web service".


6. Click on "Import web service"  and choose file from desktop (use Test.yaml file given below)
 

Test.yaml file

---
Debugger:
  DebugThreshold: debug
  TestMode: '0'
Description: Is used by me
FrameworkVersion: 4.0.5
Provider:
  Operation:
    TicketGet:
      Description: ''
      MappingInbound:
        Type: Simple
      MappingOutbound:
        Type: Simple
      Type: Ticket::TicketGet
    TicketUpdate:
      Description: ''
      MappingInbound:
        Type: Simple
      MappingOutbound:
        Type: Simple
      Type: Ticket::TicketUpdate
    TicketCreate:
      Description: ''
      MappingInbound:
        Type: Simple
      MappingOutbound:
        Type: Simple
      Type: Ticket::TicketCreate
    TicketSearch:
      Description: ''
      MappingInbound:
        Type: Simple
      MappingOutbound:
        Type: Simple
      Type: Ticket::TicketSearch
  Transport:
    Config:
      KeepAlive: ''
      MaxLength: '20000000'
      RouteOperationMapping:
        TicketGet:
          Route: /TicketGet/:TicketID
        TicketUpdate:
          RequestMethod:
          - POST
          Route: /TicketUpdate/:TicketID
        TicketCreate:
          RequestMethod:
          - POST
          Route: /TicketCreate
        TicketSearch:
          RequestMethod:
          - GET
          Route: /TicketSearch
    Type: HTTP::REST
RemoteSystem: ''
Requester:
  Transport:
    Type: ''

It will expose the web service and you can test it with any client tool like POSTMAN etc

You need to add parameter user name as UserLogin  and password as password to access any api.

Example of get API (GET)
 http://server address/otrs/nph-genericinterface.pl/Webservice/Test/TicketGet/Ticketno?UserLogin=username&Password=password

Example of Create API (POST)
http://server address/otrs/nph-genericinterface.pl/Webservice/Test/TicketCreate?UserLogin=user name&Password=password

JSON in Body as: 
 {
"Ticket" : {
"QueueID" : "1",
"PriorityID" : "5",
"CustomerUser" : "email id",
"Title" : "REST Create Test",
"State" : "open"
},
"Article" : {
"ContentType" : "text/plain; charset=utf8",
"Subject" : "Rest Create Test",
"Body" : "This is only a test"
}
}


Example of update API (POST)
http://server address/otrs/nph-genericinterface.pl/Webservice/Test/TicketUpdate/22645?UserLogin=user login&Password=password

JSON in Body as: 
 {
"Ticket" : {
"QueueID" : "1",
"PriorityID" : "5",
"CustomerUser" : "email id",
"Title" : "REST Create Test",
"State" : "open"
},
"Article" : {
"ContentType" : "text/plain; charset=utf8",
"Subject" : "Rest Create Test Update",
"Body" : "This is only a test updated"
}
}


Example of searc API
http://server address/otrs/nph-genericinterface.pl/Webservice/Test/TicketSearch?UserLogin=user name&Password=password