小编Kon*_*rad的帖子

Java进程内存使用情况(jcmd vs pmap)

我在docker容器内的Java 8上运行了一个java应用程序.该过程启动Jetty 9服务器并部署Web应用程序.传递以下JVM选项:-Xms768m -Xmx768m.

最近我注意到这个过程耗费了大量的内存:

$ ps aux 1
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
app          1  0.1 48.9 5268992 2989492 ?     Ssl  Sep23   4:47 java -server ...

$ pmap -x 1
Address           Kbytes     RSS   Dirty Mode  Mapping
...
total kB         5280504 2994384 2980776

$ jcmd 1 VM.native_memory summary
1:

Native Memory Tracking:

Total: reserved=1378791KB, committed=1049931KB
-                 Java Heap (reserved=786432KB, committed=786432KB)
                            (mmap: reserved=786432KB, committed=786432KB) 

-                     Class (reserved=220113KB, committed=101073KB)
                            (classes #17246)
                            (malloc=7121KB #25927) 
                            (mmap: …
Run Code Online (Sandbox Code Playgroud)

java memory jvm pmap

7
推荐指数
2
解决办法
6549
查看次数

Spring Secuirty Oauth 2 - 多用户身份验证服务

我的应用程序提供的oauth2令牌服务与以下github项目中提供的服务相同:https://github.com/iainporter/oauth2-provider

它基于Spring Security OAuth2.

我提供了UserDetailsS​​ervice的自定义实现:

<bean id="userService" class="org.example.core.service.DBUserServiceImpl" />
Run Code Online (Sandbox Code Playgroud)

和以下用户身份验证管理器:

<sec:authentication-manager alias="userAuthenticationManager">
    <sec:authentication-provider user-service-ref="userService">
        <sec:password-encoder ref="passwordEncoder" />
    </sec:authentication-provider>
</sec:authentication-manager>
Run Code Online (Sandbox Code Playgroud)

现在我想提供其他用户身份验证方法(其他UserDetailsS​​ervice),例如:

<bean id="otherUserService" class="org.example.core.service.LDAPUserServiceImpl" />
Run Code Online (Sandbox Code Playgroud)

不幸的是我没有找到如何在文档中做到这一点的方法.在请求级别,我想区分哪个方法(哪个用户服务)使用:

  • 查询参数
  • http标头(例如RealmName)

java spring oauth spring-security spring-security-oauth2

5
推荐指数
1
解决办法
9973
查看次数

Spring Data - 页面请求 - 按功能排序

我试图在基于 Spring 4.1.1.RELEASE 和 Hibernate 4.2.16.Final 的 Java 应用程序中实现分页。

我有以下代码:

PageRequest pageRequest = new PageRequest(pageNumber, pageSize, new Sort(new Order(Direction.DESC, "name"))
Page page = myRepository.findAll(specification, pageRequest);
Run Code Online (Sandbox Code Playgroud)

一切正常。但是现在我想按 sql 函数值排序,例如:ORDER BY CHAR_LENGTH(name)

当我在 中输入这样的子句时,它完美地工作@org.springframework.data.jpa.repository.Query,例如:

public class MyRepository {
  @Query("select e from MyEntity e order by CHAR_LENGTH(e.name)")
  public List<MyEntity> findAllOrderedByNameLength();
}
Run Code Online (Sandbox Code Playgroud)

不幸的是我无法使用 PageRequest 来做到这一点。当我将:ORDER BY CHAR_LENGTH(name)作为属性传递给 PageRequest 时:

PageRequest pageRequest = new PageRequest(pageNumber, pageSize, new Sort(new Order(Direction.DESC, "ORDER BY CHAR_LENGTH(name)"))
Page page = myRepository.findAll(specification, pageRequest);
Run Code Online (Sandbox Code Playgroud)

抛出以下异常:

Caused …
Run Code Online (Sandbox Code Playgroud)

java sql spring hibernate jpa

5
推荐指数
1
解决办法
7293
查看次数

H2索引名称唯一性

我对h2数据库中索引名称的唯一性有一个小问题.使用mysql/mariadb,可以同时为表A和表B定义名为"X"的索引.使用h2数据库是不可能的,因为索引的名称对于每个数据库应该是唯一的.

这对我来说是一个问题,因为我有一个基础JPA实体类,定义了以下属性:

@org.hibernate.annotations.Index(name = "X")
protected String x;
Run Code Online (Sandbox Code Playgroud)

它由类A和B继承,并且类B的索引创建失败,并出现以下错误:

ERROR [main] o.h.tool.hbm2ddl.SchemaUpdate   - HHH000388: Unsuccessful: create index X on B(x)
ERROR [main] o.h.tool.hbm2ddl.SchemaUpdate   - Index "X" already exists
Run Code Online (Sandbox Code Playgroud)

是否有可能告诉hibernate自动创建索引名称或以某种方式为H2创建一个适配器,它将使用表名称为这些索引名称添加前缀?

java orm hibernate jpa h2

5
推荐指数
1
解决办法
1277
查看次数

Rspec,预计范围变化

我在 Rspec 中编写了以下测试:

expect {
         ...
        }.to change( User.where(:profile.exists => true), :count ).by(1)
Run Code Online (Sandbox Code Playgroud)

但此作用域仅执行一次,并且始终是具有相同大小的相同数组。如何让rspec在expect中运行代码之前和之后执行这个范围?

scope rspec ruby-on-rails

4
推荐指数
1
解决办法
3370
查看次数

H2 服务器在调试时挂起

我有一个 Spring 应用程序,它在内存 H2 数据库中启动以下内容用于 JUnit 测试:

db.jdbc.driver=org.h2.Driver
db.jdbc.url=jdbc:h2:mem:sampledb
db.jdbc.username=
db.jdbc.password=
hibernate.dialect=org.hibernate.dialect.H2Dialect
Run Code Online (Sandbox Code Playgroud)

我想在 JUnit 测试运行时轻松调试并同时浏览数据库状态,因此在测试套件之前我启动 H2 服务器:

org.h2.tools.Server.createTcpServer("-tcpPort", "9092", "-tcpAllowOthers").start();
Run Code Online (Sandbox Code Playgroud)

不幸的是,当我放置任何暂停所有线程的断点时,它也会暂停 H2 服务器线程,因此我无法连接。我无法在不同的进程中启动 H2 服务器,因为无法从 VM 外部访问内存数据库。

我知道我可以使用其他类型的断点(仅挂起当前线程),但这是一种限制。您是否为此类问题找到了其他解决方案?

java junit spring multithreading h2

4
推荐指数
1
解决办法
1810
查看次数

没有请求的Facebook好友对话

Facebook开发者API是否提供简单的朋友选择对话框,不会向Facebook发送任何请求?

我只想让用户从列表中选择多个朋友并获取这些朋友的ID.

我发现了这样的东西:https: //github.com/mbrevoort/jquery-facebook-multi-friend-selector 但我很惊讶我必须使用外部插件.

facebook

3
推荐指数
1
解决办法
2794
查看次数

JAXRS CXF - 获取http标头而不指定方法参数

是否可以在JAXRS资源方法中检索http标头而无需将这些标头明确指定为方法参数?

例如,我有一个以下界面:

@Path("/posts")
public interface PostsResource {

  @GET
  public List<Post> getAllPosts();
}
Run Code Online (Sandbox Code Playgroud)

以及实现此接口的以下类:

public class PostsResourceImpl implements PostsResource {

  @Autowired
  private PostsService postsService;

  public List<Post> getAllPosts() {
    return postsService.getAllPosts();
  }

}
Run Code Online (Sandbox Code Playgroud)

我不想将我的方法签名更改为:

public List<Post> getAllPosts(@HeaderParam("X-MyCustomHeader") String myCustomHeader);
Run Code Online (Sandbox Code Playgroud)

此标头将由客户端上的拦截器添加,因此客户端代码不知道放在此处的内容,这不应该是显式方法参数.

java rest cxf jax-rs http-headers

3
推荐指数
1
解决办法
2778
查看次数

Rails,测试邮件地址

我的应用程序发送包含绝对网址的电子邮件。

我在 config/environment/development.rb 中设置主机

config.action_mailer.default_url_options = { :host => 'localhost:3000' }
Run Code Online (Sandbox Code Playgroud)

现在我想测试电子邮件是否包含有效的 url。使用正则表达式,我从电子邮件中取出完整的 url,并想使用 Capybara 函数访问这个地址。

mail = ActionMailer::Base.deliveries.last
address = mail.body.to_s[%r{http.+/edit}]
visit address;
Run Code Online (Sandbox Code Playgroud)

但是我不知道在 config/environment/test.rb 中应该设置什么主机

当我设置 localhost:3000 时,它会尝试连接到由rails server命令启动的本地服务器。

你有什么想法来解决这个问题吗?

host rspec ruby-on-rails mailer capybara

2
推荐指数
1
解决办法
1588
查看次数

无法构建EntityManagerFactory - Spring和Hibernate

我将使用hibernate3的spring应用程序部署到glassfish服务器3.1.2时出现以下错误:

SEVERE: Exception while loading the app
SEVERE: log4j:ERROR LogMananger.repositorySelector was null likely due to error in class reloading, using NOPLoggerRepository.
SEVERE: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in URL [file:/home/konrad/NetBeansProjects/webapp/experimental/SpringBar2012_1/SpringWebApp4/target/training/WEB-INF/classes/persistence-context.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: webappPersistenceUnit] Unable to build EntityManagerFactory
Run Code Online (Sandbox Code Playgroud)

这是我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.training.spring</groupId>
  <artifactId>demoWebAppMVC</artifactId>
  <packaging>war</packaging>
  <version>1.0</version>
  <name>SpringWebAppMVC</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope> …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate persistence.xml hibernate-entitymanager

2
推荐指数
1
解决办法
2万
查看次数

在Rails中测试作用域链的最佳方法

在我所有的ruby on rails应用程序中,我尝试不在控制器中使用数据库,因为它们应该独立于持久性类.我用了嘲笑.

以下是rspec和rspec-mock的示例:

class CouponsController < ApplicationController
  def index
    @coupons = Coupon.all
  end
end

require 'spec_helper'
describe CouponsController do
  let(:all_coupons) { mock } 
  it 'should return all coupons' do
    Coupon.should_receive(:all).and_return(all_coupons)
    get :index
    assigns(:coupons).should == all_coupons
    response.should be_success
  end
end
Run Code Online (Sandbox Code Playgroud)

但是如果控制器包含更复杂的范围,例如:

class CouponsController < ApplicationController
  def index
    @coupons = Coupon.unredeemed.by_shop(shop).by_country(country)
  end
end
Run Code Online (Sandbox Code Playgroud)

你知道测试simillar范围链的任何好方法吗?

我认为以下测试看起来不太好:

require 'spec_helper'
describe CouponsController do
  it 'should return all coupons' do
    Coupon.should_receive(:unredeemed).and_return(result = mock)
    result.should_receive(:by_shop).with(shop).and_return(result)
    result.should_receive(:by_country).with(country).and_return(result)
    get :index
    assigns(:coupons).should == result
    response.should be_success
  end
end
Run Code Online (Sandbox Code Playgroud)

testing scope ruby-on-rails mocking stub

2
推荐指数
1
解决办法
931
查看次数