Saturday, November 8, 2014

[WSO2 ESB] [4.8.1] How to Convert XML to JSON Array

Following API Demonstrate this functionality.

<api xmlns="http://ws.apache.org/ns/synapse" name="Transform" context="/Transform">
<resource methods="POST">
<inSequence>
<property name="messageType" value="application/json" scope="axis2"></property>
<log level="custom">
<property name="Status" expression="json-eval($.StockQuotes.Stock)"></property>
</log>
<filter source="json-eval($.StockQuotes.Stock.Symbol)" regex="null">
<then>
<payloadFactory media-type="json">
<format>{ "StockQuotes": { "Stock": $1 } }</format>
<args>
<arg evaluator="json" expression="$.StockQuotes.Stock"></arg>
</args>
</payloadFactory>
</then>
<else>
<payloadFactory media-type="json">
<format>{ "StockQuotes": { "Stock": [ $1 ] } }</format>
<args>
<arg evaluator="json" expression="$.StockQuotes.Stock"></arg>
</args>
</payloadFactory>
</else>
</filter>
<respond></respond>
</inSequence>
</resource>
</api>
view raw Transform.xml hosted with ❤ by GitHub
Try above api with a rest client with following sample requests.

1)Multiple Stocks.
XML request:
<StockQuotes>
<Stock>
<Symbol>IBM</Symbol>
<Last>0.00</Last>
<Date>N/A</Date>
<Time>N/A</Time>
<Change>N/A</Change>
<Open>N/A</Open>
<High>N/A</High>
<Low>N/A</Low>
<Volume>N/A</Volume>
<MktCap>N/A</MktCap>
<PreviousClose>N/A</PreviousClose>
<PercentageChange>N/A</PercentageChange>
<AnnRange>N/A - N/A</AnnRange>
<Earns>N/A</Earns>
<P-E>N/A</P-E>
<Name>IBM</Name>
</Stock>
<Stock>
<Symbol>WSO2</Symbol>
<Last>0.00</Last>
<Date>N/A</Date>
<Time>N/A</Time>
<Change>N/A</Change>
<Open>N/A</Open>
<High>N/A</High>
<Low>N/A</Low>
<Volume>N/A</Volume>
<MktCap>N/A</MktCap>
<PreviousClose>N/A</PreviousClose>
<PercentageChange>N/A</PercentageChange>
<AnnRange>N/A - N/A</AnnRange>
<Earns>N/A</Earns>
<P-E>N/A</P-E>
<Name>WSO2</Name>
</Stock>
</StockQuotes>
JSON response :
{
"StockQuotes":{
"Stock":[
{
"Name":"IBM",
"PercentageChange":"N\/A",
"Low":"N\/A",
"Open":"N\/A",
"Last":0.0,
"Time":"N\/A",
"MktCap":"N\/A",
"Change":"N\/A",
"Date":"N\/A",
"PreviousClose":"N\/A",
"Volume":"N\/A",
"Symbol":"IBM",
"AnnRange":"N\/A - N\/A",
"Earns":"N\/A",
"High":"N\/A",
"P-E":"N\/A"
},
{
"Name":"WSO2",
"PercentageChange":"N\/A",
"Low":"N\/A",
"Open":"N\/A",
"Last":0.0,
"Time":"N\/A",
"MktCap":"N\/A",
"Change":"N\/A",
"Date":"N\/A",
"PreviousClose":"N\/A",
"Volume":"N\/A",
"Symbol":"WSO2",
"AnnRange":"N\/A - N\/A",
"Earns":"N\/A",
"High":"N\/A",
"P-E":"N\/A"
}
]
}
}
2) Single Stock
XML request:
<StockQuotes>
<Stock>
<Symbol>WSO2</Symbol>
<Last>0.00</Last>
<Date>N/A</Date>
<Time>N/A</Time>
<Change>N/A</Change>
<Open>N/A</Open>
<High>N/A</High>
<Low>N/A</Low>
<Volume>N/A</Volume>
<MktCap>N/A</MktCap>
<PreviousClose>N/A</PreviousClose>
<PercentageChange>N/A</PercentageChange>
<AnnRange>N/A - N/A</AnnRange>
<Earns>N/A</Earns>
<P-E>N/A</P-E>
<Name>WSO2</Name>
</Stock>
</StockQuotes>
JSON response (As an array):
{
"StockQuotes":{
"Stock":[
{
"Name":"WSO2",
"PercentageChange":"N\/A",
"Low":"N\/A",
"Open":"N\/A",
"Last":0.0,
"Time":"N\/A",
"MktCap":"N\/A",
"Change":"N\/A",
"Date":"N\/A",
"PreviousClose":"N\/A",
"Volume":"N\/A",
"Symbol":"WSO2",
"AnnRange":"N\/A - N\/A",
"Earns":"N\/A",
"High":"N\/A",
"P-E":"N\/A"
}
]
}
}
This is with following message formatter and builder
        <messageformatter class="org.apache.synapse.commons.json.JsonStreamFormatter" contenttype="application/json" />

        <messageformatter class="org.apache.synapse.commons.json.JsonStreamFormatter" contenttype="application/json" />

Wednesday, November 5, 2014

[Active-MQ] PortOffset

1) Changing transport ports. Edit /conf/activemq.xml
and change ports numbers in following configuration.
<beans ...>
<broker ...>
...
<!--
The transport connectors expose ActiveMQ over a given protocol to
clients and other brokers. For more information, see:
http://activemq.apache.org/configuring-transports.html
-->
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
</transportConnectors>
...
</broker>
</beans>
view raw activemq.xml hosted with ❤ by GitHub

2) Changing web console port. Edit /conf/jetty.xml
and change following port in following config.
<beans ..>
..
<bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start">
<!-- the default port number for the web console -->
<property name="host" value="0.0.0.0"/>
<property name="port" value="8161"/>
</bean>
...
</beans>
view raw jetty.xml hosted with ❤ by GitHub

[Active-MQ] Setting up AMQ with MySQL database.



1) Create MySQL database.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| phpmyadmin |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database activemq;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| activemq |
| mysql |
| performance_schema |
| phpmyadmin |
+--------------------+
5 rows in set (0.00 sec)
mysql> use activemq;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql>
view raw mysql hosted with ❤ by GitHub
2) Download activeMQ and extract it.
3) Edit /conf/activemq.xml
4) Add following configuration.

<beans ...>
<bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/activemq?relaxAutoCommit=true"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="poolPreparedStatements" value="true"/>
</bean>
<broker ...>
<!--
Configure message persistence for the broker. The default persistence
mechanism is the KahaDB store (identified by the kahaDB tag).
For more information, see:
http://activemq.apache.org/persistence.html
-->
<persistenceAdapter>
<!--kahaDB directory="${activemq.data}/kahadb"/-->
<jdbcPersistenceAdapter dataSource="#mysql-ds"/>
</persistenceAdapter>
</broker>
</beans>
view raw activemq.xml hosted with ❤ by GitHub
5) Copy mysql jdbc driver (mysql-connector-java-5.1.25-bin.jar) in the directory "activemq_home/lib/optional"

6) Start ActiveMQ server using $ ./activemq start ( To stop the server use ./activemq stop )

7) Log in to activeMQ management console using http://localhost:8161/ with admin:admin credentials.

8) Create a queue and send a message to the queue with persistence enabled.

9) You can see the message in database.

mysql>
mysql> show tables;
+--------------------+
| Tables_in_activemq |
+--------------------+
| ACTIVEMQ_ACKS |
| ACTIVEMQ_LOCK |
| ACTIVEMQ_MSGS |
+--------------------+
3 rows in set (0.00 sec)
mysql> select * from ACTIVEMQ_MSGS;
+----+-------------------+-----------------------------------------------+-----------+------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------+------+
| ID | CONTAINER | MSGID_PROD | MSGID_SEQ | EXPIRATION | MSG | PRIORITY | XID |
+----+-------------------+-----------------------------------------------+-----------+------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------+------+
| 1 | queue://TestQueue | ID:hasitha-laptop-48562-1415139260206-3:1:1:1 | 1 | 0 | � { )ID:hasitha-laptop-48562-1415139260206-3:1  d TestQueue n{ )ID:hasitha-laptop-48562-1415139260206-3:1       I|��  
Hello world. I|�� | 0 | NULL |
+----+-------------------+-----------------------------------------------+-----------+------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------+------+
1 row in set (0.00 sec)
mysql>


Saturday, November 1, 2014

Packt offers its library subscription with an additional $150 worth of free content

Packt provides full online access to over 2000 books and videos to give users the knowledge they need, when they need it. From innovative new solutions and effective learning services to cutting edge guides on emerging technologies, Packt’s extensive library has got it covered.

For a limited time only, Packt is offering 5 free eBook or Video downloads in the first month of a new annual subscription – up to $150 worth of extra content. That’s in addition to one free download a month for the rest of the year.

This special PacktLib Plus deal expires on 4th November, 2014.

Check out the deal here http://bit.ly/1wN0Rq0