Tuesday, November 17, 2015

MySql Start / Stop / Restart



I was experiencing following error in my mac after i upgraded to Yosemite.

Shammis-MacBook-Pro:~ shammi$ mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)


Then i went through so many ways to get rid of the issue. Finally this advise made it working. I found that from here.

Create /Library/LaunchDaemons/com.mysql.mysql.plist and save it with the following plist:


  1. xml version="1.0" encoding="UTF-8"?>  
  2. >  
  • <plist version="1.0">  
  •   <dict>  
  •    <key>KeepAlive</key>  
  •    <true/>  
  •    <key>Label</key>  
  •    <string>com.mysql.mysqld</string>  
  •    <key>ProgramArguments</key>  
  •    <array>  
  •    <string>/usr/local/mysql/bin/mysqld_safe</string>  
  •    <string>--user=mysql</string>  
  •    </array>  
  •   </dict>  
  • </plist>  



  • And also changing some permisions to the file:

    sudo chown root /Library/LaunchDaemons/com.mysql.mysql.plist
    sudo chgrp wheel 
    /Library/LaunchDaemons/com.mysql.mysql.plist
    sudo chmod 
    644 /Library/LaunchDaemons/com.mysql.mysql.plist

    And finally load the plist:

    sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysql.plist


    In the same time, It will be useful to know following which i found from [1]

    To restart, start or stop MySQL server from the command line, type the following at the shell prompt…

    On Linux start/stop/restart from the command line:

     /etc/init.d/mysqld start
     /etc/init.d/mysqld stop
     /etc/init.d/mysqld restart
    Some Linux flavours offer the service command too
     service mysqld start
     service mysqld stop
     service mysqld restart
    or
     service mysql start
     service mysql stop
     service mysql restart

    On OS X to start/stop/restart MySQL from the command line:

     sudo /usr/local/mysql/support-files/mysql.server start
     sudo /usr/local/mysql/support-files/mysql.server stop
     sudo /usr/local/mysql/support-files/mysql.server restart

    Friday, October 2, 2015

    ERROR java.io.InvalidClassException: org.apache.synapse.message.store.impl.jms.SynapseMessage; local class incompatible


    Recently i was working with Message Store /Message Processor scenario in WSO2 ESB. In that i developed a CAR file with Dev Studio and used it with ESB 4.8.1.

    Then i executed the scenario of sending some messages to Message Store while making the Backend unavailable which leaded to remain some messages in the queue.

    Then , i used the same CAR file in ESB 4.8.0 and once i deploy the CAR file , i was getting following exception continuously.

    ERROR - JmsConsumer [TestMessageStore-C-263] cannot receive message from store. Error:Failed to build body from bytes. Reason: java.io.InvalidClassException: org.apache.synapse.message.store.impl.jms.SynapseMessage; local class incompatible: stream classdesc serialVersionUID = -7391992645087640122, local class serialVersionUID = -8457646720705005296

    Then i initially thought that this can be due to some incompatibility of Dev Studio version with ESB 4.8.1 and 4.8.0. But i was wrong.

    Actual problem was my previous act before i shutdown ESB 4.8.1. Because the message processor could not process the messages previously due to the un availability of the back end, There were some messages remaining which i sent with ESB 4.8.1.

    Then when i started 4.8.0 and deploy the CAR file, it tried to process those existing messages from the queue. Since those message were sent from different serialVersionUID of the same class org.apache.synapse.message.store.impl.jms.SynapseMessage  It is not possible to deserialise them with different serialVersionUID.

    So , once i removed all the previous messages from the queue, it worked perfectly.


    Wednesday, August 5, 2015

    How to setup networking for Virtualbox VMs in Mac

    By default when we configure Virtual box VM for  Mac we are getting networking with NAT. The configuration looks like bellow.




    But with that i could not PING from my Mac to the Windows VM or vice-versa.  As usual i changed the connection to Bridge mode but no success. Then contacted my Networking friend "Dhanushka Ranasinge" and with his guide i could get it done.

    Following is the TIP.

    For the VM
    1. Change the  network interface to "Host-Only Adapter" as in the bellow image.


    2. Then start the VM and disable to windows firewall.



    Now you are in a STATE that you can PING from your MAC to WINDOWS.

    Now we need to set up internet for the VM.

    3. Shutdown the VM and add second Network adaptor for NAT



    With this NAT interface , it will connect your VM to internet. Now it is all done. Start your VM and you should be able to connect to INTERNET and you should be able to PING from MAC to WINDOWS and vice-versa.

    Thursday, July 9, 2015

    How to deal with "java.nio.charset.MalformedInputException: Input length = 1" WSO2 ESB

    Some times we are receiving unusual characters in our responses from various back ends. Then WSO2 ESB is facing problems in understanding those characters from the response and it tends to throw following exception.

    [2015-07-09 12:42:49,651] ERROR - TargetHandler I/O error: Input length = 1
    java.nio.charset.MalformedInputException: Input length = 1
        at java.nio.charset.CoderResult.throwException(CoderResult.java:277)
        at org.apache.http.impl.nio.reactor.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:193)
        at org.apache.http.impl.nio.codecs.AbstractMessageParser.parse(AbstractMessageParser.java:171)
        at org.apache.synapse.transport.http.conn.LoggingNHttpClientConnection$LoggingNHttpMessageParser.parse(LoggingNHttpClientConnection.java:210)
        at org.apache.synapse.transport.http.conn.LoggingNHttpClientConnection$LoggingNHttpMessageParser.parse(LoggingNHttpClientConnection.java:192)
        at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:171)
        at org.apache.synapse.transport.http.conn.LoggingNHttpClientConnection.consumeInput(LoggingNHttpClientConnection.java:106)
        at org.apache.synapse.transport.passthru.ClientIODispatch.onInputReady(ClientIODispatch.java:83)
        at org.apache.synapse.transport.passthru.ClientIODispatch.onInputReady(ClientIODispatch.java:41)
        at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:119)
        at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:160)
        at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:342)
        at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:320)
        at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:280)
        at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:106)
        at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:604)
        at java.lang.Thread.run(Thread.java:745)
    


    Most of the case,  by adding following property to the "passthru-http.properties" file will solve the case.

    Eg:  once i was getting a response like following

    [2015-07-09 12:47:55,236] DEBUG - wire >> "HTTP/1.1 100 Continue[\r][\n]"
    [2015-07-09 12:47:55,237] DEBUG - wire >> "[\r][\n]"
    [2015-07-09 12:47:55,274] DEBUG - wire >> "HTTP/1.1 201 Cr[0xe9]e[\r][\n]"
    [2015-07-09 12:47:55,274] DEBUG - wire >> "Set-Cookie: JSESSIONID=87101C43FCABBB97D049C0F0C8DD216D; Path=/api/; Secure; HttpOnly[\r][\n]"
    [2015-07-09 12:47:55,274] DEBUG - wire >> "Location: https://abc.foo.com/api/office/files/2038[\r][\n]"
    [2015-07-09 12:47:55,274] DEBUG - wire >> "Content-Type: application/json[\r][\n]"
    [2015-07-09 12:47:55,274] DEBUG - wire >> "Transfer-Encoding: chunked[\r][\n]"
    [2015-07-09 12:47:55,275] DEBUG - wire >> "Vary: Accept-Encoding[\r][\n]"
    [2015-07-09 12:47:55,275] DEBUG - wire >> "Date: Thu, 09 Jul 2015 16:47:55 GMT[\r][\n]"
    [2015-07-09 12:47:55,275] DEBUG - wire >> "Server: qa-byp[\r][\n]"
    [2015-07-09 12:47:55,275] DEBUG - wire >> "[\r][\n]"
    [2015-07-09 12:47:55,275] DEBUG - wire >> "1f[\r][\n]"
    [2015-07-09 12:47:55,275] DEBUG - wire >> "{"person":john}[\r][\n]"
    [2015-07-09 12:47:55,275] DEBUG - wire >> "0[\r][\n]"
    [2015-07-09 12:47:55,275] DEBUG - wire >> "[\r][\n]"
    


    Then it thew the above exception.

    By adding the above entry as follows, i could get rid of that.

    http.protocol.element-charset=iso-8859-1
    

    සිතුවිල්ල





    එදා සුදු ගවුම ඇඳන් බස් එකෙන් ඇවිත් බහිනකල්
    නොඉවසිල්ලෙන් බස් ස්ටැන්ඩ් එකට වෙලා බලන් උන්නු මම...
    අද..
    පුංචි පුතාගෙ හුරතල් හිනාව බලන්න
    ගෙදර යන්න ඔරලොසුවෙ කටුව කැරකෙනකල් බලන් ඉන්නවා..
    කාලය... නුඹේ අරුමය..

    Wednesday, July 8, 2015

    WSO2 ESB API with JMS Queues - HTTP GET

    We are going to discuss on how we can handle HTTP GET methods with JMS Proxy services in WSO2. With this blog we are going to discuss following message flow.


    Flow is like follows;


    • Client invokes an API defined in ESB
    • From the API it sends the message to a JMS queue specifying a "ReplyTo" queue
    • There is a JMS proxy consume message from the above queue 
    • After consuming message, it will invoke the backend and response will be sent back to JMS "Reply To" queue
    • Rest API will consume the message from that "Reply To" queue and send back the response to the client.


    In order to have this flow working, you need to setup ActiveMQ with wso2 ESB. You can find the documentation to do it in this documentation page.

    Lets see the API


    <api xmlns="http://ws.apache.org/ns/synapse" name="WeatherAPI" context="/getQuote">
       <resource methods="POST GET" uri-template="/details?*">
          <inSequence>
             <property name="transport.jms.ContentTypeProperty" value="Content-Type" scope="axis2"></property>
             <log>
                <property name="httpMethod" expression="get-property('axis2','HTTP_METHOD')"></property>
             </log>
             <property name="HTTP_METHOD" expression="get-property('axis2','HTTP_METHOD')" scope="transport" type="STRING"></property>
             <send>
                <endpoint>
                   <address uri="jms:/SMSStore?transport.jms.ConnectionFactoryJNDIName=QueueConnectionFactory&java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory&java.naming.provider.url=tcp://localhost:61616&transport.jms.DestinationType=queue&transport.jms.ContentTypeProperty=Content-Type&transport.jms.ReplyDestination=SMSReceiveNotificationStore"></address>
                </endpoint>
             </send>
          </inSequence>
          <outSequence>
             <property name="TRANSPORT_HEADERS" scope="axis2" action="remove"></property>
             <send></send>
          </outSequence>
       </resource>
    </api>
    

    In the above API , there are few things to be focused on.
    • You can see that we are setting the HTTP_METHOD to a property extracted from the incoming message as follows.  By default, for HTTP POST method, API invocation works perfectly with out setting this property at the end. But If we are doing a HTTP GET , we need to obtain the HTTP method at the consumer side of the queue to invoke the back end with a http GET. So we are setting it here. 

    <property name="HTTP_METHOD" expression="get-property('axis2','HTTP_METHOD')" scope="transport" type="STRING"></property>
    


    • In the endpoint of the send mediator we are setting up following information which you should have 
      • transport.jms.ContentTypeProperty=Content-Type  - We are saying that we are passing the content type in JMS transport
      • ransport.jms.ReplyDestination=SMSReceiveNotificationStore - We are saying that ESB is expecting a response to this queue for this request 

    Then in the JMS proxy which we consume the message, it will look like follows.


    <proxy xmlns="http://ws.apache.org/ns/synapse"
           name="SMSForwardProxy"
           transports="jms"
           statistics="disable"
           trace="disable"
           startOnLoad="true">
       <target faultSequence="fault">
          <inSequence>
             <property name="HTTP_METHOD" expression="$trp:HTTP_METHOD" scope="axis2"/>
             <send>
                <endpoint>
                   <address uri="http://localhost:8080/foo">
                      <suspendOnFailure>
                         <errorCodes>-1</errorCodes>
                         <progressionFactor>1.0</progressionFactor>
                      </suspendOnFailure>
                      <markForSuspension>
                         <errorCodes>-1</errorCodes>
                      </markForSuspension>
                   </address>
                </endpoint>
             </send>
          </inSequence>
          <outSequence>
             <send/>
          </outSequence>
       </target>
       <parameter name="transport.jms.ContentType">
          <rules>
             <jmsProperty>contentType</jmsProperty>
             <default>text/xml</default>
          </rules>
       </parameter>
       <parameter name="transport.jms.ConnectionFactory">myQueueConnectionFactory</parameter>
       <parameter name="transport.jms.DestinationType">queue</parameter>
       <parameter name="transport.jms.Destination">SMSStore</parameter>
       <description/>
    </proxy>
    


    In the above proxy configuration, you can see following line


     <property name="HTTP_METHOD" expression="$trp:HTTP_METHOD" scope="axis2"/>
    

    In that , what we do is , we read the HTTP_METHOD we already set in previous API before sending the message to JMS Queue and now we are reading it from transport level property.  Then we are setting it to "HTTP_METHOD" property with the "axis2" scope.

    With that it will be able to figure out the HTTP method to be used on invoking the actual endpoint.


    After getting a response, this Proxy will send the reply to the "SMSReceiveNotificationStore" queue. Then it will consumed from the previous API and send back the response to the client.

    This will be working perfectly for HTTP POST requests. If it is a HTTP GET Request , you need to do some additional changes from the client side and the server side.

    Client Side Changes


    • We need to pass the content type from the client side as follows. You can set it as a header in SOAP UI
           contentType:  application/x-www-form-urlencoded




    Server Side Changes
    • You need to change the builder and formatter for above content type in the axis2.xml file located in WSO2ESB/repository/conf/axis2 directory as follows. 

    .....
    
    <messageFormatter contentType="application/x-www-form-urlencoded"
                            class="org.wso2.carbon.relay.ExpandingMessageFormatter"/>
    
    .......
    
    <messageBuilder contentType="application/x-www-form-urlencoded"
                            class="org.wso2.carbon.relay.BinaryRelayBuilder"/>
    
    ......
    

    If you does not set above , there will be message building and formatting errors.


    With this way you ll be able to get this working for GET http methods.

    This is tested with WSO2 ESB 4.8.1 with it's latest patches and Apache Activemq 5.10.0


    Monday, June 29, 2015

    How to generate a custom Error Message with Custom HTTP Status Code for unavailable Resources in WSO2 ESB


    WSO2 ESB 4.8.1  does not throw any exception or error message when an API defined is access with incorrect HTTP method and it will just respond with 202.  In this blog post , i am explaining on how we can get a custom HTTP status code for the above.


    In order to get a custom error message , you need to add following sequence to ESB which is not there by default.


    <?xml version="1.0" encoding="UTF-8"?>
    <sequence xmlns="http://ws.apache.org/ns/synapse" name="_resource_mismatch_handler_">
       <payloadFactory media-type="xml">
          <format>
             <tp:fault xmlns:tp="http://test.com">
                <tp:code>405</tp:code>
                <tp:type>Status report</tp:type>
                <tp:message>Method not allowed</tp:message>
                <tp:description>The requested HTTP method for resource (/$1) is not allowed.</tp:description>
             </tp:fault>
          </format>
          <args>
             <arg xmlns:ns="http://org.apache.synapse/xsd"
                  xmlns:ns3="http://org.apache.synapse/xsd"
                  evaluator="xml"
                  expression="$axis2:REST_URL_POSTFIX"/>
          </args>
       </payloadFactory>
       <property name="NO_ENTITY_BODY" scope="axis2" action="remove"/>
       <property name="HTTP_SC" value="405" scope="axis2"/>
       <respond/>
       <drop/>
    </sequence>
    

    In ESB documentation [1] , it has explained that in order to handle non-matching resources, it is needed to define this sequence _resource_mismatch_handler_


    [1] https://docs.wso2.com/display/ESB481/Configuring+Specific+Use+Cases#ConfiguringSpecificUseCases-Handlingnon-matchingresources