Buscar este blog

lunes, 10 de abril de 2017

ActiveMQ - SQL Server - Change default schema

When using ActiveMQ JBDB Persistence Adapter with SQL Server, all tables are created in the default dbo schema. If you use a Master/Slave configuration (http://activemq.apache.org/jdbc-master-slave.html), all brokers will share this schema and use these tables to synchronize.

Then, I was thinking of having a network of brokers with two pairs of Master/Slave brokers. There would be the following brokers:
  • Broker AM. Master broker of "subnetwork" A
  • Broker AS. Slave broker of "subnetwork" A
  • Broker BM. Master broker of "subnetwork" B
  • Broker BS. Slave broker of "subnetwork" B
Because of you have two pairs of Master/Slave, you need two databases to syncronize them, each one with its default dbo chema. But if you set two diferent connection users and, for each user, you set a different default schema, SQL Server will use this schema in all SQL Sentences. So you can use the same database and all the extra config will be handled by SQL Server.

The scenario will be as follows:


In SQL Server Management Studio you create two users:
  • ActiveMQ1
  • ActiveMQ2
Then you associate these users with the storage database.


Next, you create the two schemas and set the corresponding user as owner. Schemas are created in [database] > Security > Schemas option.

Finally, set the default schema for each user. This is done in [database] > Security > Users > [user].


Done. When brokers start, the master of each network (the first one to start) will create its tables in its own schema.

ActiveMQ - SQL Server - JDBC Master-Slave

These are the basic instructions to configure a Shared JDBC Persistence Adapter with SQL Server. The objective is to prepare a master/slave configuration with two or more Brokers.

Tested with ActiveMQ 5.14.4.

Add the following dependencies to ACTIVEMQ_HOME/lib/extra in all brokers:
  • Apache commons dbcp2
  • Apache commons pool 2
  • sqljdbc4

Edit the broker config in ACTIVEMQ_HOME/conf/activemq.xml, for all nodes:
<beans ... >

 (...)
  
 <bean id="sqlServerDS" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
   <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
   <property name="url" value="jdbc:sqlserver://windowsServer-R2:1433;DatabaseName=ActiveMQ"/>
   <property name="username" value="activemq"/>
   <property name="password" value="activemq"/>
   <property name="poolPreparedStatements" value="true"/>
 </bean>
 
 (...)
  
 <broker xmlns="http://activemq.apache.org/schema/core" brokerName="brokerA" dataDirectory="${activemq.data}">  
     (...)
  
     <persistenceAdapter>
        <jdbcPersistenceAdapter dataDirectory="${activemq.base}/data" dataSource="#sqlServerDS" />          
     </persistenceAdapter>
  
     (...)
 </broker>
</beans>

The database should be empty and the first time the first broker starts, all tables will be automatically created (you could get some warnings at this point)


viernes, 31 de marzo de 2017

JBoss EAP Allow weak password

In order to allow weak user passwords edit JBOSS_HOME/bin/add-user.properties file.

Set de following config keys:
  • password.restriction=RELAX
  • password.restriction.minLength=4
  • password.restriction.mustNotMatchUsername=FALSE
  • password.restriction.forbiddenValue=
  • password.restriction.strength=VERY_WEAK

# Valid values: RELAX, WARN or REJECT
# RELAX : Don't perform any strength checks on the password in both interactive and non-interactive mode
# WARN : Display a message about the strength of the password. Ask confirmation if the password is weak in interactive mode
# REJECT : Display a message about the strength of the password (if the password is weak, the user is not created).
# Ask confirmation if the password is weak in interactive mode
password.restriction=RELAX

# Password minimum length
password.restriction.minLength=4

# Password must contains at least one alpha
password.restriction.minAlpha=1

# Password must contains at least one digit
password.restriction.minDigit=1

# Password must contains at least one symbol
password.restriction.minSymbol=1

# Password must not match the username. Valid values: TRUE or FALSE.
password.restriction.mustNotMatchUsername=FALSE

# Comma separated list of forbidden passwords (easily guessable)
password.restriction.forbiddenValue=

# Password strength. Valid values: VERY_WEAK, WEAK, MODERATE, MEDIUM, STRONG, VERY_STRONG or EXCEPTIONAL.
# If not present, it defaults to "MODERATE"
password.restriction.strength=VERY_WEAK

# Class of password strength checker.
# If not present, utility will revert to default implementation
password.restriction.checker=org.jboss.as.domain.management.security.password.simple.SimplePasswordStrengthChecker

A dream come true for lazy (and bad memory) developers.

jueves, 9 de marzo de 2017

JavaMelody Collect Server - CentOS - Configure as Service

This is how I configured the JavaMelody Collect Server to start as a service in CentOS 6. The collect server is deployed as a WAR with its embedded server.

JavaMelody: https://github.com/javamelody/javamelody/wiki
JavaMelody Collect Server: https://github.com/javamelody/javamelody/wiki/UserGuideAdvanced#optional-centralization-server-setup

Directory structure:
 - /opt/javamelody
     - javamelody-X.X.X.war
     - javamelody.war (symbolic link to the previous war)
     - conf
         - javamelody.sh (launch script)
      - work
         - tmp (java temp directory for de process)
         - storage (storage directory for monitor info)


First, create the directoy structure:
mkdir /opt/javamelody

cp javamelody-1.63.0.war /opt/javamelody/

ln -s  /opt/javamelody/javamelody-1.63.0.war /opt/javamelody/javamelody.war

mkdir /opt/javamelody/work

mkdir /opt/javamelody/work/tmp

mkdir /opt/javamelody/work/storage

mkdir /opt/javamelody/conf

In /opt/javamelody/conf you have to create the launching script, called javamelody.sh:
#!/bin/sh
#
# JavaMelody script
# 09/03/2017
#
# https://github.com/javamelody/javamelody/wiki/UserGuideAdvanced#3-simpler-alternative-of-deployment-of-the-webapp-of-monitoring
#
# Params:
# - JMELODY_CONSOLE_LOG : Log directory
# - JMELODY_PIDFILE : File where the pid is stored
#
#

JAVA_OPTS="-server -Xmx256m"
JAVA_OPTS="$JAVA_OPTS -Djava.io.tmpdir=/opt/javamelody/work/tmp"
JAVA_OPTS="$JAVA_OPTS -Djavamelody.storage-directory=/opt/javamelody/work/storage"
JAVA_OPTS="$JAVA_OPTS -Djavamelody.authorized-users=sisifo:sisifo"

JMELODY_WAR=/opt/javamelody/javamelody.war

JMELODY_OPTS="--httpPort=9080"

# Run and store the PID
nohup java $JAVA_OPTS -jar $JMELODY_WAR $JMELODY_OPTS 0</dev/null 2>&1 >> $JMELODY_CONSOLE_LOG &
JMELODY_PID=$!
echo $JMELODY_PID > $JMELODY_PIDFILE

JavaMelody will be executed with its own user, called 'javamelody':
useradd -U javamelody

chown -Rf javamelody:javamelody /opt/javamelody/

Now, you have to create the service script called javamelody in /etc/init.d:
#!/bin/sh
#
# JavaMeoldy Collect Server script
#


# Source function library.
. /etc/init.d/functions

JMELODY_SCRIPT=/opt/javamelody/conf/javamelody.sh


JMELODY_CONSOLE_LOG=/var/log/javamelody/javamelody.log
JMELODY_PIDFILE=/var/run/javamelody.pid


JMELODY_USER=javamelody
prog='JavaMelody Collect Server'



# Set defaults.
if [ -z "$JMELODY_PIDFILE" ]; then
  JMELODY_PIDFILE=/var/run/javamelody.pid

fi
export JMELODY_PIDFILE


if [ -z "$JMELODY_CONSOLE_LOG" ]; then
  JMELODY_CONSOLE_LOG=/var/log/javamelody/javamelody.log
fi


if [ -z "$STARTUP_WAIT" ]; then
  STARTUP_WAIT=30
fi

if [ -z "$SHUTDOWN_WAIT" ]; then
  SHUTDOWN_WAIT=30
fi



start() {
  echo -n "Starting $prog: "
  if [ -f $JMELODY_PIDFILE ]; then
    read ppid < $JMELODY_PIDFILE
    if [ `ps --pid $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
      echo -n "$prog is already running"
      failure
      echo
      return 1
    else
      rm -f $JMELODY_PIDFILE
    fi
  fi
  mkdir -p $(dirname $JMELODY_CONSOLE_LOG)
  cat /dev/null > $JMELODY_CONSOLE_LOG
  chown -Rf $JMELODY_USER $(dirname $JMELODY_CONSOLE_LOG) || true


  mkdir -p $(dirname $JMELODY_PIDFILE)
  chown $JMELODY_USER $(dirname $JMELODY_PIDFILE) || true

  
  daemon --user $JMELODY_USER --pidfile $JMELODY_PIDFILE JMELODY_PIDFILE=$JMELODY_PIDFILE JMELODY_CONSOLE_LOG=$JMELODY_CONSOLE_LOG $JMELODY_SCRIPT
 

  count=0
  launched=false

  until [ $count -gt $STARTUP_WAIT ]
  do
    grep 'Winstone Servlet * Engine' $JMELODY_CONSOLE_LOG > /dev/null
    if [ $? -eq 0 ] ; then
      launched=true
      break
    fi
    sleep 1
    let count=$count+1;
  done


  success
  echo
  return 0
}

stop() {
  echo -n $"Stopping $prog: "
  count=0;

  if [ -f $JMELODY_PIDFILE ]; then
    read kpid < $JMELODY_PIDFILE
    let kwait=$SHUTDOWN_WAIT

    # Try issuing SIGTERM

    kill -15 $kpid
    until [ `ps --pid $kpid 2> /dev/null | grep -c $kpid 2> /dev/null` -eq '0' ] || [ $count -gt $kwait ]
    do
      sleep 1
      let count=$count+1;
    done

    if [ $count -gt $kwait ]; then
      kill -9 $kpid
    fi
  fi
  rm -f $JMELODY_PIDFILE
  success
  echo
}

status() {
  if [ -f $JMELODY_PIDFILE ]; then
    read ppid < $JMELODY_PIDFILE
    if [ `ps --pid $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
      echo "$prog is running (pid $ppid)"
      return 0
    else
      echo "$prog dead but pid file exists"
      return 1
    fi
  fi
  echo "$prog is not running"
  return 3
}

case "$1" in
  start)
      start
      ;;
  stop)
      stop
      ;;
  restart)
      $0 stop
      $0 start
      ;;
  status)
      status
      ;;
  *)
      ## If no parameters are given, print which are avaiable.
      echo "Usage: $0 {start|stop|status|restart|reload}"
      exit 1
      ;;
esac

Finally, just configure it as an automatic service:
chmod +x /etc/init.d/javamelody

chkconfig --add javamelody
chkconfig javamelody on
service javamelody start

domingo, 12 de febrero de 2017

Centos 6 - Configure SonarQube with PostgreSQL

Just for my reminder.
How to configure SonarQube 5.X with PostgreSQL 8.X in Centos 6.6

Install postgresql.
yum install postgresql-server

service postgresql initdb

chkconfig postgresql on
service postgresql start

Configure access.
Edit /var/lib/pgsql/data/pg_hba.conf file and set the following config:
local all all trust
host all 127.0.0.1/32 trust

Create sonar user and database (you have to restart the service before).
psql -U postgres
postgres=# create user sonar with password 'sonar';
postgres=# create database sonar with owner sonar encoding 'UTF8';

Configure sonar.
Edit /usr/local/sonarqube/conf/sonar.properties file:
(...)

# User credentials.
# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar

#----- Embedded Database (default)
# H2 embedded database server listening port, defaults to 9092
#sonar.embeddedDatabase.port=9092

(...)

#----- PostgreSQL 8.x/9.x
# If you don't use the schema named "public", please refer to http://jira.sonarsource.com/browse/SONAR-5000
sonar.jdbc.url=jdbc:postgresql://localhost/sonar

(...)

Done.